From 4a14b7d078a4559e6b9aff551e892aaaab7e4411 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 23 Dec 2018 14:53:01 +0000 Subject: [PATCH] Day 18 --- advent-of-code.cabal | 19 +++ data/advent18-small.txt | 10 ++ data/advent18.txt | 50 ++++++++ problems/day18.html | 267 +++++++++++++++++++++++++++++++++++++++ src/advent18/advent18.hs | 139 ++++++++++++++++++++ 5 files changed, 485 insertions(+) create mode 100644 data/advent18-small.txt create mode 100644 data/advent18.txt create mode 100644 problems/day18.html create mode 100644 src/advent18/advent18.hs diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 22868e7..3e787cf 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -208,3 +208,22 @@ executable advent17 , text , megaparsec , containers + +executable advent18 + hs-source-dirs: src/advent18 + main-is: advent18.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , text + , megaparsec + , containers + +executable advent19 + hs-source-dirs: src/advent19 + main-is: advent19.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , containers + , mtl + , text + , megaparsec \ No newline at end of file diff --git a/data/advent18-small.txt b/data/advent18-small.txt new file mode 100644 index 0000000..13d299d --- /dev/null +++ b/data/advent18-small.txt @@ -0,0 +1,10 @@ +.#.#...|#. +.....#|##| +.|..|...#. +..|#.....# +#.#|||#|#| +...#.||... +.|....|... +||...#|.#| +|.||||..|. +...#.|..|. diff --git a/data/advent18.txt b/data/advent18.txt new file mode 100644 index 0000000..77f7e29 --- /dev/null +++ b/data/advent18.txt @@ -0,0 +1,50 @@ +...##.....|..|#.##|#.......#...#|.|..#..#|#||#..## +|#.#...||.#|.....|#|.#.|....#......|#.|....#....#. +..#..#.#..#..||.#.##|#|.|..|........|#...#|.|.||#. +|#....|.|.#.|....||###|.|#|..........#...#....#|.# +#.....|....#...#|||#.......|#.........|#..###|.#|# +..#.|.|...||#...||.#|...#.|...###|....|||.#|.#.... +.|..#..#.##|...|##...#.##.|.|..|.#...#|..|.#|..... +#||.|||.#..###|....|.|...|||...|||.|...||||#...#|. +#|..||.#..#..#|.|##.......#|#....#...##.|#...#.#|| +..#....#...|....||.#||.##|#.#|.....#.#|.|#...#.#.| +.|#|.||......|#....#..|#|#.##.|...|....|#.|.#.#.#. +.....||##...||..#.||......#|.||..|.|..|..........| +#|#..#||.|....||.|..|...||....|..|...##........|.. +.|#.|#|..|##...||.|##.#...#|#..|.#.|.|#..|..#...#. +##.......#.|....|.|#.|.#..#.#|.#|..###...#...#..|. +#..#.....##.....#...##.|..#..##.|.#.##.....|.|..## +....#.||....|.....|...|..#...|.|.....#.##||....#.. +|#.#..#..#.........||#..|.|#...........|.|#......# +|#..#.|..#..|.#..||.##..|.#.|..||...||.|.#|....#.| +#.#|.....#..||#....#.|..#..|.#|....|..|....|#....| +......||....||||.........##|....#..||.|.......|... +#.....|..#.|.##|..|.|.|#.#..|.....|..|#.|..|.|..#. +###|.|.#..##.#.|#...##||.#|...##|#....##...#....#| +..|...##....#.....##......|.#|..|...#|#.||#..|.|.. +|.||#..||....#..|......#|.#.##.#..|.#|#.|...##||#. +#.#.|..|.....|#.#|#.##||...||.||.||.||......|....# +..#|.......|#|.|....|...#.#|..||.#.#||.#..#|.....| +...|#.##|...#.|...#.|....#.##...|.#.#..#.....#|... +.....#.....|...#.##.#.|......|...#.#|.....|.#.|.#. +..#....#..|...|||..#..#.......#|..|..#|.|...|#..#| +..||||........#|.#.##...|..|||#...|...#|.|###...|| +||#.##.||.....#..||...........|....|.....|#|||#... +..|||#..|.||..|#.|#.#...#|#...|.###.#...|...#....# +...........|||..|||#.#...#|....####.##||...|..|... +.##.#|##|....#..|.#|#|.......|.|.....#..|...|||... +..#...|.|..#..#..|###.#........|......#.#....##|.. +#.|..|#|.#.#..||...#.#.....|#.|#......#||.#.|.#... +...#.....##.|##...#..#|.#.#.#..||..|#....#####..|. +.....#...#.#|........##|#.#.#....|.|........#|||.. +|..##|#.|.|...||#..#.|..#..##..#|.......#|#.#..|.| +|..|....#..|..|....#.|...##....|#...|...|.|#.|...| +..|.|#|#...|.##..##|||..#..|...#..#||...#|.|.#|.|. +.|...||.##.#...|.#.#...###||.|###|......|#....#### +.|.#..#.|..|##.....|.|.#.#||.#|.......|#..|##.||.. +...|..||...#|.|..|.....|.#.##.#..#...|##.|..##..#. +##.|.#...#|#|...##|#.....|..|#.#.#.##.#|...||..#.. +##|.#..|....#..#|.#|..||...#......#.#|.#.#|..#|#.. +|....|.#|#......|....#.#.....|..#.|...|##....|.||# +|.#.|...#...||#.##...|.|..#||...|..##|#......#.|.. +...|#.|......#....#......#.....#.#|..||..|#..#...# diff --git a/problems/day18.html b/problems/day18.html new file mode 100644 index 0000000..d675249 --- /dev/null +++ b/problems/day18.html @@ -0,0 +1,267 @@ + + + + +Day 18 - Advent of Code 2018 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 37*

   0x0000|2018

