generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.go
142 lines (115 loc) · 2.41 KB
/
solution.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
package day18p1
import (
"fmt"
"io"
"regexp"
"strconv"
"aoc/utils"
)
func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
re := regexp.MustCompile(`([RDLU])\s+(\d+)\s+\(#([0-9a-f]+)\)`)
grid := make(map[utils.Point]bool)
pos := utils.Point{}
grid[pos] = true
xmin, ymin, xmax, ymax := 0, 0, 0, 0
for _, ln := range lines {
sm := re.FindStringSubmatch(ln)
inst := Instruction{}
switch sm[1] {
case "U":
inst.Direction = utils.North
case "D":
inst.Direction = utils.South
case "R":
inst.Direction = utils.East
case "L":
inst.Direction = utils.West
default:
panic("Direction not good")
}
var err error
inst.Distance, err = strconv.Atoi(sm[2])
if err != nil {
utils.Check(err, "Unable to convert %s to int", sm[2])
}
inst.Color = sm[3]
// dig out trenches
for i := 0; i < inst.Distance; i++ {
pos = pos.Add(inst.Direction)
if pos.X > xmax {
xmax = pos.X
}
if pos.Y > ymax {
ymax = pos.Y
}
if pos.X < xmin {
xmin = pos.X
}
if pos.Y < ymin {
ymin = pos.Y
}
grid[pos] = true
}
}
if utils.Verbose {
printGrid(grid, xmin, ymin, xmax, ymax)
}
g := &Grid{grid, xmin, ymin, xmax, ymax}
bfs := utils.NewBFS[utils.Point]()
_, err := bfs.Run(g)
if err != utils.BFSNotFound {
panic("did not exhaust grid")
}
outside := len(bfs.Visited)
if utils.Verbose {
printGrid(bfs.Visited, xmin-1, ymin-1, xmax+1, ymax+1)
}
area := (xmax - xmin + 3) * (ymax - ymin + 3)
return area - outside
}
type Instruction struct {
Direction utils.Point
Distance int
Color string
}
func printGrid(grid map[utils.Point]bool, xmin, ymin, xmax, ymax int) {
for j := ymax; j >= ymin; j-- {
for i := xmin; i <= xmax; i++ {
switch grid[utils.Point{X: i, Y: j}] {
case true:
fmt.Printf("#")
case false:
fmt.Printf(".")
}
}
fmt.Printf("\n")
}
fmt.Printf("\n")
}
type Grid struct {
Edges map[utils.Point]bool
Xmin int
Ymin int
Xmax int
Ymax int
}
func (g *Grid) GetInitial() utils.Point {
return utils.Point{X: g.Xmin - 1, Y: g.Ymin - 1}
}
func (g *Grid) GetNeighbors(p utils.Point) []utils.Point {
ret := []utils.Point{}
for _, dir := range utils.Directions {
np := p.Add(dir)
if np.X > g.Xmax+1 || np.X < g.Xmin-1 || np.Y < g.Ymin-1 || np.Y > g.Ymax+1 {
continue
}
if !g.Edges[np] {
ret = append(ret, np)
}
}
return ret
}
func (g *Grid) IsFinal(p utils.Point) bool {
return false
}