-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathday18.go
140 lines (124 loc) · 2.41 KB
/
day18.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
package main
import (
"bufio"
"flag"
"fmt"
"os"
)
var inputFile = flag.String("inputFile", "inputs/day18.input", "Relative file path to use as input.")
var rounds = flag.Int("rounds", 10, "The number of rounds to simulate.")
type Coord struct {
X, Y int
}
func (c Coord) Adjacent(t [50][50]int) (int, int) {
trees, yards := 0, 0
for xoff := -1; xoff <= 1; xoff++ {
for yoff := -1; yoff <= 1; yoff++ {
if xoff == 0 && yoff == 0 {
continue
}
y := c.Y + yoff
x := c.X + xoff
if y >= 50 || y < 0 || x >= 50 || x < 0 {
continue
}
if t[y][x] == 1 {
trees++
} else if t[y][x] == 2 {
yards++
}
}
}
return trees, yards
}
func main() {
flag.Parse()
f, err := os.Open(*inputFile)
if err != nil {
return
}
defer f.Close()
// 0 == empty
// 1 == lumberyard
// 2 = trees
var tiles [50][50]int
reader := bufio.NewReader(f)
for y := 0; ; y++ {
l, err := reader.ReadString('\n')
if err != nil || len(l) == 0 {
break
}
l = l[:len(l)-1]
for x, c := range l {
switch c {
case '.':
tiles[y][x] = 0
case '|':
tiles[y][x] = 1
case '#':
tiles[y][x] = 2
}
}
}
// Look for cycles.
seen := make(map[[50][50]int]int)
loopLen := 0
loopStart := 0
for r := 0; r < *rounds; r++ {
var newTiles [50][50]int
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
trees, yards := Coord{x, y}.Adjacent(tiles)
switch tiles[y][x] {
case 0:
if trees >= 3 {
newTiles[y][x] = 1
} else {
newTiles[y][x] = 0
}
case 1:
if yards >= 3 {
newTiles[y][x] = 2
} else {
newTiles[y][x] = 1
}
case 2:
if trees >= 1 && yards >= 1 {
newTiles[y][x] = 2
} else {
newTiles[y][x] = 0
}
}
}
}
if seen[newTiles] != 0 {
loopStart = seen[newTiles]
loopLen = r + 1 - loopStart
fmt.Printf("Loop detected: %d to %d.\n", loopStart, r+1)
break
}
seen[newTiles] = r + 1
tiles = newTiles
}
if loopStart != 0 && loopLen != 0 {
state := loopStart + ((*rounds - loopStart) % loopLen)
fmt.Printf("Looking for state %d\n", state)
for k, v := range seen {
if v == state {
tiles = k
break
}
}
}
trees, yards := 0, 0
for y := 0; y < 50; y++ {
for x := 0; x < 50; x++ {
if tiles[y][x] == 1 {
trees++
} else if tiles[y][x] == 2 {
yards++
}
}
}
fmt.Printf("Found %d yards and %d trees for a result of %d.\n", yards, trees, yards*trees)
}