-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
221 lines (193 loc) · 4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"context"
"io"
"math"
"sort"
"sync"
aoc "github.com/teivah/advent-of-code"
"golang.org/x/sync/errgroup"
)
func fs1(input io.Reader) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
seeds := parseSeeds(groups[0][0])
var maps []Map
for i := 1; i < len(groups); i++ {
maps = append(maps, parseMap(groups[i]))
}
lowest := math.MaxInt
for _, v := range seeds {
for _, m := range maps {
if dst, contains := m.get(v); contains {
v = dst
}
}
lowest = min(lowest, v)
}
return lowest
}
func parseSeeds(line string) []int {
line = aoc.Substring(line, ": ")
del := aoc.NewDelimiter(line, " ")
return del.GetInts()
}
type Range struct {
// Included
from int
// Included
to int
transfo int
}
type Map struct {
ranges []Range
}
func (m Map) get(v int) (int, bool) {
l := 0
r := len(m.ranges) - 1
for l <= r {
mid := l + (r-l)/2
rng := m.ranges[mid]
if v > rng.to {
l = mid + 1
} else if v < rng.from {
r = mid - 1
} else {
return v + rng.transfo, true
}
}
return 0, false
}
func parseMap(lines []string) Map {
var ranges []Range
for i := 0; i < len(lines); i++ {
if i == 0 {
// Discard header
continue
}
del := aoc.NewDelimiter(lines[i], " ")
ints := del.GetInts()
dstRange := ints[0]
srcRange := ints[1]
rangeLength := ints[2]
ranges = append(ranges, Range{
from: srcRange,
to: srcRange + rangeLength - 1,
transfo: dstRange - srcRange,
})
}
sort.Slice(ranges, func(i, j int) bool {
return ranges[i].from < ranges[j].from
})
return Map{ranges: ranges}
}
// See fs2 for the final version
func fs2NonFinal(input io.Reader) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
seeds := parseSeedsRange(groups[0][0])
var maps []Map
for i := 1; i < len(groups); i++ {
maps = append(maps, parseMap(groups[i]))
}
mu := sync.Mutex{}
lowest := math.MaxInt
wg, _ := errgroup.WithContext(context.Background())
for _, seed := range seeds {
seed := seed
wg.Go(func() error {
local := math.MaxInt
for i := 0; i < seed[1]; i++ {
local = min(local, transform(seed[0]+i, maps))
}
mu.Lock()
lowest = min(lowest, local)
mu.Unlock()
return nil
})
}
_ = wg.Wait()
return lowest
}
func transform(v int, maps []Map) int {
for _, m := range maps {
if dst, contains := m.get(v); contains {
v = dst
}
}
return v
}
func parseSeedsRange(line string) [][2]int {
line = aoc.Substring(line, ": ")
del := aoc.NewDelimiter(line, " ")
ints := del.GetInts()
var res [][2]int
for i := 0; i < len(ints); i += 2 {
res = append(res, [2]int{ints[i], ints[i+1]})
}
return res
}
func fs2(input io.Reader) int {
groups := aoc.StringGroups(aoc.ReaderToStrings(input))
seeds := parseSeedsRange(groups[0][0])
sort.Slice(seeds, func(i, j int) bool {
return seeds[i][0] < seeds[j][0]
})
var maps []Map
for i := 1; i < len(groups); i++ {
maps = append(maps, parseRevMap(groups[i]))
}
for loc := 0; loc < math.MaxInt; loc++ {
v := transformRev(loc, maps)
if isWithinSeedRange(v, seeds) {
return loc
}
}
return -1
}
func transformRev(v int, maps []Map) int {
for i := len(maps) - 1; i >= 0; i-- {
m := maps[i]
if dst, contains := m.get(v); contains {
v = dst
}
}
return v
}
func parseRevMap(lines []string) Map {
var ranges []Range
for i := 0; i < len(lines); i++ {
if i == 0 {
// Discard header
continue
}
del := aoc.NewDelimiter(lines[i], " ")
ints := del.GetInts()
dstRange := ints[0]
srcRange := ints[1]
rangeLength := ints[2]
ranges = append(ranges, Range{
from: dstRange,
to: dstRange + rangeLength - 1,
transfo: srcRange - dstRange,
})
}
sort.Slice(ranges, func(i, j int) bool {
return ranges[i].from < ranges[j].from
})
return Map{ranges: ranges}
}
func isWithinSeedRange(n int, seeds [][2]int) bool {
l := 0
r := len(seeds) - 1
for l <= r {
mid := l + (r-l)/2
rng := seeds[mid]
if n < rng[0] {
r = mid - 1
} else if n > rng[0]+rng[1]-1 {
l = mid + 1
} else {
return true
}
}
return false
}