From: Neil Smith Date: Fri, 23 Dec 2022 19:58:22 +0000 (+0000) Subject: Done day 23 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=7778be60c8e0c4c95201560d4b2d1ff250c7241b;p=advent-of-code-22.git Done day 23 --- diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 1b1a610..20d4771 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -210,3 +210,18 @@ executable advent22 import: common-extensions, build-directives main-is: advent22/Main.hs build-depends: containers, linear, lens, mtl + +executable advent23 + import: common-extensions, build-directives + main-is: advent23/Main.hs + build-depends: containers, linear, lens, mtl, multiset + +executable advent23prof + import: common-extensions, build-directives + main-is: advent23/Main.hs + build-depends: containers, linear, lens, mtl, multiset + ghc-options: -O2 + -Wall + -threaded + -rtsopts "-with-rtsopts=-N -p -s -hT" + \ No newline at end of file diff --git a/advent23/Main.hs b/advent23/Main.hs new file mode 100644 index 0000000..194a8ea --- /dev/null +++ b/advent23/Main.hs @@ -0,0 +1,215 @@ +-- Writeup at https://work.njae.me.uk/2022/12/23/advent-of-code-2022-day-23/ + +-- import Debug.Trace + +import AoC +import qualified Data.Set as S +import Linear +import Control.Lens +import Data.Ix +import Data.Maybe +-- import Data.Char +import Data.Monoid +import Data.MultiSet as MS +import Control.Monad.State.Strict + + +type Position = V2 Int -- r, c + +data Direction = North | South | West | East + deriving (Show, Eq, Ord, Enum, Bounded) + +data Elf = Elf { _current :: Position, _proposed :: Position} + -- deriving (Show, Eq, Ord) + -- deriving (Eq, Ord) +makeLenses ''Elf + +instance Show Elf where + show elf = "Elf {c= " ++ (show (elf ^. current)) + ++ ", p= " ++ (show (elf ^. proposed)) + ++ " -> " ++ (show (directionOfElf elf)) + ++ "}" + +instance Eq Elf where + e1 == e2 = (_current e1) == (_current e2) + +instance Ord Elf where + e1 `compare` e2 = (_current e1) `compare` (_current e2) + +data Grove = Grove { currentGrove :: S.Set Elf, proposalDirections :: [Direction], elapsedRounds :: Int} + deriving (Eq) + +instance Show Grove where + show grove = (show $ currentGrove grove) ++ ", " ++ (show $ take 4 $ proposalDirections grove) ++ ", e = " ++ (show $ elapsedRounds grove) + +type GroveState = State Grove + + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let grove = Grove (mkGrove text) (cycle [North .. East]) 0 + -- print grove + -- print $ execState simulateOnce grove + -- print $ execState (simulateN 4) grove + print $ part1 grove + print $ part2 grove + +part1, part2 :: Grove -> Int +part1 grove = countEmpty grove' bounds + where grove' = currentGrove $ execState (simulateN 10) grove + bounds = findBounds grove' + +part2 grove = elapsedRounds grove' + where grove' = execState simulateToCompletion grove + +directionOfElf :: Elf -> Maybe Direction +directionOfElf elf + | delta == V2 0 1 = Just North + | delta == V2 0 -1 = Just South + | delta == V2 1 0 = Just East + | delta == V2 -1 0 = Just West + | otherwise = Nothing + where delta = (elf ^. proposed) ^-^ (elf ^. current) + +simulateToCompletion, simulateOnce, proposeMoves, removeClashes, moveElves, updateDirections, updateCount :: GroveState () +simulateToCompletion = + do oldGrove <- gets currentGrove + simulateOnce + newGrove <- gets currentGrove + if oldGrove == newGrove + then return () + else simulateToCompletion + +simulateN :: Int -> GroveState () +simulateN 0 = return () +simulateN n = + do simulateOnce + simulateN (n - 1) + +simulateOnce = + do proposeMoves + removeClashes + moveElves + updateDirections + updateCount + +proposeMoves = + do grove <- gets currentGrove + proposalsInf <- gets proposalDirections + let proposals = take 4 proposalsInf + let grove' = S.map (makeProposal grove proposals) grove + modify' (\g -> g { currentGrove = grove'}) + +removeClashes = + do grove <- gets currentGrove + let clashes = findClashes grove + stopClashingElves clashes + +moveElves = + do grove <- gets currentGrove + let grove' = S.map moveElf grove + modify' (\g -> g { currentGrove = grove'}) + +updateDirections = modify' (\g -> g { proposalDirections = tail (proposalDirections g)}) +updateCount = modify' (\g -> g { elapsedRounds = (elapsedRounds g) + 1}) + +-- position changing utilities + +anyNeighbour :: S.Set Position +anyNeighbour = S.fromList [ V2 dx dy + | dx <- [-1, 0, 1] + , dy <- [-1, 0, 1] + , not ((dx == 0) && (dy == 0)) + ] + +directionNeighbour :: Direction -> S.Set Position +directionNeighbour North = S.filter (\d -> d ^. _y == 1) anyNeighbour +directionNeighbour South = S.filter (\d -> d ^. _y == -1) anyNeighbour +directionNeighbour West = S.filter (\d -> d ^. _x == -1) anyNeighbour +directionNeighbour East = S.filter (\d -> d ^. _x == 1) anyNeighbour + +stepDelta ::Direction -> Position +stepDelta North = V2 0 1 +stepDelta South = V2 0 -1 +stepDelta West = V2 -1 0 +stepDelta East = V2 1 0 + +translateTo :: Position -> S.Set Position -> S.Set Position +translateTo here deltas = S.map (here ^+^) deltas + +noElves :: S.Set Elf -> S.Set Position -> Bool +noElves elves tests = S.null $ S.intersection tests $ S.map _current elves + +-- get elves to make proposals + +isolated :: S.Set Elf -> Elf -> Bool +isolated elves elf = noElves elves $ translateTo (elf ^. current) $ anyNeighbour + +nearby :: S.Set Elf -> Elf -> S.Set Elf +nearby elves elf = S.filter (\e -> (e ^. current) `S.member` nbrs) elves + where nbrs = translateTo (elf ^. current) $ anyNeighbour + +makeProposal :: S.Set Elf -> [Direction] -> Elf -> Elf +makeProposal grove directions elf + | isolated localElves elf = elf + | otherwise = fromMaybe elf $ getFirst $ mconcat $ fmap First $ fmap (proposedStep localElves elf) directions + where localElves = nearby grove elf + +proposedStep :: S.Set Elf -> Elf -> Direction -> Maybe Elf +proposedStep grove elf direction + | noElves grove interfering = Just $ elf & proposed .~ (here ^+^ (stepDelta direction)) + | otherwise = Nothing + where here = elf ^. current + interfering = translateTo here $ directionNeighbour direction + +-- find clashing elves and prevent them moving + +findClashes :: S.Set Elf -> S.Set Position +findClashes grove = MS.toSet $ MS.foldOccur ifMany MS.empty targets + where targets = MS.map _proposed $ MS.fromSet grove + ifMany t n s + | n == 1 = s + | otherwise = MS.insert t s + +stopClashingElves :: S.Set Position -> GroveState () +stopClashingElves clashes = + do grove <- gets currentGrove + let grove' = S.map (notClash clashes) grove + modify' (\g -> g { currentGrove = grove'}) + +notClash :: S.Set Position -> Elf -> Elf +notClash clashes elf + | (elf ^. proposed) `S.member` clashes = elf & proposed .~ (elf ^. current) + | otherwise = elf + +-- the elves move + +moveElf :: Elf -> Elf +moveElf elf = elf & current .~ (elf ^. proposed) + +-- part 1 solution utilities + +findBounds :: S.Set Elf -> (Position, Position) +findBounds grove = ((V2 minX minY), (V2 maxX maxY)) + where minX = fromJust $ minimumOf (folded . current . _x) grove + minY = fromJust $ minimumOf (folded . current . _y) grove + maxX = fromJust $ maximumOf (folded . current . _x) grove + maxY = fromJust $ maximumOf (folded . current . _y) grove + +countEmpty :: S.Set Elf -> (Position, Position) -> Int +countEmpty grove bounds = (rangeSize bounds) - (S.size grove) + +-- Parse the input file + +mkGrove :: String -> S.Set Elf +mkGrove text = S.fromList + [ Elf (V2 x y) (V2 x y) + | x <- [0..maxX], y <- [0..maxY] + , isElf x y + ] + where rows = reverse $ lines text + maxY = length rows - 1 + maxX = (length $ head rows) - 1 + isElf x y = ((rows !! y) !! x) == '#' diff --git a/data/advent23.txt b/data/advent23.txt new file mode 100644 index 0000000..d4871fa --- /dev/null +++ b/data/advent23.txt @@ -0,0 +1,74 @@ +###.#..#.##...###..#..#..#.####.####.###.###...##.#...#.###...#..##.....## +#..#.#######.##.#..#..#.#.#.#..#.####..##..#.......##########.####...##... +#.....#####...#####.#....####...####..#.##.#.####....#....###.####.###.#.. +#.###.#..###.#.##.#.#.###.###.#......#..###......###..##...##.##.#..#..##. +..####.#...##.####..#.......######..##.##.#.....#.###........#.#..####..#. +.#..#.......####.....#.#..##..#..##.#.#.#...##..#.#.#.#.#..#.#.#.#...###.. +##...##......#.#.#.###....##..###.#.#..#.#...#####..#.#...#.##.#..###.##.. +#.#.##.#####..#.#.#.....##....#..#.##.###.#.##.#.#..........####...#..##.. +..##.##.##..#.#.##.##....####.#.#.##.##.###.#.##..#.##..#.##..#..#...##... +##..###.#.##.#.#.....#.#.#.###..#.........#.#.#####...#....#...#..#.#..#.. +##..###..#.#####.#.#..##..#..#.......#...#.###.####..#..#..##.##.#..#...## +#.##.#.####.##..#.....####..#.##.##..#...##.#.#....###.#.########.###..##. +#.#...##.#..#.#.##..##..##.#.###.####.##......#.##.#..#.#....###.##..##.#. +###.#.##...###.##....#####.....###.#..##.#.#..#...#.####.#..#.#.#.#....### +..###.##..##..#...##.#.#.#.#####..#.##.#..#.#.#.#.#..#.#.#.####..#.###.#.# +###....#.#..#..#.##..#####.###.###....##.#.##...##..####.#.#..#....###.### +.###.##.##..##...#..#.#####.#.###.#....#.#..##..#.####.#..#..#.###....##.. +##..###.#...#.....##..##.##.#..#....##.##...##.#....#...###.###.#..#..#### +##....##..#.#.....#.#.....######.##.##..#.#......#..#####.#..##...###.#### +..#...#...#.####...####..#.##..#.#...####.##..#.#####.#....#..##..##.#.#.. +.##.###..####.#.#...#.....#.#...#.#...#.#.#..##.##....#.####...#.##.###... +.######....######.#.###.#.#.#.#.#...###.#.#..###....#.#.##..##..#.##..##.. +.#.#...##.#.#.###....###..#..#...##..#...##...#..##...##.#...#.#......#### +.####.#####.###.##..###.##.#.#.#....#...#.#.#..#....#.........#......#..#. +######.#####.#.#...#.##.#.#.#####..#.......####..#.###..#...#...#...##...# +......###.##.##..#...###.#.#.##.##.##.#.###.#..#.#######.##.###.#....##.## +.###..#.##.#.##.#...#..#..#....####..###.##.##.#.#...#...###.#.#.....##.## +##.#....###...####.####.#.#..##.###.#.#..#..##..##...#...#....###...#.#... +..##.###..#..###...#.#.##.#####.#..##..#.#.####.#.#.#...#..#.##....##.#... +#...#.#.####.##.#.###.#.##.####..#.#..#..#..####...####.####..#.###.##..#. +.##.#..#.##...##..#..#...####...#.##.####..#..#.##.##.##.###.#.#...#.#.##. +##.###..#.....##..#.####.#..#.##.##..##...#####.#.#.#.#...#.#.####.######. +#.#.#....#..#..#..##.#.....##...####..#.###.#...##.#...######.#.##...#...# +..##.####....#..#.##..##.##.#.##..#######.#....#.#..##..####.##.##..#.##.# +.#...#..#....#.#.#####...####....#.#####..##.#.##........#.##.....#####.## +#.##.##.###.#..#.#.#.##.###...####.#.##...#..##.#.######..##.##....#.#.... +.#..####.#..##.#.#..#....#....####...####.##......#.###.###.....####..#... +###....#...##.#######..#.####.#.#.#.#.###..#..##.....#.#####.#######..##.. +#.#......#.#.#######.#..#..##.#.#####.######.##.#.####..#.#.....#####.#.#. +.#.##..####..#.#..#.#..#.###.....##.#.###.#....####.###.#.##.#.#.#...##.#. +#.#.......#####..###########.#..#####.##..##.#.#..#########.####.###.#.### +##..#.##..###....#..#..##...#.##.###.#.....#.#.#.#.#......####...##..#.#.. +...#.#####.#.###....##..#.#..##.#..##.##.#.##.##.###...##...#.#.#.#.#..#.. +.###...###...#.#.#.#.#..##....#...#....#..####...#..###.##.##.##.##...#... +#...##...##..###..###..##.#....#########.#...#.#..#..#..##......#.#..#...# +#.#.##.....#.....#.....#.###........#####.#...##.....#...##.#....##..#.... +.......#....###.#.###.......#.#.....#...#.##....#.#.#...####.#####.###..#. +.#####.#.....###.....####....#.##.#.##...##...##..##.##.#.#....#...#..###. +..#.....##...#.#.##...####..#.#.#.###.##..#.#..##.#....#.#.#####.#.##.#.#. +#######.#####..#.#.#.#...##.#....##...###....#...####...#...####.#.####... +.##.##.#.####.#.#.##.#.###...#.#...##.#.##..###.##......#.....##.###...... +#.#.#.....#..#.##..#.#.#.#######...#.##.#..##.#..##...#..#.#.#..#.##..#### +####.##..#.#####...#######.##.#...#....####...###..##.#...####.##..##....# +.##..#..#..####..#..######.#.#.#..#.#.#..#..#####..##..#..#####.#.##...##. +###.##.#..#.####.#......#..#.###.#.#......#.#.##.##.#.#.##...#..#####..##. +.#.##.#.##..#.###..#.##.###..#..#...####..##...#...#.###....#.#..#....##.# +#...##.##..#..##.###.##.####.##.#.###..#.##..#.####.##..#.##.....##..#.### +...###...#.#..##.#.######.##...###.#.#...#.#..#.#.#....##...###.....##.#.# +###....###.####..#.#..####.#.###.#..##...#...#.#.#.#######.#.##.##.###.#.. +#.##...##...#####....##.##.##..#####.##.#.####.#.#.##.#..........#.#.##.## +#.##.#.##.###...##.########.##.###.##....#####.######.##.#####.#.#...##... +#.#.#..#.#....#####.#..##..#####...###..#...##.###..####.##............#.# +###...#.#....##.##.###...#########..###.#.######......#..###..#..#.#....## +#.##...#.#####..#.#.#...##.#...#...#....##########.#..#.#.....#..##..#.#.# +##...##.####.#..#.#####.#.#..#..##.#.###..#.#.....#..##.##...##..##.#.#.## +.##.###.....#.....#.##...##.#...#..####.###............####.###.#.#....#.. +.#.#.####...#...#.#.###...##.#...#..#.##.......#.....##...##.##.#.#.####.. +#..#..#.##.#.#.###...#...##.#.###..##..#####...#..###.##.###.#.#...#...#.# +.###.#..#.#..####...#...###.#..#......#.#..#..#.###......#.##.########...# +###.####...#.#.##...##..###..##..#.#.##.#.#.#.####.#..##..#...####..#####. +##.....##..#.#.##...##....#.#.####..#.###.##.###......#....#.#.#..##.#.#.# +####..####....#...#.##.##.#.#..#####.#####...##.#..#..#.#...###..#....#.#. +...##.#..##.#...#####.#...#.###....#...#.#....##....#...#.##..#......#.#.# +.#.###.#...###.####..#..##.##..#.##........###.#...#...##..#...###..###.#. diff --git a/data/advent23a.txt b/data/advent23a.txt new file mode 100644 index 0000000..e2a080c --- /dev/null +++ b/data/advent23a.txt @@ -0,0 +1,6 @@ +..... +..##. +..#.. +..... +..##. +..... \ No newline at end of file diff --git a/data/advent23b.txt b/data/advent23b.txt new file mode 100644 index 0000000..7ac3ba9 --- /dev/null +++ b/data/advent23b.txt @@ -0,0 +1,7 @@ +....#.. +..###.# +#...#.# +.#...## +#.###.. +##.#.## +.#..#.. \ No newline at end of file diff --git a/problems/day23.html b/problems/day23.html new file mode 100644 index 0000000..ea44d3d --- /dev/null +++ b/problems/day23.html @@ -0,0 +1,315 @@ + + + + +Day 23 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 46*

       Î»y.2022

