Complete part 1 of day 2 puzzle
This commit is contained in:
parent
45c118dcac
commit
418c7f13fb
1000
2/input.txt
Normal file
1000
2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
25
2/navigator.hs
Normal file
25
2/navigator.hs
Normal 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
6
2/sampleinstructions.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
forward 5
|
||||||
|
down 5
|
||||||
|
forward 8
|
||||||
|
up 3
|
||||||
|
down 8
|
||||||
|
forward 2
|
Loading…
Reference in a new issue