Added day 1 and 2

This commit is contained in:
Riley Apeldoorn 2021-12-02 11:35:13 +01:00
commit a3ae16a66e
8 changed files with 3087 additions and 0 deletions

10
1/example.txt Normal file
View file

@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263

2000
1/input.txt Normal file

File diff suppressed because it is too large Load diff

22
1/solution.hs Normal file
View file

@ -0,0 +1,22 @@
import Data.List
main :: IO ()
main = readFile "1/input.txt" >>= print . solve . parse . lines
parse :: [String] -> [Int]
parse = map read
solve :: [Int] -> (Int, Int)
solve input = (f input, g input)
where f = countInc
g = countInc . map sum . slide 3
countInc :: [Int] -> Int
countInc (a : rest @ (b : _))
| a < b = countInc rest + 1
| otherwise = countInc rest
countInc _ = 0
slide :: Int -> [Int] -> [[Int]]
slide _ [] = []
slide n list = take n list : slide n (tail list)

6
2/example.txt Normal file
View file

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

1000
2/input.txt Normal file

File diff suppressed because it is too large Load diff

41
2/solution.hs Normal file
View file

@ -0,0 +1,41 @@
main :: IO ()
main = readFile "2/input.txt" >>= print . solve . parse . lines
parse :: [String] -> [Instruction]
parse = map (parseLine . words)
solve :: [Instruction] -> (Int,Int)
solve input = (f input, g input)
where f = uncurry (*) . simulate Abs
g = uncurry (*) . simulate (Aim 0)
data Nav = Aim Int
| Abs
data Instruction = Forward Int
| Down Int
| Up Int
parseLine :: [String] -> Instruction
parseLine [ s, x ]
| s == "forward" = Forward n
| s == "down" = Down n
| s == "up" = Up n
where
n = read x
simulate :: Nav -> [Instruction] -> (Int,Int)
simulate nav = iter nav (0,0)
iter :: Nav -> (Int,Int) -> [Instruction] -> (Int,Int)
iter _ r [] = r
iter Abs (x,y) (i:is) = case i of
Forward n -> iter Abs (x+n,y)
Down n -> iter Abs (x,y+n)
Up n -> iter Abs (x,y-n)
$ is
iter (Aim a) (x,y) (i:is) = case i of
Forward n -> iter (Aim a) (x+n,y+(a*n))
Down n -> iter (Aim (a+n)) (x,y)
Up n -> iter (Aim (a-n)) (x,y)
$ is

1
README.md Normal file
View file

@ -0,0 +1 @@
# Advent of Code 2021

7
shell.nix Normal file
View file

@ -0,0 +1,7 @@
{ pkgs ? import <nixpkgs> {} }: pkgs.mkShell {
nativeBuildInputs = with pkgs; [
haskell-language-server
cabal-install
ghc
];
}