mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2020 day11 - 2D grid & frequency/game of life - 650/300
This commit is contained in:
+133
-17
@@ -16,36 +16,152 @@ func main() {
|
||||
|
||||
if part == 1 {
|
||||
ans := part1(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
fmt.Println("Output:", ans)
|
||||
} else {
|
||||
ans := part2(util.ReadFile("./input.txt"))
|
||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||
fmt.Println("Output:", ans)
|
||||
}
|
||||
}
|
||||
|
||||
func part1(input string) int {
|
||||
parsed := parseInput(input)
|
||||
_ = parsed
|
||||
grid := parseInput(input)
|
||||
|
||||
return 0
|
||||
var lastState string
|
||||
for {
|
||||
grid = step(grid, 4, true)
|
||||
|
||||
// check if last grid matches current, break out if they match
|
||||
str := stringify(grid)
|
||||
if str == lastState {
|
||||
break
|
||||
}
|
||||
lastState = str
|
||||
}
|
||||
|
||||
func part2(input string) int {
|
||||
parsed := parseInput(input)
|
||||
_ = parsed
|
||||
|
||||
return 0
|
||||
var ans int
|
||||
for _, row := range grid {
|
||||
for _, v := range row {
|
||||
if v == "#" {
|
||||
ans++
|
||||
}
|
||||
}
|
||||
|
||||
func parseInput(input string) []int {
|
||||
var ans []int
|
||||
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, l := range lines {
|
||||
ans = append(ans, mathutil.StrToInt(l))
|
||||
}
|
||||
|
||||
return ans
|
||||
}
|
||||
|
||||
func part2(input string) int {
|
||||
grid := parseInput(input)
|
||||
|
||||
var lastState string
|
||||
for {
|
||||
grid = step(grid, 5, false)
|
||||
|
||||
// check if last grid matches current, break out if they match
|
||||
str := stringify(grid)
|
||||
if str == lastState {
|
||||
break
|
||||
}
|
||||
lastState = str
|
||||
}
|
||||
|
||||
var ans int
|
||||
for _, row := range grid {
|
||||
for _, v := range row {
|
||||
if v == "#" {
|
||||
ans++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ans
|
||||
}
|
||||
|
||||
func parseInput(input string) [][]string {
|
||||
var ans [][]string
|
||||
|
||||
lines := strings.Split(input, "\n")
|
||||
for _, l := range lines {
|
||||
ans = append(ans, strings.Split(l, ""))
|
||||
}
|
||||
|
||||
return ans
|
||||
}
|
||||
|
||||
// justNeighbors differentiates part 1 (true) from part 2
|
||||
// tolerance = 4 for part 1, 5 for part 2
|
||||
func step(grid [][]string, tolerance int, justNeighbors bool) [][]string {
|
||||
var nextGrid [][]string
|
||||
|
||||
for r, row := range grid {
|
||||
nextGrid = append(nextGrid, make([]string, len(grid[0])))
|
||||
for c, v := range row {
|
||||
if v == "." {
|
||||
nextGrid[r][c] = "."
|
||||
} else {
|
||||
neighbors := countNeighbors(grid, r, c, justNeighbors)
|
||||
// check if seats should be updated
|
||||
if v == "L" && neighbors == 0 {
|
||||
nextGrid[r][c] = "#"
|
||||
} else if v == "#" && neighbors >= tolerance {
|
||||
nextGrid[r][c] = "L"
|
||||
} else {
|
||||
nextGrid[r][c] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return nextGrid
|
||||
}
|
||||
|
||||
var directions = [8][2]int{
|
||||
{-1, -1},
|
||||
{-1, 0},
|
||||
{-1, 1},
|
||||
{0, -1},
|
||||
{0, 1},
|
||||
{1, -1},
|
||||
{1, 0},
|
||||
{1, 1},
|
||||
}
|
||||
|
||||
func countNeighbors(grid [][]string, row, col int, justNeighbors bool) int {
|
||||
var countNeighbors int
|
||||
for _, d := range directions {
|
||||
nextR, nextC := row, col
|
||||
for {
|
||||
nextR += d[0]
|
||||
nextC += d[1]
|
||||
if nextR < 0 || nextR >= len(grid) || nextC < 0 || nextC >= len(grid[0]) {
|
||||
break
|
||||
}
|
||||
if grid[nextR][nextC] == "L" {
|
||||
break
|
||||
}
|
||||
if grid[nextR][nextC] == "#" {
|
||||
countNeighbors++
|
||||
break
|
||||
}
|
||||
|
||||
// break out after first pass if only checking immediate neighbors (part 1)
|
||||
if justNeighbors {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return countNeighbors
|
||||
}
|
||||
|
||||
// stringifies grid so it can be compared to its former state
|
||||
func stringify(grid [][]string) string {
|
||||
var str string
|
||||
for _, row := range grid {
|
||||
for _, v := range row {
|
||||
|
||||
str += v
|
||||
}
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
+20
-5
@@ -1,14 +1,29 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alexchao26/advent-of-code-go/util"
|
||||
)
|
||||
|
||||
var exampleInput = `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`
|
||||
|
||||
var tests1 = []struct {
|
||||
name string
|
||||
want int
|
||||
input string
|
||||
// add extra args if needed
|
||||
}{
|
||||
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
|
||||
{"example", 37, exampleInput},
|
||||
{"actual", 2108, util.ReadFile("input.txt")},
|
||||
}
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
@@ -25,9 +40,9 @@ var tests2 = []struct {
|
||||
name string
|
||||
want int
|
||||
input string
|
||||
// add extra args if needed
|
||||
}{
|
||||
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
|
||||
{"example", 26, exampleInput},
|
||||
{"actual", 1897, util.ReadFile("input.txt")},
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
|
||||
Executable
+260
@@ -0,0 +1,260 @@
|
||||
|
||||
--- 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?
|
||||
|
||||
Reference in New Issue
Block a user