+ + + +
+

--- Day 23: Unstable Diffusion ---

You enter a large crater of gray dirt where the grove is supposed to be. All around you, plants you imagine were expected to be full of fruit are instead withered and broken. A large group of Elves has formed in the middle of the grove.

+

"...but this volcano has been dormant for months. Without ash, the fruit can't grow!"

+

You look up to see a massive, snow-capped mountain towering above you.

+

"It's not like there are other active volcanoes here; we've looked everywhere."

+

"But our scanners show active magma flows; clearly it's going somewhere."

+

They finally notice you at the edge of the grove, your pack almost overflowing from the random star fruit you've been collecting. Behind you, elephants and monkeys explore the grove, looking concerned. Then, the Elves recognize the ash cloud slowly spreading above your recent detour.

+

"Why do you--" "How is--" "Did you just--"

+

Before any of them can form a complete question, another Elf speaks up: "Okay, new plan. We have almost enough fruit already, and ash from the plume should spread here eventually. If we quickly plant new seedlings now, we can still make it to the extraction point. Spread out!"

+

The Elves each reach into their pack and pull out a tiny plant. The plants rely on important nutrients from the ash, so they can't be planted too close together.

+

There isn't enough time to let the Elves figure out where to plant the seedlings themselves; you quickly scan the grove (your puzzle input) and note their positions.

