-
Notifications
You must be signed in to change notification settings - Fork 0
/
05p2.go
57 lines (45 loc) · 1.23 KB
/
05p2.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
package main
import (
"fmt"
"strconv"
"strings"
"aoc2023/utils"
)
func D05P2() {
lines := utils.ReadLines("./inputs/05.txt")
seeds, ranges := parseAlmanacSeedsPartTwo(lines)
maps := parseAlmanacMaps(lines)
for _, almanacMap := range maps {
mappedSeeds := []int{}
mappedRanges := []int{}
for i, seed := range seeds {
newSeeds, newRanges := applyAlmanacMap(seed, ranges[i], almanacMap)
mappedSeeds = append(mappedSeeds, newSeeds...)
mappedRanges = append(mappedRanges, newRanges...)
}
seeds = mappedSeeds
ranges = mappedRanges
}
lowestLocationNumber := int(^uint(0) >> 1)
for _, seed := range seeds {
if seed < lowestLocationNumber {
lowestLocationNumber = seed
}
}
fmt.Printf("Lowest location number: %d\n", lowestLocationNumber)
}
func parseAlmanacSeedsPartTwo(lines []string) ([]int, []int) {
seedNumbers := []int{}
seedRanges := []int{}
seedListString := strings.Split(lines[0], ": ")[1]
for i, seedString := range strings.Split(seedListString, " ") {
if i%2 == 1 {
seedRange, _ := strconv.Atoi(seedString)
seedRanges = append(seedRanges, seedRange)
continue
}
seedNumber, _ := strconv.Atoi(seedString)
seedNumbers = append(seedNumbers, seedNumber)
}
return seedNumbers, seedRanges
}