-
Notifications
You must be signed in to change notification settings - Fork 1
/
15.go
152 lines (127 loc) · 2.63 KB
/
15.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
)
type ingredient struct {
capacity int
durability int
flavor int
texture int
calories int
}
func format(line string) ingredient {
// Sugar: capacity 3, durability 0, flavor 0, texture -3, calories 2
re := regexp.MustCompile("capacity (-*[0-9]+), durability (-*[0-9]+), flavor (-*[0-9]+), texture (-*[0-9]+), calories (-*[0-9]+)")
res := re.FindStringSubmatch(line)
if res == nil {
log.Fatal("Regex failed on input", line)
}
capacity, err := strconv.Atoi(res[1])
if err != nil {
log.Fatal(err)
}
durability, err := strconv.Atoi(res[2])
if err != nil {
log.Fatal(err)
}
flavor, err := strconv.Atoi(res[3])
if err != nil {
log.Fatal(err)
}
texture, err := strconv.Atoi(res[4])
if err != nil {
log.Fatal(err)
}
calories, err := strconv.Atoi(res[5])
if err != nil {
log.Fatal(err)
}
return ingredient{capacity: capacity, durability: durability, flavor: flavor, texture: texture, calories: calories}
}
func combinations(size int, max int) [][]int {
queue := [][]int{}
combos := [][]int{}
for i := 0; i <= max; i++ {
item := []int{i}
queue = append(queue, item)
}
if size == 1 {
return queue
}
for {
if len(queue) == 0 {
break
}
var item []int
item, queue = queue[0], queue[1:]
sum := 0
for _, v := range item {
sum += v
}
if len(item) == size-1 {
combos = append(combos, append(item, max-sum))
continue
}
for i := 0; i <= max-sum; i++ {
queue = append(queue, append(item, i))
}
}
return combos
}
func main() {
file, err := os.Open("input/15.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
var ingredients []ingredient
scanner := bufio.NewScanner(file)
for scanner.Scan() {
ingredients = append(ingredients, format(scanner.Text()))
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
combos := combinations(len(ingredients), 100)
best, best500 := 0, 0
for _, combo := range combos {
capacity := 0
durability := 0
flavor := 0
texture := 0
calories := 0
for i, qty := range combo {
capacity += ingredients[i].capacity * qty
durability += ingredients[i].durability * qty
flavor += ingredients[i].flavor * qty
texture += ingredients[i].texture * qty
calories += ingredients[i].calories * qty
}
if capacity < 0 {
continue
}
if durability < 0 {
continue
}
if flavor < 0 {
continue
}
if texture < 0 {
continue
}
recipe := capacity * durability * flavor * texture
if recipe > best {
best = recipe
}
if calories == 500 && recipe > best500 {
best500 = recipe
}
}
fmt.Println(best)
fmt.Println(best500)
}