+

For example:

+
....#..
+..###.#
+#...#.#
+.#...##
+#.###..
+##.#.##
+.#..#..
+
+

The scan shows Elves # and empty ground .; outside your scan, more empty ground extends a long way in every direction. The scan is oriented so that north is up; orthogonal directions are written N (north), S (south), W (west), and E (east), while diagonal directions are written NE, NW, SE, SW.

+

The Elves follow a time-consuming process to figure out where they should each go; you can speed up this process considerably. The process consists of some number of rounds during which Elves alternate between considering where to move and actually moving.

+

During the first half of each round, each Elf considers the eight positions adjacent to themself. If no other Elves are in one of those eight positions, the Elf does not do anything during this round. Otherwise, the Elf looks in each of four directions in the following order and proposes moving one step in the first valid direction:

+
    +
  • If there is no Elf in the N, NE, or NW adjacent positions, the Elf proposes moving north one step.
  • +
  • If there is no Elf in the S, SE, or SW adjacent positions, the Elf proposes moving south one step.
  • +
  • If there is no Elf in the W, NW, or SW adjacent positions, the Elf proposes moving west one step.
  • +
  • If there is no Elf in the E, NE, or SE adjacent positions, the Elf proposes moving east one step.
  • +
+

After each Elf has had a chance to propose a move, the second half of the round can begin. Simultaneously, each Elf moves to their proposed destination tile if they were the only Elf to propose moving to that position. If two or more Elves propose moving to the same position, none of those Elves move.

