-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
139 lines (113 loc) · 2.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
)
func parse(data []byte) [][]bool {
var grid [][]bool
var line []bool
for _, d := range data {
switch d {
case '#':
line = append(line, true)
case '.':
line = append(line, false)
case '\n':
grid = append(grid, line)
line = nil
default:
panic(d)
}
}
if len(line) != 0 {
grid = append(grid, line)
}
return grid
}
func neighbors(grid [][]bool, x, y int) []bool {
var result []bool
for xOff := -1; xOff <= 1; xOff++ {
for yOff := -1; yOff <= 1; yOff++ {
if xOff == 0 && yOff == 0 {
continue
}
xNeighbor := x + xOff
yNeighbor := y + yOff
if xNeighbor < 0 || xNeighbor >= len(grid) {
continue
}
if yNeighbor < 0 || yNeighbor >= len(grid[xNeighbor]) {
continue
}
result = append(result, grid[xNeighbor][yNeighbor])
}
}
return result
}
type pos struct {
x, y int
}
func step(grid [][]bool) [][]bool {
var flips []pos
for x := 0; x < len(grid); x++ {
for y := 0; y < len(grid[x]); y++ {
neighborOnCount := len(lib.Filter(neighbors(grid, x, y), func(neighbor bool) bool { return neighbor }))
p := pos{x: x, y: y}
switch p {
case pos{x: 0, y: 0}:
continue
case pos{x: 0, y: len(grid[x]) - 1}:
continue
case pos{x: len(grid) - 1, y: 0}:
continue
case pos{x: len(grid) - 1, y: len(grid[x]) - 1}:
continue
}
switch grid[x][y] {
case true:
if !(neighborOnCount == 2 || neighborOnCount == 3) {
flips = append(flips, p)
}
case false:
if neighborOnCount == 3 {
flips = append(flips, p)
}
}
}
}
for _, flip := range flips {
grid[flip.x][flip.y] = !grid[flip.x][flip.y]
}
// only for step solver
return grid
}
func solve(grid [][]bool) int {
for i := 0; i < 100; i++ {
step(grid)
}
sum := 0
for _, line := range grid {
for _, elem := range line {
if elem {
sum += 1
}
}
}
return sum
}
func main() {
stepSolver := lib.Solver[[][]bool, [][]bool]{
ParseF: lib.ParseBytesFunc(parse),
SolveF: step,
}
stepSolver.ParseExpect(".#.#\n..##", [][]bool{{false, true, false, true}, {false, false, true, true}})
stepSolver.Expect("##.#.#\n...##.\n#....#\n..#...\n#.#..#\n####.#", parse([]byte("#.##.#\n####.#\n...##.\n......\n#...#.\n#.####")))
stepSolver.Expect("#.##.#\n####.#\n...##.\n......\n#...#.\n#.####", parse([]byte("#..#.#\n#....#\n.#.##.\n...##.\n.#..##\n##.###")))
stepSolver.Expect("#..#.#\n#....#\n.#.##.\n...##.\n.#..##\n##.###", parse([]byte("#...##\n####.#\n..##.#\n......\n##....\n####.#")))
stepSolver.Expect("#...##\n####.#\n..##.#\n......\n##....\n####.#", parse([]byte("#.####\n#....#\n...#..\n.##...\n#.....\n#.#..#")))
stepSolver.Expect("#.####\n#....#\n...#..\n.##...\n#.....\n#.#..#", parse([]byte("##.###\n.##..#\n.##...\n.##...\n#.#...\n##...#")))
solver := lib.Solver[[][]bool, int]{
ParseF: lib.ParseBytesFunc(parse),
SolveF: solve,
}
solver.Verify(781)
}