summaryrefslogtreecommitdiffstats
path: root/aoc23/day05_test.go
blob: 4b07a76e46d751cd9a187e9c627ff40d070cdd92 (plain) (blame)
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
package aoc23

import (
	"fmt"
	"io"
	"math"
	"strings"
	"testing"
)

type Almanac struct {
	seeds    []Range
	mappings [][]Mapping
}

func (a *Almanac) Resolve(n int) int {
	for _, rs := range a.mappings {
		for _, r := range rs {
			if found, ok := r.Target(n); ok {
				n = found
				break
			}
		}
	}
	return n
}

type Mapping struct {
	src  int
	dst  int
	size int
}

func (r Mapping) Target(n int) (int, bool) {
	if n >= r.src && n <= r.src+r.size {
		return r.dst + (n - r.src), true
	}
	return 0, false
}

type Range struct{ start, end int }

func (r Range) Each(f func(int)) {
	for i := r.start; i <= r.end; i++ {
		f(i)
	}
}

func parseMapping(s string) Mapping {
	parts := strings.Fields(s)
	if got, want := len(parts), 3; got != want {
		panic(fmt.Sprintf("got %d fields in %q, want %d", got, s, want))
	}
	ints := transform(parts, atoi)
	return Mapping{src: ints[1], dst: ints[0], size: ints[2]}
}

func parseAlmanac(r io.Reader, seedRange bool) Almanac {
	var (
		seeds    []Range
		mappings [][]Mapping
	)
	mappingIdx := -1
	parseLines(r, func(line string) string {
		if strings.HasPrefix(line, "seeds:") {
			parts := strings.Fields(line)
			ints := transform(parts[1:], atoi)
			if seedRange {
				for i := 0; i < len(ints); i += 2 {
					start := ints[i]
					end := start + ints[i+1]
					seeds = append(seeds, Range{start, end})
				}
			} else {
				for _, seed := range ints {
					seeds = append(seeds, Range{seed, seed})
				}
			}
		} else if strings.HasSuffix(line, "map:") {
			mappings = append(mappings, []Mapping{})
			mappingIdx++
		} else if mappingIdx > -1 && line != "" {
			mappings[mappingIdx] = append(mappings[mappingIdx], parseMapping(line))
		}
		return ""
	})
	return Almanac{seeds, mappings}
}

func locationNumber(r io.Reader, seedRange bool) int {
	a := parseAlmanac(r, seedRange)
	best := math.MaxInt
	for _, seed := range a.seeds {
		seed.Each(func(n int) {
			best = min(best, a.Resolve(n))
		})
	}
	return best
}

func TestDay05(t *testing.T) {
	example := `
seeds: 79 14 55 13

seed-to-soil map:
50 98 2
52 50 48

soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15

fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4

water-to-light map:
88 18 7
18 25 70

light-to-temperature map:
45 77 23
81 45 19
68 64 13

temperature-to-humidity map:
0 69 1
1 0 69

humidity-to-location map:
60 56 37
56 93 4
`
	assert(t, 35, run(partial(locationNumber, false), inputString(example)))
	assert(t, 424490994, run(partial(locationNumber, false), inputFile(5)))

	assert(t, 46, run(partial(locationNumber, true), inputString(example)))
	assert(t, 15290096, run(partial(locationNumber, true), inputFile(5))) // Correct, but terribly slow
}