+

Finally, at the end of the round, the first direction the Elves considered is moved to the end of the list of directions. For example, during the second round, the Elves would try proposing a move to the south first, then west, then east, then north. On the third round, the Elves would first consider west, then east, then north, then south.

+

As a smaller example, consider just these five Elves:

+
.....
+..##.
+..#..
+.....
+..##.
+.....
+
+

The northernmost two Elves and southernmost two Elves all propose moving north, while the middle Elf cannot move north and proposes moving south. The middle Elf proposes the same destination as the southwest Elf, so neither of them move, but the other three do:

+
..##.
+.....
+..#..
+...#.
+..#..
+.....
+
+

Next, the northernmost two Elves and the southernmost Elf all propose moving south. Of the remaining middle two Elves, the west one cannot move south and proposes moving west, while the east one cannot move south or west and proposes moving east. All five Elves succeed in moving to their proposed positions:

+
.....
+..##.
+.#...
+....#
+.....
+..#..
+
+

Finally, the southernmost two Elves choose not to move at all. Of the remaining three Elves, the west one proposes moving west, the east one proposes moving east, and the middle one proposes moving north; all three succeed in moving:

+
..#..
+....#
+#....
+....#
+.....
+..#..
+
+

At this point, no Elves need to move, and so the process ends.

