41 lines
1.4 KiB
Haskell
41 lines
1.4 KiB
Haskell
import System.IO
|
|
import Control.Monad
|
|
|
|
main :: IO()
|
|
main = do handle <- openFile "../rust/inputs/01.txt" ReadMode
|
|
contents <- hGetContents handle
|
|
print (doday1_1 contents)
|
|
hClose handle
|
|
|
|
-- Day 1: ...
|
|
|
|
-- Part One (correct)
|
|
|
|
doday1_1 :: String -> String
|
|
doday1_1 input = show (countZeroRotations (words input) 50)
|
|
|
|
countZeroRotations :: [String] -> Int -> Int
|
|
countZeroRotations [] _ = 0
|
|
countZeroRotations (x : xs) pos = countZeroRotations xs (fst (rotateDial x pos)) + snd (rotateDial x pos)
|
|
|
|
rotateDial :: String -> Int -> (Int, Int)
|
|
rotateDial (direction : r) pos
|
|
| direction == 'L' && newposl == 0 = (normalizeHundred newposl, 1)
|
|
| direction == 'L' = (normalizeHundred newposl, 0)
|
|
| direction == 'R' && newposr == 0 = (normalizeHundred newposr, 1)
|
|
| direction == 'R' = (normalizeHundred newposr, 0)
|
|
where newposl = normalizeHundred (pos - (read r))
|
|
newposr = normalizeHundred (pos + (read r))
|
|
|
|
normalizeHundred :: Int -> Int
|
|
normalizeHundred x
|
|
| x >= 0 && x < 100 = x
|
|
| x >= 100 = normalizeHundred (x - 100)
|
|
| x < 0 = normalizeHundred (x + 100)
|
|
| True = x
|
|
|
|
-- Part Two
|
|
|
|
doday1_2 :: String -> String
|
|
doday1_2 input = undefined
|