-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
167 lines (129 loc) · 3.67 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"github.com/danvolchek/AdventOfCode/lib"
"golang.org/x/exp/slices"
)
type Pos struct {
x, y int
}
type Reading struct {
Sensor Pos
ClosestBeacon Pos
}
func parse(line string) Reading {
nums := lib.Ints(line)
return Reading{
Sensor: Pos{
x: nums[0],
y: nums[1],
},
ClosestBeacon: Pos{
x: nums[2],
y: nums[3],
},
}
}
func dist(a, b Pos) int {
return lib.Abs(a.x-b.x) + lib.Abs(a.y-b.y)
}
func coverageInRow(source Pos, maxD int, targRow int) (int, int, bool) {
distToRow := dist(source, Pos{x: source.x, y: targRow})
if distToRow > maxD {
return 0, 0, false
}
remaining := maxD - distToRow
return source.x - remaining, source.x + remaining, true
}
var maxRow int
func solve(lines []Reading) int {
var covered []func(targRow int) (int, int, bool)
for _, line := range lines {
d := dist(line.Sensor, line.ClosestBeacon)
q := line.Sensor
covered = append(covered, func(targRow int) (int, int, bool) {
return coverageInRow(q, d, targRow)
})
}
//s := time.Now()
for y := 0; y <= maxRow; y++ {
rc := coverageForRow(covered, y)
//fmt.Println(rc)
rc = simplify(rc)
//fmt.Println(rc, sz(rc, 0, maxRow))
//fmt.Println("to find right row", time.Now().Sub(s))
//if sz(rc, 0, maxRow) == maxRow+1 {
// continue
//}
// faster than size check, always true?
if len(rc) != 2 {
continue
}
slices.SortFunc(rc, func(r1, r2 rangey) bool {
return r1.s < r2.s
})
return (rc[0].e+1)*4000000 + y
/*for i, rr := range rc[1:] {
if rc[i].e != rr.s && rc[i].e < maxRow && rc[i].e >= -1 {
fmt.Println(y)
return (rc[i].e+1)*4000000 + y
}
}*/
}
panic("not found")
}
func (r rangey) intersect(o rangey) bool {
return (r.e >= o.s && r.s <= o.e) || (o.e >= r.s && o.s <= r.e)
}
func simplify(ranges []rangey) []rangey {
for start := 0; start < len(ranges)-1; start++ {
if len(ranges) == 1 {
return ranges
}
toMerge := ranges[start]
for i := start + 1; i < len(ranges); i++ {
if toMerge.intersect(ranges[i]) {
//fmt.Println("intersect", toMerge, ranges[i])
newR := rangey{lib.Min(toMerge.s, ranges[i].s), lib.Max(toMerge.e, ranges[i].e)}
ranges = lib.Remove(ranges, i)
ranges[start] = newR
start = -1
break
}
}
}
return ranges
}
func sz(ranges []rangey, min, max int) int {
return lib.SumSlice(lib.Map(ranges, func(r rangey) int {
s, e := r.s, r.e
if s < min {
s = min
}
if e > max {
e = max
}
return e - s + 1
}))
}
type rangey struct{ s, e int }
func coverageForRow(covered []func(targRow int) (int, int, bool), row int) []rangey {
var result []rangey
for _, c := range covered {
minX, maxX, ok := c(row)
if !ok {
continue
}
result = append(result, rangey{minX, maxX})
}
return result
}
func main() {
solver := lib.Solver[[]Reading, int]{
ParseF: lib.ParseLine(parse),
SolveF: solve,
}
maxRow = 20
solver.Expect("Sensor at x=2, y=18: closest beacon is at x=-2, y=15\nSensor at x=9, y=16: closest beacon is at x=10, y=16\nSensor at x=13, y=2: closest beacon is at x=15, y=3\nSensor at x=12, y=14: closest beacon is at x=10, y=16\nSensor at x=10, y=20: closest beacon is at x=10, y=16\nSensor at x=14, y=17: closest beacon is at x=10, y=16\nSensor at x=8, y=7: closest beacon is at x=2, y=10\nSensor at x=2, y=0: closest beacon is at x=2, y=10\nSensor at x=0, y=11: closest beacon is at x=2, y=10\nSensor at x=20, y=14: closest beacon is at x=25, y=17\nSensor at x=17, y=20: closest beacon is at x=21, y=22\nSensor at x=16, y=7: closest beacon is at x=15, y=3\nSensor at x=14, y=3: closest beacon is at x=15, y=3\nSensor at x=20, y=1: closest beacon is at x=15, y=3", 56000011)
maxRow = 4000000
solver.Verify(11016575214126)
}