Complete part 1 of day 2 puzzle

main
Tristan Daniël Maat 2021-12-03 03:35:15 +00:00
parent 45c118dcac
commit 418c7f13fb
Signed by: tlater
GPG Key ID: 49670FD774E43268
3 changed files with 1031 additions and 0 deletions

1000
2/input.txt Normal file

File diff suppressed because it is too large Load Diff

25
2/navigator.hs Normal file
View File

@ -0,0 +1,25 @@
main :: IO ()
main = do
instructions <- getContents
let
parsed = parseInstructions instructions
location = navigate parsed
solution1 = uncurry (*) location
print solution1
parseInstructions :: String -> [(Int, Int)]
parseInstructions instructions =
map parseLine (lines instructions)
where
parseLine line = case break (== ' ') line of
("forward", distance) -> (read distance, 0)
("down", distance) -> (0, read distance)
("up", distance) -> (0, (-read distance))
_ -> error "Invalid line"
navigate :: [(Int, Int)] -> (Int, Int)
navigate instructions =
foldl1 sumTuples instructions
where
sumTuples :: (Int, Int) -> (Int, Int) -> (Int, Int)
sumTuples (a, b) (c, d) = (a + c, b + d)

6
2/sampleinstructions.txt Normal file
View File

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