/
05.go
148 lines (135 loc) · 2.8 KB
/
05.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
package main
import (
"slices"
"strings"
)
func init() {
addSolutions(5, problem5)
}
func problem5(ctx *problemContext) {
var a almanac
var curMap []seedMapRange
scanner := ctx.scanner()
for scanner.scan() {
line := scanner.text()
switch {
case len(a.seeds) == 0:
s := strings.TrimPrefix(line, "seeds: ")
for _, sn := range strings.Fields(s) {
a.seeds = append(a.seeds, parseInt(sn))
}
case line == "":
case strings.Contains(line, "map:"):
if len(curMap) > 0 {
a.maps = append(a.maps, curMap)
}
curMap = nil
default:
var ns []int64
for _, sn := range strings.Fields(line) {
ns = append(ns, parseInt(sn))
}
if len(ns) != 3 {
panic("bad range")
}
rnge := seedMapRange{dest: ns[0], source: ns[1], len: ns[2]}
curMap = append(curMap, rnge)
}
}
if len(curMap) > 0 {
a.maps = append(a.maps, curMap)
}
a.sortMaps()
ctx.reportLoad()
ctx.reportPart1(a.solve1())
ctx.reportPart2(a.solve2())
}
type almanac struct {
seeds []int64
maps [][]seedMapRange
}
type seedMapRange struct {
dest int64
source int64
len int64
}
func (a *almanac) sortMaps() {
for _, m := range a.maps {
slices.SortFunc(m, func(r0, r1 seedMapRange) int {
return int(r0.source - r1.source)
})
}
}
func (a *almanac) solve1() int64 {
best := int64(-1)
for _, seed := range a.seeds {
if loc := a.getLocation(seed); best < 0 || loc < best {
best = loc
}
}
return best
}
func (a *almanac) getLocation(seed int64) int64 {
n := seed
for _, m := range a.maps {
n = convertSeed(n, m)
}
return n
}
func convertSeed(source int64, m []seedMapRange) int64 {
for _, r := range m {
if source < r.source {
return source
}
if source < r.source+r.len {
return r.dest + (source - r.source)
}
}
return source
}
func (a *almanac) solve2() int64 {
best := int64(-1)
for i := 0; i < len(a.seeds); i += 2 {
start := a.seeds[i]
end := a.seeds[i] + a.seeds[i+1]
best1 := bestLocationForRange(a.maps, start, end)
if best < 0 || best1 < best {
best = best1
}
}
return best
}
func bestLocationForRange(maps [][]seedMapRange, start, end int64) int64 {
if len(maps) == 0 {
return start
}
best := int64(-1)
updateBest := func(candidate int64) {
if best < 0 || candidate < best {
best = candidate
}
}
m := maps[0]
maps = maps[1:]
for start < end {
if len(m) == 0 {
updateBest(bestLocationForRange(maps, start, end))
break
}
if start < m[0].source {
next := min(end, m[0].source)
updateBest(bestLocationForRange(maps, start, next))
start = next
}
sourceEnd := m[0].source + m[0].len
if start < sourceEnd {
start1 := m[0].dest + (start - m[0].source)
next := min(end, sourceEnd)
end1 := m[0].dest + (next - m[0].source)
updateBest(bestLocationForRange(maps, start1, end1))
start = next
}
m = m[1:]
}
return best
}