-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.go
71 lines (57 loc) · 1.47 KB
/
day06.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
package main
import (
_ "embed"
"fmt"
"sort"
"strconv"
"strings"
)
//go:embed input.txt
var input string
func parseNumbers(line string) []int {
var numbers []int
for _, s := range strings.Fields(line) {
n, _ := strconv.Atoi(s)
numbers = append(numbers, n)
}
return numbers
}
func parseNumber(line string) int {
n, _ := strconv.Atoi(strings.Join(strings.Fields(line), ""))
return n
}
func greaterThanDistance(limit, distance int) func(int) bool {
return func(n int) bool {
return (limit-n)*n > distance
}
}
func lessEqualThanDistance(limit, distance int) func(int) bool {
return func(n int) bool {
return (limit-n)*n <= distance
}
}
func part1(input string) int {
lines := strings.Split(input, "\n")
limits := parseNumbers(lines[0][11:])
distances := parseNumbers(lines[1][11:])
ans := 1
for i := 0; i < len(limits); i++ {
start := sort.Search(limits[i], greaterThanDistance(limits[i], distances[i]))
end := sort.Search(limits[i], lessEqualThanDistance(limits[i], distances[i]))
ans *= end - start
}
return ans
}
func part2(input string) int {
lines := strings.Split(input, "\n")
limit := parseNumber(lines[0][11:])
distance := parseNumber(lines[1][11:])
start := sort.Search(limit, greaterThanDistance(limit, distance))
end := sort.Search(limit, lessEqualThanDistance(limit, distance))
return end - start
}
func main() {
fmt.Println("--- 2023 day 06 answer ---")
fmt.Println("part 1:\t", part1(input))
fmt.Println("part 2:\t", part2(input))
}