From 73c3b07df614d7392f1c5ae2e43af0d3a59e8415 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Sat, 15 Dec 2018 21:59:54 -0800 Subject: [PATCH] Day 12. --- README.md | 2 +- app/Main.hs | 2 +- input/12.txt | 34 ++++++++++++++++++++++++++++++ src/Day12.hs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 input/12.txt diff --git a/README.md b/README.md index ab81969..f574cae 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ To arrive at [⟨ortho|normal⟩](https://hilb.ert.space) as December comes. | 09 | ~ 7 | | 10 | ~ 6.5 | | 11 | ~ 7 | -| 12 | | +| 12 | ~ 1.5 | | 13 | | | 14 | | | 15 | | diff --git a/app/Main.hs b/app/Main.hs index 5c28b90..4dd8ef2 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -30,4 +30,4 @@ import qualified Day24 import qualified Day25 main :: IO () -main = Day11.main +main = Day12.main diff --git a/input/12.txt b/input/12.txt new file mode 100644 index 0000000..a0ea0b2 --- /dev/null +++ b/input/12.txt @@ -0,0 +1,34 @@ +initial state: #.##.#.##..#.#...##...#......##..#..###..##..#.#.....##..###...#.#..#...######...#####..##....#..### + +##.## => . +##... => # +..#.# => # +#.... => . +#..#. => # +.#### => . +.#..# => . +.##.# => . +#.##. => # +####. => . +..##. => . +##..# => . +.#.## => # +.#... => . +.##.. => # +..#.. => # +#..## => # +#.#.. => # +..### => # +...#. => # +###.. => . +##.#. => # +#.#.# => # +##### => # +....# => . +#.### => . +.#.#. => # +.###. => # +...## => . +..... => . +###.# => # +#...# => . diff --git a/src/Day12.hs b/src/Day12.hs index f1cfc5e..bde6246 100644 --- a/src/Day12.hs +++ b/src/Day12.hs @@ -1,6 +1,61 @@ module Day12 (main) where +import Prelude hiding (replicate, fromList) +import Data.Bits ((.|.), shift) +import Data.List (sort) +import Data.Foldable (toList) +import Data.Vector (Vector, (!)) +import qualified Data.Vector as V (fromList) +import Data.Sequence (Seq(..), (><), fromList, takeWhileL, takeWhileR, replicate, empty) +import Debug.Trace (traceShow) + +type PotState = (Int, Pots) +type Pots = Seq Char +type Notes = Vector Char + +parse :: String -> (Notes, Pots) +parse str = + let init:_:ns = lines str + seq = fromList . drop 15 $ init + notes = V.fromList . map last . reverse . sort $ ns + in (notes, seq) + +potsToInt :: Seq Char -> Int +potsToInt = potsToIntRec 0 5 + where + potsToIntRec val 0 _ = val + potsToIntRec val n (c:<|cs) = potsToIntRec (val `shift` 1 .|. fromEnum (c == '#')) (n - 1) cs + +appendEmptyPots :: PotState -> PotState +appendEmptyPots potState@(lp, ps) = + let lPots = max 0 . (5 -) . length . takeWhileL (== '.') $ ps + rPots = max 0 . (5 -) . length . takeWhileR (== '.') $ ps + in (lp - lPots, replicate lPots '.' >< ps >< replicate rPots '.') + +grow :: Notes -> PotState -> PotState +grow notes potState = + let (leftPot, pots) = appendEmptyPots potState + (_, newPots) = foldr (\_ (op@(p:<|ps), np) -> (ps, np :|> notes ! potsToInt op)) (pots, empty) [0 .. length pots - 5] + in (leftPot, fromList ".." >< newPots >< fromList "..") + +sumPotNums :: PotState -> Int +sumPotNums (leftPot, pots) = sum . map fst . filter ((== '#') . snd) . zip [leftPot..] . toList $ pots + +part1 :: Notes -> Pots -> Int +part1 notes pots = sumPotNums $ iterate (grow notes) (0, pots) !! 20 + +part2 :: Notes -> Pots -> Int +part2 notes pots = + let sums = map sumPotNums $ iterate (grow notes) (0, pots) + diffs = zipWith (-) (tail sums) sums + index = findRepeatIndex 1 diffs + diff = sums !! (index + 1) - sums !! index + in sums !! index + diff * (50000000000 - index) + where findRepeatIndex n (d1:d2:ds) = + if d1 == d2 then n else findRepeatIndex (n + 1) (d2:ds) + main :: IO () main = do - input <- readFile "input/12.txt" - print input \ No newline at end of file + (notes, pots) <- parse <$> readFile "input/12.txt" + print $ part1 notes pots + print $ part2 notes pots