2020 Day 11 Complete

This commit is contained in:
Brian Buller 2020-12-11 08:11:30 -06:00
parent 581b0682d4
commit 772298895b
6 changed files with 634 additions and 0 deletions

97
2020/day11/input Normal file
View File

@ -0,0 +1,97 @@
LLL.LLL.LLLLL.LLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LLLLLLLL
LLLLLLL.LLLLLLLL.LLLLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLLLLLL..LLLLLLL..LLLLLLL.LLLL.LLLLLLLLLL
LLLLLLLLLLLLLLLL.LLLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.L.LLLLLLLL.LLLLLL.LLLLLLLL
LLLLLLL.LLLLLL.LLLL.LLLLLLLLL.LLLLL.LLL.LLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLLLLLLLLLL.LLLLLLLL
LLLLLLL.LLL.LLLL.LLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLL.LLLLLL.LLLLLLLL
LLLLLLL.LLLLLLLLLLLLLLLL.LLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLLLLLL.LLLL
...L..L.L......LL.L.......L...L..LLL....L.LL.L..L.L.LL..L..L............LLL.L..L.L.L..LL..
LLLLLLLLLLLLLLL..LLLLL.L.LLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLL.L.LLLLLL.LLLLLLLL
.LLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLLLLLLLL.LLLLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLL.LLLLLL.LL.LLLLL
LLLLLLLLLLLLLLLL.LLLLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL.LLL.LLLLLLLLLLL
LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLLLL.L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LLLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLL.LL.LLLLLLLL.LLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL.LLLLLLL.LLLL.LLLLLLLLL.LLLLLLLLLLLLLLLLL.LLLLLLL.LLLLLLLLLLLLLLL.LLLLLLL.
LLLLLLL.LLLLLLLL.LLLLLLL.LLLLLLLLLLLLLL.LLLLLLL.LLLLLL.LL.LLLLLLL.LLLLLLLL.LLLLLLLLLLLLLLL

263
2020/day11/main.go Normal file
View File