+

The larger example above proceeds as follows:

+
== Initial State ==
+..............
+..............
+.......#......
+.....###.#....
+...#...#.#....
+....#...##....
+...#.###......
+...##.#.##....
+....#..#......
+..............
+..............
+..............
+
+== End of Round 1 ==
+..............
+.......#......
+.....#...#....
+...#..#.#.....
+.......#..#...
+....#.#.##....
+..#..#.#......
+..#.#.#.##....
+..............
+....#..#......
+..............
+..............
+
+== End of Round 2 ==
+..............
+.......#......
+....#.....#...
+...#..#.#.....
+.......#...#..
+...#..#.#.....
+.#...#.#.#....
+..............
+..#.#.#.##....
+....#..#......
+..............
+..............
+
+== End of Round 3 ==
+..............
+.......#......
+.....#....#...
+..#..#...#....
+.......#...#..
+...#..#.#.....
+.#..#.....#...
+.......##.....
+..##.#....#...
+...#..........
+.......#......
+..............
+
+== End of Round 4 ==
+..............
+.......#......
+......#....#..
+..#...##......
+...#.....#.#..
+.........#....
+.#...###..#...
+..#......#....
+....##....#...
+....#.........
+.......#......
+..............
+
+== End of Round 5 ==
+.......#......
+..............
+..#..#.....#..
+.........#....
+......##...#..
+.#.#.####.....
+...........#..
+....##..#.....
+..#...........
+..........#...
+....#..#......
+..............
+
+

