-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (127 loc) · 2.65 KB
/
main.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
// -*- compile-command: "go run main.go ../example1.txt ../input.txt"; -*-
package main
import (
"flag"
"fmt"
"log"
"regexp"
"strings"
. "github.com/gmlewis/advent-of-code-2021/enum"
"github.com/gmlewis/advent-of-code-2021/must"
)
var logf = log.Printf
var printf = fmt.Printf
func main() {
flag.Parse()
Each(flag.Args(), process)
}
func process(filename string) {
logf("Processing %v ...", filename)
lines := must.ReadFileLines(filename)
puz := parsePuzzle(lines)
rowY := 10
if strings.Contains(filename, "input.txt") {
rowY = 2000000
}
solution := puz.noBeacons(rowY)
printf("Solution: %v\n", solution)
}
type keyT [2]int
func (k keyT) x() int { return k[0] }
func (k keyT) y() int { return k[1] }
type puzT struct {
xmin int
xmax int
ymin int
ymax int
grid map[keyT]rune
sensors map[keyT]*pairT // keyed by sensor location
}
type pairT struct {
beacon keyT
dx int // positive
dy int // positive
}
func (p *puzT) noBeacons(y int) int {
var impossibleBeacons int
// log.Printf("Searching from x=%v to x=%v", p.xmin, p.xmax)
for x := p.xmin; x <= p.xmax; x++ {
if !p.isBeaconPossible(x, y) {
impossibleBeacons++
}
}
return impossibleBeacons
}
func (p *puzT) isBeaconPossible(x, y int) bool {
if p.grid[keyT{x, y}] == 'B' {
return true
}
for s, pair := range p.sensors {
if !pair.isBeaconPossible(s, x, y) {
// log.Printf("beacon impossible at (%v,%v) because of sensor %v", x, y, s)
return false
}
}
return true
}
func (p *pairT) isBeaconPossible(sensor keyT, x, y int) bool {
dx := sensor.x() - x
if dx < 0 {
dx = -dx
}
dy := sensor.y() - y
if dy < 0 {
dy = -dy
}
possible := dx+dy > p.dx+p.dy
return possible
}
var lineRE = regexp.MustCompile(`=(-?\d+)`)
func parsePuzzle(lines []string) *puzT {
p := &puzT{grid: map[keyT]rune{}, sensors: map[keyT]*pairT{}}
for _, line := range lines {
m := lineRE.FindAllStringSubmatch(line, -1)
if len(m) != 4 {
log.Fatalf("Unable to parse line: %v, %#v", line, m)
}
sx := must.Atoi(m[0][1])
sy := must.Atoi(m[1][1])
sensor := keyT{sx, sy}
bx := must.Atoi(m[2][1])
by := must.Atoi(m[3][1])
beacon := keyT{bx, by}
dx, dy := sx-bx, sy-by
if dx < 0 {
dx = -dx
}
if dy < 0 {
dy = -dy
}
p.grid[sensor] = 'S'
p.grid[beacon] = 'B'
p.updateBounds(sx-dx, sy-dy)
p.updateBounds(sx+dx, sy+dy)
p.updateBounds(bx-dx, by-dy)
p.updateBounds(bx+dx, by+dy)
p.sensors[sensor] = &pairT{
beacon: beacon,
dx: dx,
dy: dy,
}
}
return p
}
func (p *puzT) updateBounds(x, y int) {
if x < p.xmin {
p.xmin = x
}
if x > p.xmax {
p.xmax = x
}
if y < p.ymin {
p.ymin = y
}
if y > p.ymax {
p.ymax = y
}
}