-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDayEighteen.go
153 lines (113 loc) · 2.41 KB
/
DayEighteen.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
package adventofcode2015
import (
"fmt"
"strings"
)
func parseMatrix(input string) [][]bool {
lines := strings.Split(input, "\n")
matrix := make([][]bool, len(lines))
for i := 0; i < len(lines); i++ {
row := make([]bool, len(lines[i]))
for j := 0; j < len(lines[i]); j++ {
if lines[i][j] == '#' {
row[j] = true
}
}
matrix[i] = row
}
return matrix
}
func animateMatrix(inputMatrix [][]bool) [][]bool {
outputMatrix := make([][]bool, len(inputMatrix))
for y := 0; y < len(inputMatrix); y++ {
outputRow := make([]bool, len(inputMatrix[y]))
for x := 0; x < len(inputMatrix[y]); x++ {
outputRow[x] = getNextCellState(x, y, inputMatrix)
}
outputMatrix[y] = outputRow
}
return outputMatrix
}
func getNextCellState(x, y int, matrix [][]bool) bool {
litCount := 0
//Row Above
if y > 0 {
//Above left
if x > 0 && matrix[y-1][x-1] {
litCount++
}
//above center
if matrix[y-1][x] {
litCount++
}
//above right
if x < len(matrix[y])-1 && matrix[y-1][x+1] {
litCount++
}
}
//Same Row
//Center left
if x > 0 && matrix[y][x-1] {
litCount++
}
//center right
if x < len(matrix[y])-1 && matrix[y][x+1] {
litCount++
}
//Row Below
if y < len(matrix)-1 {
//below left
if x > 0 && matrix[y+1][x-1] {
litCount++
}
//below center
if matrix[y+1][x] {
litCount++
}
//below right
if x < len(matrix[y])-1 && matrix[y+1][x+1] {
litCount++
}
}
if matrix[y][x] {
return litCount == 2 || litCount == 3
} else {
return litCount == 3
}
}
func getLitCount(matrix [][]bool) int {
litCount := 0
for y := 0; y < len(matrix); y++ {
for x := 0; x < len(matrix[y]); x++ {
if matrix[y][x] {
litCount++
}
}
}
return litCount
}
func DayEighteenExample() {
fmt.Println("Day 18 - Example")
}
func DayEighteenPartOne() {
fmt.Println("Day 18 - Part One")
input := ReadFile("day18-input.txt")
matrix := parseMatrix(input)
for i := 0; i < 100; i++ {
matrix = animateMatrix(matrix)
}
fmt.Println("Lit Count:", getLitCount(matrix))
}
func DayEighteenPartTwo() {
fmt.Println("Day 18 - Part Two")
input := ReadFile("day18-input.txt")
matrix := parseMatrix(input)
for i := 0; i < 100; i++ {
matrix = animateMatrix(matrix)
matrix[0][0] = true
matrix[0][len(matrix[0])-1] = true
matrix[len(matrix)-1][0] = true
matrix[len(matrix)-1][len(matrix[len(matrix)-1])-1] = true
}
fmt.Println("Lit Count:", getLitCount(matrix))
}