Day 17*
This commit is contained in:
parent
b0b3abff27
commit
3ec1948753
2 changed files with 27 additions and 21 deletions
|
@ -200,7 +200,7 @@ day16 = do
|
||||||
day17 :: IO ()
|
day17 :: IO ()
|
||||||
day17 = do
|
day17 = do
|
||||||
putText "Day 17:"
|
putText "Day 17:"
|
||||||
sol1 <- Day17.solution1 2017 Day17.input
|
let sol1 = Day17.solution1 2017 Day17.input
|
||||||
showSol "Solution 1" (int (fromMaybe 0 sol1))
|
showSol "Solution 1" (int (fromMaybe 0 sol1))
|
||||||
let sol2 = Day17.solution2 50000000 Day17.input
|
let sol2 = Day17.solution2 50000000 Day17.input
|
||||||
showSol "Solution 2" (int sol2)
|
showSol "Solution 2" (int sol2)
|
||||||
|
|
46
src/Day17.hs
46
src/Day17.hs
|
@ -101,7 +101,7 @@ nextPreStep nbSteps b@Buffer{..} =
|
||||||
|
|
||||||
showBuffer :: Buffer -> Text
|
showBuffer :: Buffer -> Text
|
||||||
showBuffer Buffer{..} =
|
showBuffer Buffer{..} =
|
||||||
zip [0..] elems
|
zip [0..] (take 30 elems)
|
||||||
& map (\(i,v) -> if i == pos
|
& map (\(i,v) -> if i == pos
|
||||||
then "(" <> show v <> ") "
|
then "(" <> show v <> ") "
|
||||||
else show v <> " ")
|
else show v <> " ")
|
||||||
|
@ -124,38 +124,44 @@ addElem roundNumber b@Buffer {..} =
|
||||||
++ [roundNumber]
|
++ [roundNumber]
|
||||||
++ drop (pos + 1) elems
|
++ drop (pos + 1) elems
|
||||||
|
|
||||||
solution1 :: Int -> Int -> IO (Maybe Int)
|
solution1 :: Int -> Int -> Maybe Int
|
||||||
solution1 nbOcc nbSteps = go initState 0
|
solution1 nbOcc nbSteps = finalState & elems & elemAt (1 + (pos finalState))
|
||||||
where
|
where
|
||||||
initState = Buffer [0] 0
|
initState = Buffer [0] 0
|
||||||
go st n
|
finalState = genState nbOcc nbSteps 0 initState
|
||||||
| n == nbOcc = st & elems & elemAt (1 + (pos st)) & return
|
|
||||||
| otherwise = do
|
genState :: Int -> Int -> Int -> Buffer -> Buffer
|
||||||
-- print st
|
genState nbOcc nbSteps n st
|
||||||
-- putText $ "> " <> showBuffer st
|
| n == nbOcc = -- traceShow st $
|
||||||
go (nextStep nbSteps (n+1) st) (n+1)
|
st
|
||||||
elemAt :: Int -> [a] -> Maybe a
|
| otherwise = -- traceShow (showBuffer st) $
|
||||||
elemAt 0 (x:xs) = Just x
|
genState nbOcc nbSteps (n+1) (nextStep nbSteps (n+1) st)
|
||||||
elemAt n (_:xs) = elemAt (n-1) xs
|
|
||||||
elemAt _ _ = Nothing
|
elemAt :: Int -> [a] -> Maybe a
|
||||||
|
elemAt 0 (x:xs) = Just x
|
||||||
|
elemAt n (_:xs) = elemAt (n-1) xs
|
||||||
|
elemAt _ _ = Nothing
|
||||||
|
|
||||||
data Buf2 = Buf2 { position :: Int
|
data Buf2 = Buf2 { position :: Int
|
||||||
, size :: Int
|
, size :: Int
|
||||||
, lastSol :: Int
|
, lastSol :: Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
solution2 :: Int -> Int -> Int
|
solution2 :: Int -> Int -> Int
|
||||||
solution2 nbOcc nbSteps = go initState 1
|
solution2 nbOcc nbSteps = go initState 1
|
||||||
where
|
where
|
||||||
initState = Buf2 0 1 0
|
initState = Buf2 0 1 0
|
||||||
go st@Buf2{..} n
|
go st@Buf2{..} n
|
||||||
| n == nbOcc = lastSol
|
| n == nbOcc = lastSol
|
||||||
| (position + nbSteps) `rem` size == 0 =
|
| (1 + position + nbSteps) `rem` size == 0 =
|
||||||
traceShow n $ go (st { position = 1
|
-- traceShow n $
|
||||||
, lastSol = n
|
go (st { position = (1 + position + nbSteps) `rem` size
|
||||||
, size = size + 1
|
, lastSol = n
|
||||||
})
|
, size = size + 1
|
||||||
(n+1)
|
})
|
||||||
| otherwise = go (st { position = (1 + position + nbSteps) `rem` size
|
(n+1)
|
||||||
|
| otherwise = go (st { position = (1+ position + nbSteps) `rem` size
|
||||||
, size = size + 1 })
|
, size = size + 1 })
|
||||||
(n+1)
|
(n+1)
|
||||||
|
|
Loading…
Reference in a new issue