-
Notifications
You must be signed in to change notification settings - Fork 0
/
day15_part2.go
101 lines (84 loc) · 2.2 KB
/
day15_part2.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
package day15_part2
import (
"strings"
"GoAdventOfCode/util"
)
type Ingredient struct {
Cap int
Dur int
Fla int
Tex int
Cal int
}
func getAnswerForTwoIngredients(lines []string) int {
list := make([]*Ingredient, 0)
score := 0
for _, line := range lines {
in := getIngredient(line)
list = append(list, in)
}
m := 100
for a := 0; a <= m; a++ {
for b := 0; b <= m-a; b++ {
cal := a*list[0].Cal + b*list[1].Cal
if cal == 500 {
cap := a*list[0].Cap + b*list[1].Cap
dur := a*list[0].Dur + b*list[1].Dur
fla := a*list[0].Fla + b*list[1].Fla
tex := a*list[0].Tex + b*list[1].Tex
if cap > 0 && dur > 0 && fla > 0 && tex > 0 {
total := cap * dur * fla * tex
score = max(score, total)
}
}
}
}
return score
}
func getAnswerForFourIngredients(lines []string) int {
list := make([]*Ingredient, 0)
score := 0
for _, line := range lines {
in := getIngredient(line)
list = append(list, in)
}
m := 100
for a := 0; a <= m; a++ {
for b := 0; b <= m; b++ {
for c := 0; c <= m; c++ {
for d := 0; d <= m; d++ {
cal := a*list[0].Cal + b*list[1].Cal + c*list[2].Cal + d*list[3].Cal
if cal == 500 {
cap := a*list[0].Cap + b*list[1].Cap + c*list[2].Cap + d*list[3].Cap
dur := a*list[0].Dur + b*list[1].Dur + c*list[2].Dur + d*list[3].Dur
fla := a*list[0].Fla + b*list[1].Fla + c*list[2].Fla + d*list[3].Fla
tex := a*list[0].Tex + b*list[1].Tex + c*list[2].Tex + d*list[3].Tex
if a+b+c+d == 100 && cap > 0 && dur > 0 && fla > 0 && tex > 0 {
total := cap * dur * fla * tex
if total > score {
score = total
}
}
}
}
}
}
}
return score
}
func getIngredient(line string) *Ingredient {
parts := strings.Fields(line)
capacity := util.ConvertStringToInt(strings.TrimSuffix(parts[2], ","))
durability := util.ConvertStringToInt(strings.TrimSuffix(parts[4], ","))
flavor := util.ConvertStringToInt(strings.TrimSuffix(parts[6], ","))
texture := util.ConvertStringToInt(strings.TrimSuffix(parts[8], ","))
calories := util.ConvertStringToInt(strings.TrimSuffix(parts[10], ","))
in := Ingredient{
Cap: capacity,
Dur: durability,
Fla: flavor,
Tex: texture,
Cal: calories,
}
return &in
}