-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (71 loc) · 1.98 KB
/
main.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
package main
import (
"regexp"
"github.com/danvolchek/AdventOfCode/lib"
)
type ingredient struct {
name string
capacity, durability, flavor, texture, calories int
}
var parseRegExp = regexp.MustCompile(`(.*): capacity (-?\d+), durability (-?\d+), flavor (-?\d+), texture (-?\d+), calories (-?\d+)`)
func parse(matches []string) ingredient {
return ingredient{
name: matches[0],
capacity: lib.Atoi(matches[1]),
durability: lib.Atoi(matches[2]),
flavor: lib.Atoi(matches[3]),
texture: lib.Atoi(matches[4]),
calories: lib.Atoi(matches[5]),
}
}
func score(amounts []int, ingredients []ingredient) int {
capacity, durability, flavor, texture := 0, 0, 0, 0
for i := 0; i < len(ingredients); i++ {
capacity += amounts[i] * ingredients[i].capacity
durability += amounts[i] * ingredients[i].durability
flavor += amounts[i] * ingredients[i].flavor
texture += amounts[i] * ingredients[i].texture
}
if capacity < 0 || durability < 0 || flavor < 0 || texture < 0 {
return 0
}
total := capacity * durability * flavor * texture
return lib.Max(total, 0)
}
func increment(amounts []int) bool {
for index := len(amounts) - 1; index > -1; index-- {
switch amounts[index] {
case 100:
amounts[index] = 0
default:
amounts[index] += 1
}
if amounts[index] != 0 {
return true
}
}
return false
}
func solve(ingredients []ingredient) int {
max := 0
amounts := make([]int, len(ingredients))
for increment(amounts) {
sum := 0
for _, v := range amounts {
sum += v
}
if sum != 100 {
continue
}
max = lib.Max(max, score(amounts, ingredients))
}
return max
}
func main() {
solver := lib.Solver[[]ingredient, int]{
ParseF: lib.ParseLine(lib.ParseRegexp(parseRegExp, parse)),
SolveF: solve,
}
solver.Expect("Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8\nCinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3", 62842880)
solver.Verify(18965440)
}