-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05-1.go
70 lines (58 loc) · 1.29 KB
/
day05-1.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
package main
import (
"fmt"
"regexp"
"slices"
)
var (
day5SeedsRe = regexp.MustCompile(`^seeds: (.+)$`)
)
type day5Range struct {
dst, src, len int
}
// Returns value and true if seed in range, otherwise 0 and false.
func (r day5Range) mapSeed(seed int) (int, bool) {
return r.dst + (seed - r.src), seed >= r.src && seed < r.src+r.len
}
func day5part1(filename string) (string, error) {
var seeds []int
var ranges []day5Range
if err := forLineError(filename, func(line string) error {
if len(seeds) == 0 {
matches := day5SeedsRe.FindStringSubmatch(line)
seeds = parseNumberLine(matches[1])
return nil
}
if line == "" {
if len(ranges) == 0 {
return nil
}
for i, s := range seeds {
seeds[i] = day5RunMap(s, ranges)
}
return nil
}
if line[len(line)-1:] == ":" {
ranges = nil
return nil
}
p := parseNumberLine(line)
ranges = append(ranges, day5Range{dst: p[0], src: p[1], len: p[2]})
return nil
}); err != nil {
return "", err
}
// Run one final time after the last line.
for i, s := range seeds {
seeds[i] = day5RunMap(s, ranges)
}
return fmt.Sprint(slices.Min(seeds)), nil
}
func day5RunMap(seed int, ranges []day5Range) int {
for _, r := range ranges {
if mapped, ok := r.mapSeed(seed); ok {
return mapped
}
}
return seed
}