-
Couldn't load subscription status.
- Fork 0
Solve Day 11 #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Solve Day 11 #19
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e4073c
Setup Day 11
manuphatak 9404ee0
Rename fromRightOrError' -> fromRightOrShowError
manuphatak efe7bb4
Generalize occurrences util to work with Foldables
manuphatak b87abd8
Solve part 1
manuphatak c3edaa9
update readme
manuphatak dfabcf1
delete unused
manuphatak 9500515
prepare part 2
manuphatak f8ecc63
Solve part 2
manuphatak ec5e3a6
use explicit imports
manuphatak 3a2971b
cleanup solution
manuphatak 35c908b
De-generalize the result of occurrences
manuphatak ec8a6bc
Fix pedantic hs warnings
manuphatak 32c7668
Merge remote-tracking branch 'origin/main' into day_11
manuphatak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,12 @@ import Data.List (tails) | |
| isBetween :: Ord a => a -> a -> a -> Bool | ||
| isBetween lower upper target = target >= lower && target <= upper | ||
|
|
||
| occurrences :: Eq a => a -> [a] -> Int | ||
| occurrences target = length . filter (target ==) | ||
| occurrences :: (Foldable t, Eq a) => a -> t a -> Int | ||
| occurrences target = foldr go 0 | ||
| where | ||
| go value | ||
| | value == target = succ | ||
| | otherwise = id | ||
|
|
||
| readInt :: String -> Int | ||
| readInt n = read n :: Int | ||
|
|
@@ -22,6 +26,6 @@ combinations n xs = | |
| ys <- combinations (pred n) xs' | ||
| ] | ||
|
|
||
| fromRightOrError' :: Show a => Either a b -> b | ||
| fromRightOrError' (Left x) = error (show x) | ||
| fromRightOrError' (Right x) = x | ||
| fromRightOrShowError :: Show a => Either a b -> b | ||
| fromRightOrShowError (Left x) = error (show x) | ||
| fromRightOrShowError (Right x) = x | ||
|
Comment on lines
-25
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was lookin' for a name for this thing. This is inspired by Debug.Trace.traceShow |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| ## 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?_ | ||
|
|
||
| ## 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?_ | ||
|
|
||
| ## Link | ||
|
|
||
| [https://adventofcode.com/2020/day/11][1] | ||
|
|
||
| [1]: https://adventofcode.com/2020/day/11 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Generalized this function to work with
Foldables includingData.Map