-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11.go
More file actions
171 lines (145 loc) · 3.7 KB
/
day11.go
File metadata and controls
171 lines (145 loc) · 3.7 KB
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
package main
import (
"fmt"
)
func countOccupiedSeats(seatPlan [][]byte) int {
var occupiedSeats int = 0
for y := 0; y < len(seatPlan); y++ {
for x := 0; x < len(seatPlan[0]); x++ {
if seatPlan[y][x] == '#' {
occupiedSeats++
}
}
}
return occupiedSeats
}
func setSeatPlan(puzzleInput []string, seatPlan [][]byte) {
for y := 0; y < len(puzzleInput); y++ {
for x := 0; x < len(puzzleInput[0]); x++ {
seatPlan[y][x] = puzzleInput[y][x]
}
}
}
func checkArea(tempArea [][]byte, yCoord int, xCoord int) (int, int) {
var occupiedSeats int = 0
var emptySeats int = 0
var startX, startY, maxX, maxY int
// To consider: the edges. So if xCoord / yCoord == 0 or xCoord / yCoord = len(tempArea)
// x x x o o x x x x o x x x x x x x x
// o x x x x x x o x x x x o x o x x o
// x x x x
startX = xCoord - 1
startY = yCoord - 1
maxX = xCoord + 1
maxY = yCoord + 1
if xCoord == 0 {
startX = 0
} else {
if xCoord == len(tempArea[yCoord])-1 {
maxX = xCoord
}
}
if yCoord == 0 {
startY = 0
} else {
if yCoord == len(tempArea)-1 {
maxY = yCoord
}
}
for i := startY; i <= maxY; i++ {
for j := startX; j <= maxX; j++ {
if i == yCoord && j == xCoord {
// Do nothing
} else {
if tempArea[i][j] == '#' {
occupiedSeats++
} else {
if tempArea[i][j] == 'L' {
emptySeats++
}
}
}
}
}
return occupiedSeats, emptySeats
}
func applySeatingRules(oldSeatPlan [][]byte, newSeatPlan [][]byte, debug bool) ([][]byte, bool) {
var occupiedSeats int
var somethingChanged bool = false
// Loop through old array and apply rules when copying to new array
for y := 0; y < len(oldSeatPlan); y++ {
for x := 0; x < len(oldSeatPlan[0]); x++ {
newSeatPlan[y][x] = oldSeatPlan[y][x]
switch oldSeatPlan[y][x] {
case 'L':
occupiedSeats, _ = checkArea(oldSeatPlan, y, x)
if occupiedSeats == 0 {
newSeatPlan[y][x] = '#'
somethingChanged = true
}
break
case '#':
occupiedSeats, _ = checkArea(oldSeatPlan, y, x)
if occupiedSeats >= 4 {
newSeatPlan[y][x] = 'L'
somethingChanged = true
}
break
case '.':
break
default:
fmt.Printf("Found a dodgy area at x:%d y:%d\n", x, y)
break
}
}
}
if debug {
fmt.Println("====== SEAT PLAN =====")
print2DArray(oldSeatPlan)
}
for y := 0; y < len(newSeatPlan); y++ {
for x := 0; x < len(newSeatPlan[0]); x++ {
oldSeatPlan[y][x] = newSeatPlan[y][x]
}
}
if debug {
fmt.Println("====== NEW SEAT PLAN =====")
print2DArray(oldSeatPlan)
}
return oldSeatPlan, somethingChanged
}
func howManyFilledSeats(filename string, part byte, debug bool) int {
var seatPlan [][]byte
var scratchSeatPlan [][]byte
puzzleInput, _ := readFile(filename)
seatPlan = make([][]byte, len(puzzleInput))
scratchSeatPlan = make([][]byte, len(puzzleInput))
for i := 0; i < len(puzzleInput); i++ {
seatPlan[i] = make([]byte, len(puzzleInput[0]))
scratchSeatPlan[i] = make([]byte, len(puzzleInput[0]))
}
setSeatPlan(puzzleInput, seatPlan)
if debug {
fmt.Println("====== START =====")
print2DArray(seatPlan)
}
var somethingChanged bool = true
for somethingChanged {
seatPlan, somethingChanged = applySeatingRules(seatPlan, scratchSeatPlan, debug)
}
return countOccupiedSeats(seatPlan)
}
// Main routine
func main() {
filenamePtr, execPart, debug, test := catchUserInput()
if test {
return
}
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else if execPart == 'a' {
fmt.Println("occupied seats:", howManyFilledSeats(filenamePtr, execPart, debug))
} else {
fmt.Println("occupied seats:", howManyFilledSeatsB(filenamePtr, execPart, debug))
}
}