might be making a mistake starting 2018...

This commit is contained in:
alexchao26
2020-09-06 20:12:08 -04:00
parent 6508ec81d4
commit 3aa2b3e09a
104 changed files with 1100 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
#.#.#
.#...
...#.
.###.
###.#
+98
View File
@@ -0,0 +1,98 @@
package main
import (
"adventofcode/util"
"fmt"
"strings"
)
func main() {
input := util.ReadFile("../input.txt")
lines := strings.Split(input, "\n")
grid := make([][]string, len(lines))
for i, v := range lines {
grid[i] = strings.Split(v, "")
}
// can map biodiversity scores because they're essentially bitmaps
previousBiodiversities := map[int]bool{getBiodiversity(grid): true}
// run indefinitely
for {
// step through a minute
grid = stepMinute(grid)
newBiodiversity := getBiodiversity(grid)
// if new biodiversity score is already in the map, print it and exit
if previousBiodiversities[newBiodiversity] {
fmt.Println("Repeated biodiversity score", newBiodiversity)
return
}
// set biodiversity score into in map
previousBiodiversities[newBiodiversity] = true
}
}
// steps through one minute and returns the next grid
func stepMinute(grid [][]string) [][]string {
result := make([][]string, len(grid))
dRow := [4]int{0, 0, -1, 1}
dCol := [4]int{-1, 1, 0, 0}
for row, rowSli := range grid {
// initialize the rows for the result slice
result[row] = make([]string, len(grid[0]))
for col, val := range rowSli {
// count up the neighbors that are bugs
var countNeighborBugs int
for i := 0; i < 4; i++ {
nextRow, nextCol := row+dRow[i], col+dCol[i]
isInbounds := nextRow >= 0 && nextCol >= 0 && nextRow < len(grid) && nextCol < len(grid[0])
if isInbounds && grid[nextRow][nextCol] == "#" {
countNeighborBugs++
}
}
// determine future state of cell
switch val {
case "#":
// if bug has ONE neighbor only, it lives, otherwise becomes empty
if countNeighborBugs == 1 {
result[row][col] = "#"
} else {
result[row][col] = "."
}
case ".":
// if empty, becomes a bug if has ONE or TWO neighbors only
if countNeighborBugs == 1 || countNeighborBugs == 2 {
result[row][col] = "#"
} else {
result[row][col] = "."
}
}
}
}
return result
}
func getBiodiversity(grid [][]string) int {
var biodiversity int
for i, row := range grid {
for j, val := range row {
// dumb cheeky way to get power of two... 1 is 2^0, then shift the power
// 1 << 0 == 2^0 == 1; 1 << 1 == 2^1
powerOfTwo := 1 << (5*i + j)
if val == "#" {
biodiversity += powerOfTwo
}
}
}
return biodiversity
}
+171
View File
@@ -0,0 +1,171 @@
package main
import (
"adventofcode/util"
"fmt"
"strings"
)
// RecursiveWorld stores a big 3D matrix & will have associated methods
type RecursiveWorld struct {
// 401 so there are 200 layers above and below initial layer
// using ints to expedite calculating neighbor sums and initializing as 0s
levels [401][5][5]int
}
func main() {
input := util.ReadFile("../input.txt")
lines := strings.Split(input, "\n")
var initialGrid [5][5]int
for i, line := range lines {
for j, v := range line {
if v == '#' {
initialGrid[i][j] = 1
}
}
}
// initialize recursive world - zero values of array will start every cell at 0
var world RecursiveWorld
// set the "middle" layer at 200 - this works because we're only running for 200 minutes
world.levels[200] = initialGrid
// run for 200 minutes
for i := 0; i < 200; i++ {
world.minute()
}
// print the final count
fmt.Println("Final count", world.countBugs())
}
func (world *RecursiveWorld) minute() {
nextMinuteLevels := [401][5][5]int{}
for i := 0; i < 401; i++ {
for row := 0; row < 5; row++ {
for col := 0; col < 5; col++ {
sumNeighbors := world.getSumOfNeighbors(i, row, col)
nextMinuteLevels[i][row][col] = world.nextCellValue(
world.levels[i][row][col], sumNeighbors)
}
}
}
// reassign levels
world.levels = nextMinuteLevels
}
// get the sum of neighbors, including recursive layers
func (world *RecursiveWorld) getSumOfNeighbors(i, j, k int) int {
// center should always remain zero
if j == 2 && k == 2 {
return 0
}
dx, dy := [4]int{0, 0, -1, 1}, [4]int{-1, 1, 0, 0}
var sumOfNeighbors int
for d := 0; d < 4; d++ {
nextRow, nextCol := j+dx[d], k+dy[d]
isInBounds := nextRow >= 0 && nextCol >= 0 && nextRow < 5 && nextCol < 5
// if not in bounds, this cell is trying to access the layer "outside" of it
if !isInBounds {
sumOfNeighbors += world.getNeighborsOut(i+1, nextRow, nextCol)
} else if nextRow == 2 && nextCol == 2 {
// if a neighbor cell has 2,2 coordinates, it is trying to recurse "in"
sumOfNeighbors += world.getNeighborsIn(i-1, j, k)
} else if isInBounds {
// otherwise if it is inbounds, add from this layer
sumOfNeighbors += world.levels[i][nextRow][nextCol]
}
}
return sumOfNeighbors
}
// Assuming going outwards moves UP level indexes
// nextRow and nextCol are the requested coordinates from the origin/cell calling this function
func (world *RecursiveWorld) getNeighborsOut(level, nextRow, nextCol int) int {
// edge case for "recursive" calls asking for -1 layer or 401 layer
if level == -1 || level == 401 {
return 0
}
currentLevel := world.levels[level]
// origin cell is asking for "above" itself
if nextRow == -1 {
return currentLevel[1][2]
}
// origin cell asking for "below" itself
if nextRow == 5 {
return currentLevel[3][2]
}
// asking for "left"
if nextCol == -1 {
return currentLevel[2][1]
}
// asking for "right"
return currentLevel[2][3]
}
// Assume going inwards moves DOWN level indices
// originRow and Col are coordinates of the cell requesting its neighboring values
func (world *RecursiveWorld) getNeighborsIn(level, originRow, originCol int) int {
// edge case for "recursive" calls asking for -1 layer or 401 layer
if level == -1 || level == 401 {
return 0
}
// sum up 5 values of the argument level
currentLevel := world.levels[level]
var left, right, top, bottom int
for i := 0; i < 5; i++ {
left += currentLevel[i][0]
right += currentLevel[i][4]
top += currentLevel[0][i]
bottom += currentLevel[4][i]
}
// if originRow is 1, then it was above this layer, return top values
if originRow == 1 {
return top
}
// if originRow is 3, it is below this layer
if originRow == 3 {
return bottom
}
// if originCol is 1, it is left of this layer
if originCol == 1 {
return left
}
// otherwise only remaining direction is right
return right
}
// gets the next values of a cell given the old value of the cell & sum of its neighbors
func (world *RecursiveWorld) nextCellValue(oldVal, sumNeighbors int) int {
if oldVal == 1 && sumNeighbors == 1 {
return 1
}
if oldVal == 0 && (sumNeighbors == 1 || sumNeighbors == 2) {
return 1
}
return 0
}
// count up the bugs in every level and return it
func (world *RecursiveWorld) countBugs() int {
var bugs int
for _, grid := range world.levels {
for _, row := range grid {
for _, val := range row {
bugs += val
}
}
}
return bugs
}
+223
View File
@@ -0,0 +1,223 @@
--- Day 24: Planet of Discord ---
You land on Eris, your last stop before reaching Santa. As soon as you do, your sensors start picking up strange life forms moving around: Eris is infested with bugs! With an over 24-hour roundtrip for messages between you and Earth, you'll have to deal with this problem on your own.
Eris isn't a very large place; a scan of the entire area fits into a 5x5 grid (your puzzle input). The scan shows bugs (#) and empty spaces (.).
Each minute, The bugs live and die based on the number of bugs in the four adjacent tiles:
A bug dies (becoming an empty space) unless there is exactly one bug adjacent to it.
An empty space becomes infested with a bug if exactly one or two bugs are adjacent to it.
Otherwise, a bug or empty space remains the same. (Tiles on the edges of the grid have fewer than four adjacent tiles; the missing tiles count as empty space.) This process happens in every location simultaneously; that is, within the same minute, the number of adjacent bugs is counted for every tile first, and then the tiles are updated.
Here are the first few minutes of an example scenario:
Initial state:
....#
#..#.
#..##
..#..
#....
After 1 minute:
#..#.
####.
###.#
##.##
.##..
After 2 minutes:
#####
....#
....#
...#.
#.###
After 3 minutes:
#....
####.
...##
#.##.
.##.#
After 4 minutes:
####.
....#
##..#
.....
##...
To understand the nature of the bugs, watch for the first time a layout of bugs and empty spaces matches any previous layout. In the example above, the first layout to appear twice is:
.....
.....
.....
#....
.#...
To calculate the biodiversity rating for this layout, consider each tile left-to-right in the top row, then left-to-right in the second row, and so on. Each of these tiles is worth biodiversity points equal to increasing powers of two: 1, 2, 4, 8, 16, 32, and so on. Add up the biodiversity points for tiles with bugs; in this example, the 16th tile (32768 points) and 22nd tile (2097152 points) have bugs, a total biodiversity rating of 2129920.
What is the biodiversity rating for the first layout that appears twice?
Your puzzle answer was 11042850.
--- Part Two ---
After careful analysis, one thing is certain: you have no idea where all these bugs are coming from.
Then, you remember: Eris is an old Plutonian settlement! Clearly, the bugs are coming from recursively-folded space.
This 5x5 grid is only one level in an infinite number of recursion levels. The tile in the middle of the grid is actually another 5x5 grid, the grid in your scan is contained as the middle tile of a larger 5x5 grid, and so on. Two levels of grids look like this:
| | | |
| | | |
| | | |
-----+-----+---------+-----+-----
| | | |
| | | |
| | | |
-----+-----+---------+-----+-----
| | | | | | | |
| |-+-+-+-+-| |
| | | | | | | |
| |-+-+-+-+-| |
| | | |?| | | |
| |-+-+-+-+-| |
| | | | | | | |
| |-+-+-+-+-| |
| | | | | | | |
-----+-----+---------+-----+-----
| | | |
| | | |
| | | |
-----+-----+---------+-----+-----
| | | |
| | | |
| | | |
(To save space, some of the tiles are not drawn to scale.) Remember, this is only a small part of the infinitely recursive grid; there is a 5x5 grid that contains this diagram, and a 5x5 grid that contains that one, and so on. Also, the ? in the diagram contains another 5x5 grid, which itself contains another 5x5 grid, and so on.
The scan you took (your puzzle input) shows where the bugs are on a single level of this structure. The middle tile of your scan is empty to accommodate the recursive grids within it. Initially, no other levels contain bugs.
Tiles still count as adjacent if they are directly up, down, left, or right of a given tile. Some tiles have adjacent tiles at a recursion level above or below its own level. For example:
| | | |
1 | 2 | 3 | 4 | 5
| | | |
-----+-----+---------+-----+-----
| | | |
6 | 7 | 8 | 9 | 10
| | | |
-----+-----+---------+-----+-----
| |A|B|C|D|E| |
| |-+-+-+-+-| |
| |F|G|H|I|J| |
| |-+-+-+-+-| |
11 | 12 |K|L|?|N|O| 14 | 15
| |-+-+-+-+-| |
| |P|Q|R|S|T| |
| |-+-+-+-+-| |
| |U|V|W|X|Y| |
-----+-----+---------+-----+-----
| | | |
16 | 17 | 18 | 19 | 20
| | | |
-----+-----+---------+-----+-----
| | | |
21 | 22 | 23 | 24 | 25
| | | |
Tile 19 has four adjacent tiles: 14, 18, 20, and 24.
Tile G has four adjacent tiles: B, F, H, and L.
Tile D has four adjacent tiles: 8, C, E, and I.
Tile E has four adjacent tiles: 8, D, 14, and J.
Tile 14 has eight adjacent tiles: 9, E, J, O, T, Y, 15, and 19.
Tile N has eight adjacent tiles: I, O, S, and five tiles within the sub-grid marked ?.
The rules about bugs living and dying are the same as before.
For example, consider the same initial state as above:
....#
#..#.
#.?##
..#..
#....
The center tile is drawn as ? to indicate the next recursive grid. Call this level 0; the grid within this one is level 1, and the grid that contains this one is level -1. Then, after ten minutes, the grid at each level would look like this:
Depth -5:
..#..
.#.#.
..?.#
.#.#.
..#..
Depth -4:
...#.
...##
..?..
...##
...#.
Depth -3:
#.#..
.#...
..?..
.#...
#.#..
Depth -2:
.#.##
....#
..?.#
...##
.###.
Depth -1:
#..##
...##
..?..
...#.
.####
Depth 0:
.#...
.#.##
.#?..
.....
.....
Depth 1:
.##..
#..##
..?.#
##.##
#####
Depth 2:
###..
##.#.
#.?..
.#.##
#.#..
Depth 3:
..###
.....
#.?..
#....
#...#
Depth 4:
.###.
#..#.
#.?..
##.#.
.....
Depth 5:
####.
#..#.
#.?#.
####.
.....
In this example, after 10 minutes, a total of 99 bugs are present.
Starting with your scan, how many bugs are present after 200 minutes?
Your puzzle answer was 1967.
Both parts of this puzzle are complete! They provide two gold stars: **