@ -0,0 +1,263 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
const (
EMPTY = 'L'
FULL = '#'
FLOOR = '.'
N = iota
NE
E
SE
S
SW
W
NW
)
func main() {
fmt.Println("# Day 11")
inp := h.StdinToStringSlice()
pt := h.GetArgNumber(1)
if pt == "1" {
part1(inp)
} else {
part2(inp)
}
}
func part1(inp []string) {
fmt.Println("## Part 1")
w := BuildWaitingRoom(inp)
var last string
for w.String() != last {
last = w.String()
w.Tick()
}
fmt.Println(w)
fmt.Printf("At stabilization there are %d occupied seats\n", w.OccupiedSeats())
}
func part2(inp []string) {
fmt.Println("## Part 2")
w := BuildWaitingRoom(inp)
var last string
for w.String() != last {
last = w.String()
w.VisibleTick()
}
fmt.Println(w)
fmt.Printf("At stabilization there are %d occupied seats\n", w.OccupiedSeats())
}
type WaitingRoom struct {
Layout map[h.Coordinate]byte
NorthMost int
SouthMost int
EastMost int
WestMost int
}
func BuildWaitingRoom(inp []string) *WaitingRoom {
ret := WaitingRoom{
Layout: make(map[h.Coordinate]byte),
NorthMost: h.MAX_INT,
SouthMost: h.MIN_INT,
EastMost: h.MIN_INT,
WestMost: h.MAX_INT,
}
for y := range inp {
for x := range inp[y] {
ret.Layout[h.Coordinate{X: x, Y: y}] = inp[y][x]
if x < ret.WestMost {
ret.WestMost = x
}
if x > ret.EastMost {
ret.EastMost = x
}
}
if y < ret.NorthMost {
ret.NorthMost = y
}
if y > ret.SouthMost {
ret.SouthMost = y
}
}
return &ret
}
func (w *WaitingRoom) OccupiedSeats() int {
var ret int
for k := range w.Layout {
if !w.CoordEmpty(k) {
ret++
}
}
return ret
}
func (w *WaitingRoom) CoordEmpty(c h.Coordinate) bool {
return w.Layout[c] != FULL
}
func (w *WaitingRoom) ShouldFill(c h.Coordinate) bool {
if w.Layout[c] == FULL || w.Layout[c] == FLOOR {
return false
}
return w.CoordEmpty(c.North()) && w.CoordEmpty(c.NE()) && w.CoordEmpty(c.East()) && w.CoordEmpty(c.SE()) && w.CoordEmpty(c.South()) && w.CoordEmpty(c.SW()) && w.CoordEmpty(c.West()) && w.CoordEmpty(c.NW())
}
func (w *WaitingRoom) ShouldEmpty(c h.Coordinate) bool {
if w.Layout[c] == EMPTY || w.Layout[c] == FLOOR {
return false
}
var fullSeats int
for _, v := range []h.Coordinate{c.North(), c.NE(), c.East(), c.SE(), c.South(), c.SW(), c.West(), c.NW()} {
if !w.CoordEmpty(v) {
fullSeats++
if fullSeats >= 4 {
return true
}
}
}
return false
}
func (w *WaitingRoom) Tick() {
wrk := make(map[h.Coordinate]byte)
for k := range w.Layout {
wrk[k] = w.Layout[k]
switch w.Layout[k] {
case EMPTY:
if w.ShouldFill(k) {
wrk[k] = FULL
}
case FULL:
if w.ShouldEmpty(k) {
wrk[k] = EMPTY
}
}
}
for k := range wrk {
w.Layout[k] = wrk[k]
}
}
func (w *WaitingRoom) IsFloor(c h.Coordinate) bool {
return w.Layout[c] == FLOOR
}
func (w *WaitingRoom) GetCoordInDir(c h.Coordinate, dir int) h.Coordinate {
switch dir {
case N:
return c.North()
case NE:
return c.NE()
case E:
return c.East()
case SE:
return c.SE()
case S:
return c.South()
case SW:
return c.SW()
case W:
return c.West()
case NW:
return c.NW()
}
return c
}
func (w *WaitingRoom) DirectionEmpty(c h.Coordinate, dir int) bool {
c = w.GetCoordInDir(c, dir)
for w.IsFloor(c) {
c = w.GetCoordInDir(c, dir)
}
return w.CoordEmpty(c)
}
func (w *WaitingRoom) VisibleShouldFill(c h.Coordinate) bool {
if w.Layout[c] == FULL || w.Layout[c] == FLOOR {
return false
}
return w.DirectionEmpty(c, N) && w.DirectionEmpty(c, NE) && w.DirectionEmpty(c, E) && w.DirectionEmpty(c, SE) && w.DirectionEmpty(c, S) && w.DirectionEmpty(c, SW) && w.DirectionEmpty(c, W) && w.DirectionEmpty(c, NW)
}
func (w *WaitingRoom) VisibleShouldEmpty(c h.Coordinate) bool {
if w.Layout[c] == EMPTY || w.Layout[c] == FLOOR {
return false
}
var fullSeats int
for _, v := range []int{N, NE, E, SE, S, SW, W, NW} {
if !w.DirectionEmpty(c, v) {
fullSeats++
if fullSeats >= 5 {
return true
}
}
}
return false
}
func (w *WaitingRoom) VisibleTick() {
wrk := make(map[h.Coordinate]byte)
for k := range w.Layout {
wrk[k] = w.Layout[k]
switch w.Layout[k] {
case EMPTY:
if w.VisibleShouldFill(k) {
wrk[k] = FULL
}
case FULL:
if w.VisibleShouldEmpty(k) {
wrk[k] = EMPTY
}
}
}
for k := range wrk {
w.Layout[k] = wrk[k]
}
}
func (w WaitingRoom) String() string {
var ret string
for y := w.NorthMost; y <= w.SouthMost; y++ {
for x := w.WestMost; x <= w.EastMost; x++ {
ret = ret + string(w.Layout[h.Coordinate{X: x, Y: y}])
}
ret = ret + "\n"
}
return ret
}
func DirToStr(dir int) string {
switch dir {
case N:
return "N"
case NE:
return "NE"
case E:
return "E"
case SE:
return "SE"
case S:
return "S"
case SW:
return "SW"
case W:
return "W"
case NW:
return "NW"
}
return "-"
}

243
2020/day11/problem Normal file
View File

@ -0,0 +1,243 @@
Advent of Code
--- 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 2265.
--- 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 2045.
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.
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2020/about
. https://adventofcode.com/2020/events
. https://adventofcode.com/2020/settings
. https://adventofcode.com/2020/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2020/support
. https://adventofcode.com/2020
. https://adventofcode.com/2020
. https://adventofcode.com/2020/support
. https://adventofcode.com/2020/sponsors
. https://adventofcode.com/2020/leaderboard
. https://adventofcode.com/2020/stats
. https://adventofcode.com/2020/sponsors
. https://adventofcode.com/2020
. https://adventofcode.com/2020/day/11/input

10
2020/day11/testinput Normal file
View File

@ -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

View File

@ -26,6 +26,18 @@ func (c *Coordinate) South() Coordinate {
func (c *Coordinate) West() Coordinate { func (c *Coordinate) West() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y} return Coordinate{X: c.X - 1, Y: c.Y}
} }
func (c *Coordinate) NW() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y - 1}
}
func (c *Coordinate) NE() Coordinate {
return Coordinate{X: c.X + 1, Y: c.Y - 1}
}
func (c *Coordinate) SW() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y + 1}
}
func (c *Coordinate) SE() Coordinate {
return Coordinate{X: c.X + 1, Y: c.Y + 1}
}
func (c *Coordinate) GetNorthCoord() *Coordinate { func (c *Coordinate) GetNorthCoord() *Coordinate {
return &Coordinate{ return &Coordinate{

View File

@ -230,3 +230,12 @@ func IntSlicesAreEqual(s1 []int, s2 []int) bool {
} }
return true return true
} }
func StringSliceContains(h []string, n string) bool {
for _, v := range h {
if v == n {
return true
}
}
return false
}