-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11b.go
More file actions
124 lines (102 loc) · 2.78 KB
/
day11b.go
File metadata and controls
124 lines (102 loc) · 2.78 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
package main
import (
"fmt"
)
// empty seats that see no occupied seats become occupied
// five or more visible occupied seats for an occupied seat to become vacant
// seat matching no rule don't change
type directionStruct struct {
xChange int
yChange int
}
func checkAreaB(tempArea [][]byte, yCoord int, xCoord int, debug bool) int {
var directions = []directionStruct{
{-1, -1}, {0, -1}, {1, -1},
{-1, 0}, {1, 0},
{-1, 1}, {0, 1}, {1, 1}}
var occupiedSeats int = 0
var checkX, checkY int
var keepLooping bool
for i := 0; i < len(directions); i++ {
checkX = xCoord
checkY = yCoord
keepLooping = true
for keepLooping {
checkX += directions[i].xChange
checkY += directions[i].yChange
if checkX < 0 || checkX > len(tempArea[yCoord])-1 {
keepLooping = false
}
if checkY < 0 || checkY > len(tempArea)-1 {
keepLooping = false
}
if keepLooping {
if tempArea[checkY][checkX] == '#' {
occupiedSeats++
keepLooping = false
} else if tempArea[checkY][checkX] == 'L' {
keepLooping = false
}
}
}
}
return occupiedSeats
}
func applySeatingRulesB(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 = checkAreaB(oldSeatPlan, y, x, debug)
if occupiedSeats == 0 {
newSeatPlan[y][x] = '#'
somethingChanged = true
}
break
case '#':
occupiedSeats = checkAreaB(oldSeatPlan, y, x, debug)
if occupiedSeats >= 5 {
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]
}
}
return oldSeatPlan, somethingChanged
}
func howManyFilledSeatsB(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)
var somethingChanged bool = true
for somethingChanged {
seatPlan, somethingChanged = applySeatingRulesB(seatPlan, scratchSeatPlan, debug)
}
return countOccupiedSeats(seatPlan)
}