From a3175189f99c76ed782189826efdde2fbadfd708 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 18 Dec 2020 12:00:29 +0000 Subject: [PATCH] Done day 11 --- advent11/package.yaml | 68 ++++++++ advent11/src/advent11.hs | 131 ++++++++++++++ advent11/src/advent11naive.hs | 101 +++++++++++ data/advent11.txt | 95 +++++++++++ data/advent11a.txt | 10 ++ problems/day11.html | 309 ++++++++++++++++++++++++++++++++++ stack.yaml | 1 + 7 files changed, 715 insertions(+) create mode 100644 advent11/package.yaml create mode 100644 advent11/src/advent11.hs create mode 100644 advent11/src/advent11naive.hs create mode 100644 data/advent11.txt create mode 100644 data/advent11a.txt create mode 100644 problems/day11.html diff --git a/advent11/package.yaml b/advent11/package.yaml new file mode 100644 index 0000000..17440a8 --- /dev/null +++ b/advent11/package.yaml @@ -0,0 +1,68 @@ +# This YAML file describes your package. Stack will automatically generate a +# Cabal file when you run `stack build`. See the hpack website for help with +# this file: . + +name: advent11 +synopsis: Advent of Code +version: '0.0.1' + +default-extensions: +- AllowAmbiguousTypes +- ApplicativeDo +- BangPatterns +- BlockArguments +- DataKinds +- DeriveFoldable +- DeriveFunctor +- DeriveGeneric +- DeriveTraversable +- EmptyCase +- FlexibleContexts +- FlexibleInstances +- FunctionalDependencies +- GADTs +- GeneralizedNewtypeDeriving +- ImplicitParams +- KindSignatures +- LambdaCase +- MonadComprehensions +- MonoLocalBinds +- MultiParamTypeClasses +- MultiWayIf +- NamedFieldPuns +- NegativeLiterals +- NumDecimals +# - OverloadedLists +- OverloadedStrings +- PartialTypeSignatures +- PatternGuards +- PatternSynonyms +- PolyKinds +- RankNTypes +- RecordWildCards +- ScopedTypeVariables +- TemplateHaskell +- TransformListComp +- TupleSections +- TypeApplications +- TypeFamilies +- TypeInType +- TypeOperators +- ViewPatterns + +executables: + advent11: + main: advent11.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - containers + - mtl + - monad-loops + + advent11naive: + main: advent11naive.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - containers diff --git a/advent11/src/advent11.hs b/advent11/src/advent11.hs new file mode 100644 index 0000000..b56dc3d --- /dev/null +++ b/advent11/src/advent11.hs @@ -0,0 +1,131 @@ +-- import Debug.Trace + +import Prelude hiding (Left, Right) + +import qualified Data.Map as M +import Data.Map ((!)) +import qualified Data.Set as S +-- import Data.Sort +import Data.List +import Control.Monad.Reader +import Control.Monad.Loops + + +type Position = (Int, Int) +data Seat = Floor | Empty | Occupied deriving (Eq, Ord) +data Direction = Up | UpRight | Right | DownRight | Down | DownLeft | Left | UpLeft + deriving (Eq, Ord, Show, Enum) +type Seats = M.Map Position Seat + +type Neighbourhood = M.Map Position (S.Set Position) +type Rule = Seats -> Neighbourhood -> Position -> Seat -> Seat + +instance Show Seat where + show Floor = "." + show Empty = "L" + show Occupied = "#" + + +type CachedSeats a = Reader (Neighbourhood, Rule) a + + +main :: IO () +main = + do text <- readFile "data/advent11.txt" + let (seats, maxCorner) = readGrid text + -- print $ M.size seats + -- print maxCorner + print $ part1 seats + print $ part2 seats + + +part1 seats = M.size $ M.filter (== Occupied) stableSeats + where cachedNeighbours = allNeighbourhoods seats + env = (cachedNeighbours, ruleA) + stableSeats = snd $ runReader (runSteps seats) env + +part2 seats = M.size $ M.filter (== Occupied) stableSeats + where cachedNeighbours = allSightNeighbourhoods seats + env = (cachedNeighbours, ruleB) + stableSeats = snd $ runReader (runSteps seats) env + + +runSteps :: Seats -> CachedSeats (Seats, Seats) +runSteps seats = iterateUntilM (uncurry (==)) seatChanges (M.empty, seats) + +seatChanges :: (Seats, Seats) -> CachedSeats (Seats, Seats) +seatChanges (_, seats0) = + do seats <- step seats0 + return (seats0, seats) + +step :: Seats -> CachedSeats Seats +step seats = + do (nbrs, rule) <- ask + return $ M.mapWithKey (rule seats nbrs) seats + +ruleA :: Seats -> Neighbourhood -> Position -> Seat -> Seat +ruleA seats nbrs here thisSeat + | thisSeat == Empty && nOccs == 0 = Occupied + | thisSeat == Occupied && nOccs >= 4 = Empty + | otherwise = thisSeat + where nOccs = M.size $ occupiedNeighbours seats nbrs here + +ruleB :: Seats -> Neighbourhood -> Position -> Seat -> Seat +ruleB seats nbrs here thisSeat + | thisSeat == Empty && nOccs == 0 = Occupied + | thisSeat == Occupied && nOccs >= 5 = Empty + | otherwise = thisSeat + where nOccs = M.size $ occupiedNeighbours seats nbrs here + + +neighbours (r, c) = S.delete (r, c) $ S.fromList [(r + dr, c + dc) | dr <- [-1, 0, 1], dc <- [-1, 0, 1]] + +neighbourhood seats here = S.intersection (M.keysSet seats) (neighbours here) + +allNeighbourhoods :: Seats -> Neighbourhood +allNeighbourhoods seats = M.mapWithKey (\h _ -> neighbourhood seats h) seats + +occupiedNeighbours seats nbrs here = M.filter (== Occupied) $ M.restrictKeys seats (nbrs!here) + + +onSightLine :: Position -> Direction -> Position -> Bool +onSightLine (r0, c0) Down (r, c) = (c0 == c) && (r > r0) +onSightLine (r0, c0) Up (r, c) = (c0 == c) && (r < r0) +onSightLine (r0, c0) Right (r, c) = (r0 == r) && (c > c0) +onSightLine (r0, c0) Left (r, c) = (r0 == r) && (c < c0) +onSightLine (r0, c0) DownRight (r, c) = ((r - r0) > 0) && ((r - r0) == (c - c0)) +onSightLine (r0, c0) UpLeft (r, c) = ((r - r0) < 0) && ((r - r0) == (c - c0)) +onSightLine (r0, c0) DownLeft (r, c) = ((r - r0) > 0) && ((r - r0) == (c0 - c)) +onSightLine (r0, c0) UpRight (r, c) = ((r - r0) < 0) && ((r - r0) == (c0 - c)) + +manhattan (r1, c1) (r2, c2) = abs (r1 - r2) + abs (c1 - c2) + +closestInDirection seats here direction = take 1 sortedSeats + -- where seatsInDirection = M.keys $ M.filterWithKey (\o _ -> onSightLine here direction o) seats + where seatsInDirection = filter (onSightLine here direction) $ M.keys seats + sortedSeats = sortOn (manhattan here) seatsInDirection + +closestInSight :: Seats -> Position -> (S.Set Position) +closestInSight seats here = S.fromList $ concatMap (closestInDirection seats here) [d | d <- [Up .. UpLeft]] + +allSightNeighbourhoods :: Seats -> Neighbourhood +allSightNeighbourhoods seats = M.mapWithKey (\h _ -> closestInSight seats h) seats + +-- occupiedInSight :: Seats -> Position -> Seats +-- occupiedInSight seats here = M.filter (== Occupied) $ M.restrictKeys seats $ closestInSight seats here + + + +readGrid :: String -> (Seats, Position) +readGrid input = (seats, (maxR, maxC)) + where seats = M.fromList $ concat + [ [((r, c), Empty) | (t, c) <- zip row [0..], t == 'L'] + | (row, r) <- zip rows [0..] ] + rows = lines input + maxC = (length $ head rows) - 1 + maxR = (length rows) - 1 + +showGrid seats (maxR, maxC) = + unlines $ [ concat [showSeat (r, c) | c <- [0..maxC] ] | r <- [0..maxR]] + where showSeat here = show $ M.findWithDefault Floor here seats + diff --git a/advent11/src/advent11naive.hs b/advent11/src/advent11naive.hs new file mode 100644 index 0000000..ac6d260 --- /dev/null +++ b/advent11/src/advent11naive.hs @@ -0,0 +1,101 @@ +-- import Debug.Trace + +import Prelude hiding (Left, Right) + +import qualified Data.Map as M +import Data.Map ((!)) +import qualified Data.Set as S +-- import Data.Sort +import Data.List + +type Position = (Int, Int) +data Seat = Floor | Empty | Occupied deriving (Eq, Ord) +data Direction = Up | UpRight | Right | DownRight | Down | DownLeft | Left | UpLeft + deriving (Eq, Ord, Show, Enum) +type Seats = M.Map Position Seat + +instance Show Seat where + show Floor = "." + show Empty = "L" + show Occupied = "#" + + +main :: IO () +main = + do text <- readFile "data/advent11.txt" + let (seats, maxCorner) = readGrid text + print $ M.size seats + print maxCorner + print $ part1 seats + print $ part2 seats + -- print $ part2 trees maxCorner + + +part1 seats = M.size $ M.filter (== Occupied) $ runUntilSame ruleA seats +part2 seats = M.size $ M.filter (== Occupied) $ runUntilSame ruleB seats + +step rule seats = M.mapWithKey (rule seats) seats + +runSteps rule seats = iterate (step rule) seats + +seatDifferences rule seats = zip (runSteps rule seats) (tail $ runSteps rule seats) + +runUntilSame rule seats = fst $ head $ dropWhile (uncurry (/=)) $ seatDifferences rule seats + + +ruleA seats here thisSeat + | thisSeat == Empty && nOccs == 0 = Occupied + | thisSeat == Occupied && nOccs >= 4 = Empty + | otherwise = thisSeat + where nOccs = M.size $ occupiedNeighbours seats here + +ruleB seats here thisSeat + | thisSeat == Empty && nOccs == 0 = Occupied + | thisSeat == Occupied && nOccs >= 5 = Empty + | otherwise = thisSeat + where nOccs = M.size $ occupiedInSight seats here + + +neighbours (r, c) = S.delete (r, c) $ S.fromList [(r + dr, c + dc) | dr <- [-1, 0, 1], dc <- [-1, 0, 1]] + +neighbourhood seats here = M.restrictKeys seats (neighbours here) +occupiedNeighbours seats here = M.filter (== Occupied) $ neighbourhood seats here + + +onSightLine :: Position -> Direction -> Position -> Bool +onSightLine (r0, c0) Down (r, c) = (c0 == c) && (r > r0) +onSightLine (r0, c0) Up (r, c) = (c0 == c) && (r < r0) +onSightLine (r0, c0) Right (r, c) = (r0 == r) && (c > c0) +onSightLine (r0, c0) Left (r, c) = (r0 == r) && (c < c0) +onSightLine (r0, c0) DownRight (r, c) = ((r - r0) > 0) && ((r - r0) == (c - c0)) +onSightLine (r0, c0) UpLeft (r, c) = ((r - r0) < 0) && ((r - r0) == (c - c0)) +onSightLine (r0, c0) DownLeft (r, c) = ((r - r0) > 0) && ((r - r0) == (c0 - c)) +onSightLine (r0, c0) UpRight (r, c) = ((r - r0) < 0) && ((r - r0) == (c0 - c)) + +manhattan (r1, c1) (r2, c2) = abs (r1 - r2) + abs (c1 - c2) + +closestInDirection seats here direction = take 1 sortedSeats + -- where seatsInDirection = M.keys $ M.filterWithKey (\o _ -> onSightLine here direction o) seats + where seatsInDirection = filter (onSightLine here direction) $ M.keys seats + sortedSeats = sortOn (manhattan here) seatsInDirection + +closestInSight :: Seats -> Position -> (S.Set Position) +closestInSight seats here = S.fromList $ concatMap (closestInDirection seats here) [d | d <- [Up .. UpLeft]] + +occupiedInSight :: Seats -> Position -> Seats +occupiedInSight seats here = M.filter (== Occupied) $ M.restrictKeys seats $ closestInSight seats here + + +readGrid :: String -> (Seats, Position) +readGrid input = (seats, (maxR, maxC)) + where seats = M.fromList $ concat + [ [((r, c), Empty) | (t, c) <- zip row [0..], t == 'L'] + | (row, r) <- zip rows [0..] ] + rows = lines input + maxC = (length $ head rows) - 1 + maxR = (length rows) - 1 + +showGrid seats (maxR, maxC) = + unlines $ [ concat [showSeat (r, c) | c <- [0..maxC] ] | r <- [0..maxR]] + where showSeat here = show $ M.findWithDefault Floor here seats + diff --git a/data/advent11.txt b/data/advent11.txt new file mode 100644 index 0000000..57f9323 --- /dev/null +++ b/data/advent11.txt @@ -0,0 +1,95 @@ +.LLLL.L.LLLL.LL.LLL.L.LLLLLLL.LLLLL.LLLLLLLLLLLLL.LLLL.LLL.LLLLL.LLLLLLLL.LLLLL.LLLLLLL.LLLLL +LLLLLLL.LLLLLLL.LLLLLLLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLL.LLL..LLLL.LLLLLLLL.LLLLL.LLLLLLL.LLLLL +LLLLLLLLLLLL.LL.LLLLL.LLLL.L.LLLL.LLLLLL.L.LLL.LL..LLL.LLLLLLLLLLLLLLLLLL.L.LLL.LLLLLLL.LLLLL +LLLLLLLLLLLLLLL.LLLLL.LLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLL.LLLLLLLLL.LLLLLLLL.LLLLL.LLLLLLL.L.LLL +LL.LLLL.LLLLLLL.L.LLL.LLLLLL.L.LL.LLLLLLLL.LLLLLLLLL.LLLL.LLLLLL.LLLLLLLL.LL.LL.LLLLLLL.LLLLL +LLLLLLL.LLLLLLL.LLLLL.LLLLLLL.LLL.LLLLL.LLLLLLLLL.LLLL.LLLLLLLLL.LLLLLLLL.LLLL.L.LLLLLLLLLLLL +LLLLLLL.LLLLLLL.LLLLLLLLLLLL.LL.L.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL.LLLL. +LLLLLLLLLLLLLLL.LLLLLLLLLLL..LLLL.LLLLLLLL.LLLLLL.LL.L.LLLLL.LLL.LLLLLLLLLLLLL.LLLLLLL.L.LLLL +....L..L.....L......LL...L.LL.......L...L.L......L.L.LLL....L..L....L.LL..LLL....LL..L..L...L +LLLLLLLLLLLLL.L.LLLLLLLLLLLL.LLLLLL.LLLLLL.LLLLLL.LLLLLLLLLLLLLL.LLLL.LLL.LLLLL.LLLLLLLLLLLL. +LLLLLLL.LLLLLLL.LLLLLLLLLL.L.LLLL.LLLLL.LL.LLLLLL.LLLLLLLLLLLLLL.LLLLLLLL.LLLLL.LLLLLLLLLLLLL +LLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLL.LLLLLLLLLLLLLLLLLLLLL.LLLLLLLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLL +LLLLLLL.LLLLLLLLLLLLL.LLLLLL.LLLL.LLLLL.LL.LL.LLLLLLLL.LLLLLLLLLLLLLLLLLL..LLLL.LLLLLLLLLLLLL +LLLL.LL.LLLLLLLLLLLLL.LLLLLL.LLLL.LLLLLLLL.LLLLLL.LLLLLLLLLLLLLL.LLLLLLLL.LLLLL.LLLLLLL.L.LLL +LLLLLLL..LLLLLL.LLLLL..LLLLL.LLLL.LLLLLLLLLLL.LLL.LLL..LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLL..LLLL +...L...L..LL....L.......L...L.......LL........LL.....LL....L.....LL............L..LL....L.... +LLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLL..LLLLLLLL.LLLLLLLL.LLL.L.LLLLLLLLLLLLL +LLLLLLL.LLLLLLL.LLLLLLLLLLLL.L.LLLLLLLLLLL.LLLLL.LLLLLLL..LLLLLL.LLLLLL.LLLLLLLLLLLLL.L.LLLLL +LLLLLLL.LLLLLLLLLL.L..LLLLLLLLLLL.L.L.LLLLLLLLLLL.LLLL.LLLLL.LLL.LLLLLLLL.LLLLLLLLLLLLL.LLLLL +LLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLL.LLLLLLLLLLLLL.L.LLLL.LLLLLLLLL.LLL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LL +LLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLLL.LLLLLLLL.LLLLLLLLLLL.LLLLL.LLLLL.LLLLLL.LL.LL.LLLLLLLLLLLLL +..L....L.L.LL.L.........L...LL.L....LL......LL..LL.L.L.....L..L..L..L..LL.L...L..L....L...... +LLLLLLL.LLLLLLL.LLLLL.LLLLLL.LLL..LLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLL.LL.LLLLL.LLLLLLL.LLLLL +LLLLLLL.L.LLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL..LLLLLLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLL +LLLLLLL.LLLLLLL.LLLLL.L.LLLL.L.LLLLLLLLLLL..LLLLLLLLLL.LL.LLLLLLLLLLLLLLL.LLLLL.LLLLLLL.LLLLL +LLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLLLLLLLLLLL.LLLLLL.LLLL.LLLLLL.LL.LLLLLLLL..L.LL.LLLLLLLLLLLLL +.L......L....L.L.L...LLLL......LL...LL...L...L......L..L...........L......L.......L....L.L... +LL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLL.LL.LLLLL.LLLLLLLLLLL.LLL.LLLLL.LLLLLLLL.LLLLL.LL.LLLL.LLLLL +L.LLLLL.LLLLLLL.LLLLL.LLLLLL.LL.LLLLLLLLLL.L.LLLLLLLLL.LLLLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLLLL +LLLLLLL.LLLLLLL.LLLLL.LLLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLLLLLLLLLLL.LLL.LLLL.LLLLLLLLLLLLL.LLLLL +LLLLLLLLLLLLLLLLL.LLLLLLLLLL.LLLL.LLLLLLLLLLLLLLL.L.LLLLLLLLLLLL.LLLLL.LLLLLLLL.LLLLLLL.LLLLL +.LLLLLL.LLLLLLL.LLLLLLLLLLLL.LLLL.LLLLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLL.L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LLLL.LLLLLLLLLL.L.L.LLLLLLLL.LLLLL.LLLLLLL.L.LL. +LL.LL.L.LLLLLLLLLLLLL.L.LLLLLLLLLLLLLLLLLL.LLLLLL..LLL.LLLLLLLLLLLLLLLLL..LLLLL.LLLLLLL.LLLLL +LLLLLLLLLLLLLLL.LLL.LL.LLLLL.LLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLLLLLLLL.LL.L.LLLLLLLLLLLLL +LLLLLLL.LLLL.LL.LLLLL.LLLLLL.LLLL.L.LLLLLLLLLLLLLLLLLLLLLLLLLLL..LLLLLLLLL.LLLLLLLLLLLL.LLLLL +L.LLLLL.LLLLLLL.LLLLL.LLLLLLLLLLL.LLLLLLLL.LLLLLL.LLLL.LLLLLLLLLLLLLLLL.L.LLLLLLLLLLLLL..LLLL +LLLLLLL.LLLLLLLL.LLLLLLLLLLL.LLLLLLLLL.LLL.LLLLLLLLLL..LL.LLLLLL.LLLLLLLL.LLLLL.LLLLLLLLLLLLL \ No newline at end of file diff --git a/data/advent11a.txt b/data/advent11a.txt new file mode 100644 index 0000000..ff5431a --- /dev/null +++ b/data/advent11a.txt @@ -0,0 +1,10 @@ +L.LL.LL.LL +LLLLLLL.LL +L.L.L..L.. +LLLL.LL.LL +L.LL.LL.LL +L.LLLLL.LL +..L.L..... +LLLLLLLLLL +L.LLLLLL.L +L.LLLLL.LL \ No newline at end of file diff --git a/problems/day11.html b/problems/day11.html new file mode 100644 index 0000000..b0f30af --- /dev/null +++ b/problems/day11.html @@ -0,0 +1,309 @@ + + + + +Day 11 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 22*

   int y=2020;