+ + + +
+

--- Day 18: Settlers of The North Pole ---

On the outskirts of the North Pole base construction project, many Elves are collecting lumber.

+

The lumber collection area is 50 acres by 50 acres; each acre can be either open ground (.), trees (|), or a lumberyard (#). You take a scan of the area (your puzzle input).

+

Strange magic is at work here: each minute, the landscape looks entirely different. In exactly one minute, an open acre can fill with trees, a wooded acre can be converted to a lumberyard, or a lumberyard can be cleared to open ground (the lumber having been sent to other projects).

+

The change to each acre is based entirely on the contents of that acre as well as the number of open, wooded, or lumberyard acres adjacent to it at the start of each minute. Here, "adjacent" means any of the eight acres surrounding that acre. (Acres on the edges of the lumber collection area might have fewer than eight adjacent acres; the missing acres aren't counted.)

+

In particular:

+
    +
  • An open acre will become filled with trees if three or more adjacent acres contained trees. Otherwise, nothing happens.
  • +
  • An acre filled with trees will become a lumberyard if three or more adjacent acres were lumberyards. Otherwise, nothing happens.
  • +
  • An acre containing a lumberyard will remain a lumberyard if it was adjacent to at least one other lumberyard and at least one acre containing trees. Otherwise, it becomes open.
  • +
+

These changes happen across all acres simultaneously, each of them using the state of all acres at the beginning of the minute and changing to their new form by the end of that same minute. Changes that happen during the minute don't affect each other.

+

For example, suppose the lumber collection area is instead only 10 by 10 acres with this initial configuration:

+
Initial state:
+.#.#...|#.
+.....#|##|
+.|..|...#.
+..|#.....#
+#.#|||#|#|
+...#.||...
+.|....|...
+||...#|.#|
+|.||||..|.
+...#.|..|.
+
+After 1 minute:
+.......##.
+......|###
+.|..|...#.
+..|#||...#
+..##||.|#|
+...#||||..
+||...|||..
+|||||.||.|
+||||||||||
+....||..|.
+
+After 2 minutes:
+.......#..
+......|#..
+.|.|||....
+..##|||..#
+..###|||#|
+...#|||||.
+|||||||||.
+||||||||||
+||||||||||
+.|||||||||
+
+After 3 minutes:
+.......#..
+....|||#..
+.|.||||...
+..###|||.#
+...##|||#|
+.||##|||||
+||||||||||
+||||||||||
+||||||||||
+||||||||||
+
+After 4 minutes:
+.....|.#..
+...||||#..
+.|.#||||..
+..###||||#
+...###||#|
+|||##|||||
+||||||||||
+||||||||||
+||||||||||
+||||||||||
+
+After 5 minutes:
+....|||#..
+...||||#..
+.|.##||||.
+..####|||#
+.|.###||#|
+|||###||||
+||||||||||
+||||||||||
+||||||||||
+||||||||||
+
+After 6 minutes:
+...||||#..
+...||||#..
+.|.###|||.
+..#.##|||#
+|||#.##|#|
+|||###||||
+||||#|||||
+||||||||||
+||||||||||
+||||||||||
+
+After 7 minutes:
+...||||#..
+..||#|##..
+.|.####||.
+||#..##||#
+||##.##|#|
+|||####|||
+|||###||||
+||||||||||
+||||||||||
+||||||||||
+
+After 8 minutes:
+..||||##..
+..|#####..
+|||#####|.
+||#...##|#
+||##..###|
+||##.###||
+|||####|||
+||||#|||||
+||||||||||
+||||||||||
+
+After 9 minutes:
+..||###...
+.||#####..
+||##...##.
+||#....###
+|##....##|
+||##..###|
+||######||
+|||###||||
+||||||||||
+||||||||||
+
+After 10 minutes:
+.||##.....
+||###.....
+||##......
+|##.....##
+|##.....##
+|##....##|
+||##.####|
+||#####|||
+||||#|||||
+||||||||||
+
+

After 10 minutes, there are 37 wooded acres and 31 lumberyards. Multiplying the number of wooded acres by the number of lumberyards gives the total resource value after ten minutes: 37 * 31 = 1147.

+

What will the total resource value of the lumber collection area be after 10 minutes?

+
+

Your puzzle answer was 384480.

--- Part Two ---

This important natural resource will need to last for at least thousands of years. Are the Elves collecting this lumber sustainably?

+

What will the total resource value of the lumber collection area be after 1000000000 minutes?

+
+

Your puzzle answer was 177004.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/src/advent18/advent18.hs b/src/advent18/advent18.hs new file mode 100644 index 0000000..39da4c5 --- /dev/null +++ b/src/advent18/advent18.hs @@ -0,0 +1,139 @@ +{-# LANGUAGE OverloadedStrings #-} + +-- import Debug.Trace + +import Data.Text (Text) +-- import qualified Data.Text as T +import qualified Data.Text.IO as TIO + +import Data.Void (Void) + +import Text.Megaparsec +import Text.Megaparsec.Char +import qualified Text.Megaparsec.Char.Lexer as L +import qualified Control.Applicative as CA + +import Data.List +-- import qualified Data.Set as S + +import qualified Data.Map.Strict as M +import Data.Map.Strict ((!)) +-- import Data.Tuple (swap) + +type Coord = (Int, Int) -- row, col +data Cell = Open | Trees | Lumberyard deriving (Eq, Enum, Bounded, Ord) +type World = M.Map Coord Cell +type Cache = M.Map World Int + +instance Show Cell where + show Open = "." + show Trees = "|" + show Lumberyard = "#" + +main :: IO () +main = do + text <- TIO.readFile "data/advent18.txt" + let worldSpec = successfulParse text + let world = makeWorld worldSpec + -- print $ neighbours (1, 1) world + -- putStrLn $ showWorld world + -- putStrLn $ showWorld $ generation world + -- putStrLn $ showWorld $ (iterate generation world)!!10 + print $ part1 world + print $ part2 world + +part1 :: World -> Int +part1 world = score ((iterate generation world)!!10) + +part2 :: World -> Int +part2 world = score usedWorld + where (worlds, repeated) = cacheWorlds world + lastMinute = M.size worlds + prevMinute = worlds!repeated + final = 1000000000 + cycleLength = lastMinute - prevMinute + nCycles = (final - lastMinute) `div` cycleLength + usedIteration = final - (lastMinute + nCycles * cycleLength) + prevMinute + usedWorld = head $ M.keys $ M.filter (== usedIteration) worlds + + +score :: World -> Int +score world = nTrees * nLumber + where nTrees = M.size $ M.filter (== Trees) world + nLumber = M.size $ M.filter (== Lumberyard) world + +makeWorld :: [[Cell]] -> World +makeWorld rows = M.unions $ [makeWorldRow r row | (r, row) <- zip [1..] rows] + +makeWorldRow :: Int -> [Cell] -> World +makeWorldRow r row = M.fromList [((r, c), cell) | (c, cell) <- zip [1..] row] + +neighbours :: Coord -> World -> World +neighbours here world = M.filterWithKey isNeighbour world + where isNeighbour c _ = c `elem` neighbourCoords here + +neighbourCoords :: Coord -> [Coord] +neighbourCoords (r, c) = [(r', c') | r' <- [(r - 1)..(r + 1)] + , c' <- [(c - 1)..(c + 1)] + , ((r' /= r) || (c' /= c)) + ] + +showWorld world = unlines $ [showWorldRow r world | r <-[minR..maxR]] + where ((minR, _), _) = M.findMin world + ((maxR, _), _) = M.findMax world + +showWorldRow r world = concat [show (lookupCell (r, c) world) | c <- [minC..maxC]] + where ((_, minC), _) = M.findMin world + ((_, maxC), _) = M.findMax world + + +lookupCell :: Coord -> World -> Cell +lookupCell coord world = M.findWithDefault Open coord world + +generation :: World -> World +generation world = M.mapWithKey generationCell world + where generationCell here _ = propogateCell here world + +propogateCell :: Coord -> World -> Cell +propogateCell here world = propogateCell' (world!here) + where propogateCell' Open = if nTrees >= 3 then Trees else Open + propogateCell' Trees = if nLumber >= 3 then Lumberyard else Trees + propogateCell' Lumberyard = if (nLumber >= 1) && (nTrees >= 1) then Lumberyard else Open + ns = neighbours here world + nTrees = M.size $ M.filter (== Trees) ns + nLumber = M.size $ M.filter (== Lumberyard) ns + +cacheWorlds :: World -> (Cache, World) +cacheWorlds world = go (M.empty, world, 0) (drop 1 $ iterate generation world) + where go (cache, prev, minute) [] = (cache, prev) + go (cache, prev, minute) (w:ws) = + if w `M.member` cache + then (cache', w) + else go (cache', w, minute + 1) ws + where cache' = M.insert prev minute cache + +-- Parse the input file + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome (char ' ')) CA.empty CA.empty + +-- lexeme = L.lexeme sc +-- integer = lexeme L.decimal +symb = L.symbol sc + +openP = (symb "." *> pure Open) +treesP = (symb "|" *> pure Trees) +lumberP = (symb "#" *> pure Lumberyard) +cellP = openP <|> treesP <|> lumberP + +fileP = rowP `sepEndBy` (char '\n') + +rowP = many cellP + +successfulParse :: Text -> [[Cell]] +successfulParse input = + case parse fileP "input" input of + Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err + Right world -> world \ No newline at end of file -- 2.34.1