From 4c58e5c51cecfdd4509b927a048b60f755a58e7c Mon Sep 17 00:00:00 2001 From: Parnic Date: Sun, 26 Jun 2022 10:24:54 -0500 Subject: [PATCH] Day 24 part 1 I think I understand part 2, I just...ugh. It seems like it's going to be a beating of rote map implementation. --- days/24.go | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ inputs/24p.txt | 5 ++ inputs/24s1.txt | 5 ++ inputs/24s2.txt | 5 ++ main.go | 1 + 5 files changed, 144 insertions(+) create mode 100644 days/24.go create mode 100644 inputs/24p.txt create mode 100644 inputs/24s1.txt create mode 100644 inputs/24s2.txt diff --git a/days/24.go b/days/24.go new file mode 100644 index 0000000..84b74d9 --- /dev/null +++ b/days/24.go @@ -0,0 +1,128 @@ +package days + +import ( + "fmt" + "math" + + u "parnic.com/aoc2019/utilities" +) + +var ( + day24AdjacentOffsets = []u.Vec2i{ + {X: -1, Y: 0}, + {X: 1, Y: 0}, + {X: 0, Y: -1}, + {X: 0, Y: 1}, + } +) + +type Day24 struct { + grid [][]bool +} + +func (d *Day24) Parse() { + lines := u.GetStringLines("24p") + d.grid = make([][]bool, len(lines)) + for i, line := range lines { + d.grid[i] = make([]bool, len(line)) + for j, ch := range line { + d.grid[i][j] = ch == '#' + } + } +} + +func (d Day24) Num() int { + return 24 +} + +func (d Day24) calcActivatedNeighbors(grid [][]bool, i, j int) int { + activatedNeighbors := 0 + for _, o := range day24AdjacentOffsets { + newI := i + o.X + newJ := j + o.Y + if newI < 0 || newI >= len(grid) || newJ < 0 || newJ >= len(grid[i]) { + continue + } + if grid[newI][newJ] { + activatedNeighbors++ + } + } + + return activatedNeighbors +} + +func (d Day24) calcRating(grid [][]bool) int { + rating := 0 + for i, r := range grid { + for j := range r { + pow := (i * len(r)) + j + if grid[i][j] { + result := int(math.Pow(2, float64(pow))) + rating += result + } + } + } + return rating +} + +func copy2d[T comparable](dest [][]T, src [][]T) { + for i, r := range src { + copy(dest[i], r) + } +} + +func (d Day24) Draw(grid [][]bool) { + for _, r := range grid { + for _, c := range r { + if c { + fmt.Print("#") + } else { + fmt.Print(".") + } + } + fmt.Println() + } + fmt.Println() +} + +func (d *Day24) Part1() string { + grid := make([][]bool, len(d.grid)) + scratch := make([][]bool, len(grid)) + for i, g := range d.grid { + grid[i] = make([]bool, len(g)) + scratch[i] = make([]bool, len(g)) + copy(grid[i], d.grid[i]) + } + + found := false + answer := 0 + seenRatings := make([]int, 0) + for i := 1; !found; i++ { + // d.Draw(grid) + for i, r := range grid { + for j := range r { + numActivated := d.calcActivatedNeighbors(grid, i, j) + if grid[i][j] { + scratch[i][j] = numActivated == 1 + } else { + scratch[i][j] = numActivated == 1 || numActivated == 2 + } + } + } + + rating := d.calcRating(scratch) + if u.ArrayContains(seenRatings, rating) { + found = true + d.Draw(scratch) + answer = rating + } + seenRatings = append(seenRatings, rating) + copy2d(grid, scratch) + } + + return fmt.Sprintf("First repeated biodiversity rating is %s%d%s", u.TextBold, answer, u.TextReset) +} + +func (d *Day24) Part2() string { + return fmt.Sprintf("%s%d%s", u.TextBold, 0, u.TextReset) +} diff --git a/inputs/24p.txt b/inputs/24p.txt new file mode 100644 index 0000000..3406c57 --- /dev/null +++ b/inputs/24p.txt @@ -0,0 +1,5 @@ +##..# +...## +.#.## +#..#. +..#.. \ No newline at end of file diff --git a/inputs/24s1.txt b/inputs/24s1.txt new file mode 100644 index 0000000..949fe7c --- /dev/null +++ b/inputs/24s1.txt @@ -0,0 +1,5 @@ +....# +#..#. +#..## +..#.. +#.... \ No newline at end of file diff --git a/inputs/24s2.txt b/inputs/24s2.txt new file mode 100644 index 0000000..b95bc74 --- /dev/null +++ b/inputs/24s2.txt @@ -0,0 +1,5 @@ +..... +..... +..... +#.... +.#... \ No newline at end of file diff --git a/main.go b/main.go index 9564d8e..8f04338 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,7 @@ var dayMap = []day{ &days.Day21{}, &days.Day22{}, &days.Day23{}, + &days.Day24{}, } func main() {