+ + + +
+ +

--- Day 11: Seating System ---

Your plane lands with plenty of time to spare. The final leg of your journey is a ferry that goes directly to the tropical island where you can finally start your vacation. As you reach the waiting area to board the ferry, you realize you're so early, nobody else has even arrived yet!

+

By modeling the process people use to choose (or abandon) their seat in the waiting area, you're pretty sure you can predict the best place to sit. You make a quick map of the seat layout (your puzzle input).

+

The seat layout fits neatly on a grid. Each position is either floor (.), an empty seat (L), or an occupied seat (#). For example, the initial seat layout might look like this:

+
L.LL.LL.LL
+LLLLLLL.LL
+L.L.L..L..
+LLLL.LL.LL
+L.LL.LL.LL
+L.LLLLL.LL
+..L.L.....
+LLLLLLLLLL
+L.LLLLLL.L
+L.LLLLL.LL
+
+

Now, you just need to model the people who will be arriving shortly. Fortunately, people are entirely predictable and always follow a simple set of rules. All decisions are based on the number of occupied seats adjacent to a given seat (one of the eight positions immediately up, down, left, right, or diagonal from the seat). The following rules are applied to every seat simultaneously:

+
    +
  • If a seat is empty (L) and there are no occupied seats adjacent to it, the seat becomes occupied.
  • +
  • If a seat is occupied (#) and four or more seats adjacent to it are also occupied, the seat becomes empty.
  • +
  • Otherwise, the seat's state does not change.
  • +
+

Floor (.) never changes; seats don't move, and nobody sits on the floor.

+

After one round of these rules, every seat in the example layout becomes occupied:

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

After a second round, the seats with four or more occupied adjacent seats become empty again:

+
#.LL.L#.##
+#LLLLLL.L#
+L.L.L..L..
+#LLL.LL.L#
+#.LL.LL.LL
+#.LLLL#.##
+..L.L.....
+#LLLLLLLL#
+#.LLLLLL.L
+#.#LLLL.##
+
+

This process continues for three more rounds:

+
#.##.L#.##
+#L###LL.L#
+L.#.#..#..
+#L##.##.L#
+#.##.LL.LL
+#.###L#.##
+..#.#.....
+#L######L#
+#.LL###L.L
+#.#L###.##
+
+
#.#L.L#.##
+#LLL#LL.L#
+L.L.L..#..
+#LLL.##.L#
+#.LL.LL.LL
+#.LL#L#.##
+..L.L.....
+#L#LLLL#L#
+#.LLLLLL.L
+#.#L#L#.##
+
+
#.#L.L#.##
+#LLL#LL.L#
+L.#.L..#..
+#L##.##.L#
+#.#L.LL.LL
+#.#L#L#.##
+..L.L.....
+#L#L##L#L#
+#.LLLLLL.L
+#.#L#L#.##
+
+

At this point, something interesting happens: the chaos stabilizes and further applications of these rules cause no seats to change state! Once people stop moving around, you count 37 occupied seats.

+

Simulate your seating area by applying the seating rules repeatedly until no seats change state. How many seats end up occupied?

+
+

Your puzzle answer was 2270.

--- Part Two ---

As soon as people start to arrive, you realize your mistake. People don't just care about adjacent seats - they care about the first seat they can see in each of those eight directions!

+

Now, instead of considering just the eight immediately adjacent seats, consider the first seat in each of those eight directions. For example, the empty seat below would see eight occupied seats:

+
.......#.
+...#.....
+.#.......
+.........
+..#L....#
+....#....
+.........
+#........
+...#.....
+
+

The leftmost empty seat below would only see one empty seat, but cannot see any of the occupied ones:

+
.............
+.L.L.#.#.#.#.
+.............
+
+

The empty seat below would see no occupied seats:

+
.##.##.
+#.#.#.#
+##...##
+...L...
+##...##
+#.#.#.#
+.##.##.
+
+

Also, people seem to be more tolerant than you expected: it now takes five or more visible occupied seats for an occupied seat to become empty (rather than four or more from the previous rules). The other rules still apply: empty seats that see no occupied seats become occupied, seats matching no rule don't change, and floor never changes.

+

Given the same starting layout as above, these new rules cause the seating area to shift around as follows:

+
L.LL.LL.LL
+LLLLLLL.LL
+L.L.L..L..
+LLLL.LL.LL
+L.LL.LL.LL
+L.LLLLL.LL
+..L.L.....
+LLLLLLLLLL
+L.LLLLLL.L
+L.LLLLL.LL
+
+
#.##.##.##
+#######.##
+#.#.#..#..
+####.##.##
+#.##.##.##
+#.#####.##
+..#.#.....
+##########
+#.######.#
+#.#####.##
+
+
#.LL.LL.L#
+#LLLLLL.LL
+L.L.L..L..
+LLLL.LL.LL
+L.LL.LL.LL
+L.LLLLL.LL
+..L.L.....
+LLLLLLLLL#
+#.LLLLLL.L
+#.LLLLL.L#
+
+
#.L#.##.L#
+#L#####.LL
+L.#.#..#..
+##L#.##.##
+#.##.#L.##
+#.#####.#L
+..#.#.....
+LLL####LL#
+#.L#####.L
+#.L####.L#
+
+
#.L#.L#.L#
+#LLLLLL.LL
+L.L.L..#..
+##LL.LL.L#
+L.LL.LL.L#
+#.LLLLL.LL
+..L.L.....
+LLLLLLLLL#
+#.LLLLL#.L
+#.L#LL#.L#
+
+
#.L#.L#.L#
+#LLLLLL.LL
+L.L.L..#..
+##L#.#L.L#
+L.L#.#L.L#
+#.L####.LL
+..#.#.....
+LLL###LLL#
+#.LLLLL#.L
+#.L#LL#.L#
+
+
#.L#.L#.L#
+#LLLLLL.LL
+L.L.L..#..
+##L#.#L.L#
+L.L#.LL.L#
+#.LLLL#.LL
+..#.L.....
+LLL###LLL#
+#.LLLLL#.L
+#.L#LL#.L#
+
+

Again, at this point, people stop shifting around and the seating area reaches equilibrium. Once this occurs, you count 26 occupied seats.

+

Given the new visibility method and the rule change for occupied seats becoming empty, once equilibrium is reached, how many seats end up occupied?

+
+

Your puzzle answer was 2042.

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/stack.yaml b/stack.yaml index d3710dd..daaaab9 100644 --- a/stack.yaml +++ b/stack.yaml @@ -45,6 +45,7 @@ packages: - advent08 - advent09 - advent10 +- advent11 # Dependency packages to be pulled from upstream that are not in the resolver. # These entries can reference officially published versions as well as -- 2.34.1