final day 1
This commit is contained in:
parent
37a7566aed
commit
b3565d5596
1 changed files with 15 additions and 5 deletions
|
|
@ -1,17 +1,27 @@
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
import Debug.Trace (trace)
|
||||||
|
|
||||||
|
convertToInts = map (\it -> (if head it == 'L' then -1 else 1) * read (drop 1 it))
|
||||||
|
|
||||||
part1 :: String -> Int
|
part1 :: String -> Int
|
||||||
part1 = length . dropNonZero . scanl (+) 50 . convertToInts . lines
|
part1 = length . dropNonZero . scanl (+) 50 . convertToInts . lines
|
||||||
where
|
where
|
||||||
convertToInts = map (\it -> (if head it == 'L' then -1 else 1) * read (drop 1 it))
|
|
||||||
dropNonZero = filter ((0==) . (`mod` 100))
|
dropNonZero = filter ((0==) . (`mod` 100))
|
||||||
|
|
||||||
part2 :: String -> Int
|
part2 :: String -> Int
|
||||||
part2 = length . dropNonZero . scanl (+) 50 . makeSingleSteps . convertToInts . lines
|
part2 = go 50 . convertToInts . lines
|
||||||
where
|
where
|
||||||
convertToInts = map (\it -> (if head it == 'L' then -1 else 1) * read (drop 1 it))
|
go :: Int -> [Int] -> Int
|
||||||
dropNonZero = filter ((0==) . (`mod` 100))
|
go p [] = 0
|
||||||
makeSingleSteps = concatMap (\it -> replicate (abs it) (signum it))
|
go p (x:xs) = n + edgeCase + go p' xs
|
||||||
|
where
|
||||||
|
n = abs $ p' `div` 100 - p `div` 100
|
||||||
|
|
||||||
|
edgeCase = if x < 0 && p' `mod` 100 == 0 then 1 else 0
|
||||||
|
+ if x < 0 && p `mod` 100 == 0 then -1 else 0
|
||||||
|
|
||||||
|
p' = (p + x)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue