-
Notifications
You must be signed in to change notification settings - Fork 0
/
part1.go
183 lines (139 loc) · 3.5 KB
/
part1.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
// To run this, yuo need to add a few blank lines to the input.
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
)
var numbers_regex = regexp.MustCompile(`\d+`)
// Capture groups give mapping ingredients (0 to 1)
var mapping_regex = regexp.MustCompile(`([a-z]+)-to-([a-z]+) map`)
// var MAXIMUM_NUMBER_OF_SEEDS = 100
type MappingRange struct {
start_from int
stop_from int
start_to int
stop_to int
width int
}
func (mapping *SeedMapping) FindMapping(number int) int {
for _, v := range mapping.ranges {
if number >= v.start_from && number <= v.stop_from {
return v.start_to + (number - v.start_from)
}
}
return number
}
type SeedMapping struct {
from string
to string
ranges []MappingRange
}
func parse_name_string(str string) (string, string) {
matches := mapping_regex.FindStringSubmatch(str)
return matches[1], matches[2]
}
func parse_mapping(strs []string) SeedMapping {
result := SeedMapping{
ranges: make([]MappingRange, 0),
}
for _, v := range strs {
if strings.Contains(v, ":") {
result.from, result.to = parse_name_string(v)
} else {
match_numbers := numbers_regex.FindAllString(strings.TrimSpace(v), -1)
to_start, _ := strconv.Atoi(match_numbers[0])
from_start, _ := strconv.Atoi(match_numbers[1])
length, _ := strconv.Atoi(match_numbers[2])
new_mapping_range := MappingRange{
start_from: from_start,
stop_from: from_start + length - 1,
start_to: to_start,
stop_to: to_start + length - 1,
width: length,
}
result.ranges = append(result.ranges, new_mapping_range)
}
}
return result
}
func make_hops(have string, want string, mappings []SeedMapping, number int) int {
// fmt.Println("Hopping: have ", have, " want ", want, " number ", number)
if have == want {
return number
}
// Find the correct mapping...
for _, v := range mappings {
if v.from == have {
new_number := v.FindMapping(number)
return make_hops(
v.to, want, mappings, new_number,
)
}
}
fmt.Println("FAILED TO FIND MAPPING FOR: ", have, want, number)
os.Exit(1)
return 1
}
func parse_seeds(str string) []int {
result := make([]int, 0)
for _, v := range numbers_regex.FindAllString(str, -1) {
integer, _ := strconv.Atoi(v)
result = append(result, integer)
}
return result
}
func main() {
DEBUG := true
scanner := bufio.NewScanner(os.Stdin)
mappings := make([]SeedMapping, 0)
seeds := make([]int, 0)
buffer := make([]string, 0)
for scanner.Scan() {
text := scanner.Text()
if strings.Contains(text, "seeds") {
seeds = parse_seeds(text)
if DEBUG {
fmt.Println("Seeds: ", seeds)
}
} else {
if len(text) == 0 {
if len(buffer) == 0 {
continue
}
if DEBUG {
fmt.Println("Buffer: ")
for _, v := range buffer {
fmt.Println(v)
}
}
this_mapping := parse_mapping(buffer)
if DEBUG {
fmt.Println("Output: ", this_mapping)
}
mappings = append(mappings, this_mapping)
buffer = make([]string, 0)
} else {
buffer = append(buffer, text)
}
}
}
if scanner.Err() != nil {
log.Fatal("No input provided.")
}
smallest_location := 1000000000000
for _, v := range seeds {
new_location := make_hops("seed", "location", mappings, v)
smallest_location = min(new_location, smallest_location)
if DEBUG {
fmt.Println("Seed: ", v)
fmt.Println("Maps to location: ", new_location)
fmt.Println("Smallest location so far: ", smallest_location)
}
}
fmt.Println("Smallest location: ", smallest_location)
}