After a few more rounds...

+
== End of Round 10 ==
+.......#......
+...........#..
+..#.#..#......
+......#.......
+...#.....#..#.
+.#......##....
+.....##.......
+..#........#..
+....#.#..#....
+..............
+....#..#..#...
+..............
+
+

To make sure they're on the right track, the Elves like to check after round 10 that they're making good progress toward covering enough ground. To do this, count the number of empty ground tiles contained by the smallest rectangle that contains every Elf. (The edges of the rectangle should be aligned to the N/S/E/W directions; the Elves do not have the patience to calculate arbitrary rectangles.) In the above example, that rectangle is:

+
......#.....
+..........#.
+.#.#..#.....
+.....#......
+..#.....#..#
+#......##...
+....##......
+.#........#.
+...#.#..#...
+............
+...#..#..#..
+
+

In this region, the number of empty ground tiles is 110.

+

Simulate the Elves' process and find the smallest rectangle that contains the Elves after 10 rounds. How many empty ground tiles does that rectangle contain?

+
+

Your puzzle answer was 4236.

--- Part Two ---

It seems you're on the right track. Finish simulating the process and figure out where the Elves need to go. How many rounds did you save them?

+

In the example above, the first round where no Elf moved was round 20:

+
.......#......
+....#......#..
+..#.....#.....
+......#.......
+...#....#.#..#
+#.............
+....#.....#...
+..#.....#.....
+....#.#....#..
+.........#....
+....#......#..
+.......#......
+
+

Figure out where the Elves need to go. What is the number of the first round where no Elf moves?

+
+

Your puzzle answer was 1023.

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