-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
193 lines (169 loc) · 3.78 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"fmt"
"io"
aoc "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader) int {
grid := toGrid(aoc.ReaderToStrings(input))
before := fmt.Sprintf("%s", grid)
for {
grid.round()
after := fmt.Sprintf("%s", grid)
if before == after {
return grid.occupied()
}
before = after
}
}
type Grid struct {
row int
col int
board [][]Unit
}
func (g *Grid) String() string {
s := ""
for row := 0; row < g.row; row++ {
for col := 0; col < g.col; col++ {
pos := aoc.Position{row, col}
s += fmt.Sprintf("%c", g.seatType(pos))
}
s += "\n"
}
return s
}
func toGrid(lines []string) *Grid {
board := make([][]Unit, len(lines))
for i, line := range lines {
board[i] = make([]Unit, len(line))
for j := 0; j < len(line); j++ {
switch line[j] {
case 'L':
board[i][j] = empty
case '#':
board[i][j] = occupied
case '.':
board[i][j] = floor
}
}
}
return &Grid{
row: len(lines),
col: len(lines[0]),
board: board,
}
}
func (g *Grid) newBoard() [][]Unit {
board := make([][]Unit, g.row)
for i := 0; i < g.row; i++ {
board[i] = make([]Unit, g.col)
}
return board
}
func (g *Grid) seatType(pos aoc.Position) Unit {
return g.board[pos.Row][pos.Col]
}
func (g *Grid) isOccupied(pos aoc.Position) int {
if pos.Row < 0 || pos.Row >= g.row || pos.Col < 0 || pos.Col >= g.col {
return 0
}
if g.board[pos.Row][pos.Col] == occupied {
return 1
}
return 0
}
func (g *Grid) round() {
board := g.newBoard()
for row := 0; row < g.row; row++ {
for col := 0; col < g.col; col++ {
pos := aoc.Position{row, col}
sum := g.isOccupied(pos.Delta(-1, -1)) +
g.isOccupied(pos.Delta(-1, 0)) +
g.isOccupied(pos.Delta(-1, 1)) +
g.isOccupied(pos.Delta(0, -1)) +
g.isOccupied(pos.Delta(0, 1)) +
g.isOccupied(pos.Delta(1, -1)) +
g.isOccupied(pos.Delta(1, 0)) +
g.isOccupied(pos.Delta(1, 1))
seatType := g.seatType(pos)
if seatType == empty && sum == 0 {
board[row][col] = occupied
continue
}
if seatType == occupied && sum >= 4 {
board[row][col] = empty
continue
}
board[row][col] = seatType
}
}
g.board = board
}
func (g *Grid) occupied() int {
sum := 0
for _, row := range g.board {
for _, unit := range row {
if unit == occupied {
sum++
}
}
}
return sum
}
type Unit rune
const (
empty Unit = 'L'
occupied Unit = '#'
floor Unit = '.'
)
func fs2(input io.Reader) int {
grid := toGrid(aoc.ReaderToStrings(input))
before := fmt.Sprintf("%s", grid)
for {
grid.round2()
after := fmt.Sprintf("%s", grid)
if before == after {
return grid.occupied()
}
before = after
}
}
func (g *Grid) round2() {
board := g.newBoard()
for row := 0; row < g.row; row++ {
for col := 0; col < g.col; col++ {
pos := aoc.Position{row, col}
sum := g.isOccupiedRange(pos.Delta(-1, -1), aoc.UpLeft) +
g.isOccupiedRange(pos.Delta(-1, 0), aoc.Up) +
g.isOccupiedRange(pos.Delta(-1, 1), aoc.UpRight) +
g.isOccupiedRange(pos.Delta(0, -1), aoc.Left) +
g.isOccupiedRange(pos.Delta(0, 1), aoc.Right) +
g.isOccupiedRange(pos.Delta(1, -1), aoc.DownLeft) +
g.isOccupiedRange(pos.Delta(1, 0), aoc.Down) +
g.isOccupiedRange(pos.Delta(1, 1), aoc.DownRight)
seatType := g.seatType(pos)
if seatType == empty && sum == 0 {
board[row][col] = occupied
continue
}
if seatType == occupied && sum >= 5 {
board[row][col] = empty
continue
}
board[row][col] = seatType
}
}
g.board = board
}
func (g *Grid) isOccupiedRange(pos aoc.Position, direction aoc.Direction) int {
if pos.Row < 0 || pos.Row >= g.row || pos.Col < 0 || pos.Col >= g.col {
return 0
}
if g.board[pos.Row][pos.Col] == occupied {
return 1
}
if g.board[pos.Row][pos.Col] == empty {
return 0
}
return g.isOccupiedRange(pos.Move(direction, 1), direction)
}