Solve day 1 part 2
This commit is contained in:
parent
fd2b0ed0cc
commit
0efd99220e
24
app/Main.hs
24
app/Main.hs
@ -1,6 +1,7 @@
|
||||
module Main (main) where
|
||||
|
||||
import Control.Monad.State
|
||||
import Data.Bifunctor
|
||||
import Data.Traversable
|
||||
import Text.Read (readMaybe)
|
||||
|
||||
@ -19,8 +20,21 @@ main = do
|
||||
. sum
|
||||
. flip evalState 50
|
||||
$ for input \(d, i) -> state \p ->
|
||||
let p' = step i d p
|
||||
let (_, p') = step i d p
|
||||
in (Count if p' == 0 then 1 else 0, p')
|
||||
print
|
||||
. sum
|
||||
. flip evalState 50
|
||||
$ for input \(d, i) -> state \p ->
|
||||
let (c, p') = step i d p
|
||||
c' = case d of
|
||||
R -> abs c
|
||||
L ->
|
||||
if
|
||||
| p == 0 -> abs c - 1
|
||||
| p' == 0 -> abs c + 1
|
||||
| otherwise -> abs c
|
||||
in (c', p')
|
||||
|
||||
data Direction = L | R
|
||||
deriving (Eq, Ord, Show)
|
||||
@ -34,7 +48,7 @@ newtype Inc = Inc Int
|
||||
newtype Count = Count Int
|
||||
deriving newtype (Eq, Ord, Show, Num)
|
||||
|
||||
step :: Inc -> Direction -> Pos -> Pos
|
||||
step (Inc i) d (Pos p) = Pos case d of
|
||||
L -> (p - i) `mod` 100
|
||||
R -> (p + i) `mod` 100
|
||||
step :: Inc -> Direction -> Pos -> (Count, Pos)
|
||||
step (Inc i) d (Pos p) = bimap Count Pos case d of
|
||||
L -> (p - i) `divMod` 100
|
||||
R -> (p + i) `divMod` 100
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user