-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_part2.go
166 lines (140 loc) Β· 3.26 KB
/
main_part2.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
package main
import (
_ "embed"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
)
type Rule struct {
dst int
src int
length int
}
//go:embed input.txt
var input string
/**
* Day 4: Scratchcards - Part 2
* url: https://adventofcode.com/2023/day/5
*/
func main() {
lines := strings.Split(strings.TrimSpace(input), "\n")
current := ""
seeds := []int{}
rules := map[string][]*Rule{}
for _, s := range lines {
s = strings.TrimSpace(s)
if strings.Contains(s, ":") {
current = strings.Replace(strings.Split(s, ":")[0], " map", "", -1)
if current == "seeds" {
re := regexp.MustCompile(`\d+`)
numbers := []int{}
for _, val := range re.FindAllStringSubmatch(strings.Split(s, ":")[1], -1) {
num, _ := strconv.Atoi(val[0])
numbers = append(numbers, num)
}
seeds = numbers
continue
}
}
rule := parseNumbers(s)
if rule != nil {
rules[current] = append(rules[current], rule)
}
}
min := 0
locations := []int{}
for i := 0; i < len(seeds); i += 2 {
start := seeds[i]
length := seeds[i+1]
resolveMatchingInterval(rules, "seed", start, start+length, &locations)
}
for _, val := range locations {
if min == 0 || val < min {
min = val
}
}
fmt.Println("Part 2 = ", min)
}
/*
* Recursive method which for a given range of id and category, split in different interval of ids and categories.
*/
func resolveMatchingInterval(rules map[string][]*Rule, name string, start int, end int, locations *[]int) {
r := filterValidRules(rules, name, start, end)
// Exit condition, we found a location range (we only take the lowest one)
if name == "location" {
*locations = append(*locations, start)
}
// We split the range based on the rules
for key, ranges := range r {
sort.Slice(ranges, func(i, j int) bool {
return ranges[i].start < ranges[j].start
})
s := start
for _, ran := range ranges {
rule := ran.rule
maxStart := max(s, rule.src)
minEnd := min(end, rule.src+rule.length)
if s < maxStart {
resolveMatchingInterval(rules, key, s, maxStart-1, locations)
}
resolveMatchingInterval(rules, key, maxStart+ran.offset, minEnd+ran.offset, locations)
s = minEnd
}
if s < end {
resolveMatchingInterval(rules, key, s, end, locations)
}
}
}
type Range struct {
start int
end int
rule *Rule
offset int
}
func filterValidRules(rules map[string][]*Rule, name string, start int, end int) map[string][]Range {
res := map[string][]Range{}
for key, ranges := range rules {
if !strings.Contains(key, name+"-") {
continue
}
keySplit := strings.Split(key, "-")
to := keySplit[2]
if res[to] == nil {
res[to] = []Range{}
}
for _, rule := range ranges {
a1 := start
b1 := end
a2 := rule.src
b2 := rule.src + rule.length
if b2 < a1 || a2 > b1 {
continue
}
res[to] = append(res[to], Range{
start: max(a1, a2),
end: min(b1, b2) - 1,
rule: rule,
offset: rule.dst - rule.src,
})
}
}
return res
}
func parseNumbers(s string) *Rule {
re := regexp.MustCompile(`\d+`)
numbers := []int{}
for _, val := range re.FindAllStringSubmatch(s, -1) {
num, _ := strconv.Atoi(val[0])
numbers = append(numbers, num)
}
if len(numbers) != 3 {
return nil
}
return &Rule{
dst: numbers[0],
src: numbers[1],
length: numbers[2],
}
}