2015-day15: cookie science, gross nested for loops

This commit is contained in:
alexchao26
2020-12-26 19:39:35 -05:00
parent 3c078ace91
commit 1bc72e63d7
2 changed files with 94 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
package main
import (
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/mathutil"
"github.com/alexchao26/advent-of-code-go/util"
)
func main() {
part1Ans, part2Ans := cookieScience(util.ReadFile("./input.txt"))
fmt.Printf("Part1: %d\nPart2: %d\n", part1Ans, part2Ans)
}
func cookieScience(input string) (int, int) {
lines := strings.Split(input, "\n")
cookieVals := [][]int{}
for _, line := range lines {
var name string
var cap, dur, fla, tex, cal int
fmt.Sscanf(line, "%s capacity %d, durability %d, flavor %d, texture %d, calories %d",
&name, &cap, &dur, &fla, &tex, &cal)
// trim off colons, fmt.Sscanf parses between spaces/whitespace
// names end up going unused...
name = strings.Trim(name, ":")
// cookie vals are simply all five values in order
cookieVals = append(cookieVals, []int{cap, dur, fla, tex, cal})
}
var bestScore, best500CalScore int
for ing1 := 0; ing1 < 100; ing1++ {
for ing2 := 0; ing2 < 100; ing2++ {
for ing3 := 0; ing3 < 100; ing3++ {
ing4 := 100 - ing1 - ing2 - ing3
cap := ing1*cookieVals[0][0] + ing2*cookieVals[1][0] + ing3*cookieVals[2][0] + ing4*cookieVals[3][0]
dur := ing1*cookieVals[0][1] + ing2*cookieVals[1][1] + ing3*cookieVals[2][1] + ing4*cookieVals[3][1]
fla := ing1*cookieVals[0][2] + ing2*cookieVals[1][2] + ing3*cookieVals[2][2] + ing4*cookieVals[3][2]
tex := ing1*cookieVals[0][3] + ing2*cookieVals[1][3] + ing3*cookieVals[2][3] + ing4*cookieVals[3][3]
cal := ing1*cookieVals[0][4] + ing2*cookieVals[1][4] + ing3*cookieVals[2][4] + ing4*cookieVals[3][4]
// make negatives zero, without this two negative scores could
// make a very large positive
cap = mathutil.MaxInt(0, cap)
dur = mathutil.MaxInt(0, dur)
fla = mathutil.MaxInt(0, fla)
tex = mathutil.MaxInt(0, tex)
score := cap * dur * fla * tex
if cal == 500 {
best500CalScore = mathutil.MaxInt(best500CalScore, score)
}
bestScore = mathutil.MaxInt(bestScore, score)
}
}
}
return bestScore, best500CalScore
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"testing"
"github.com/alexchao26/advent-of-code-go/util"
)
func Test_cookieScience(t *testing.T) {
tests := []struct {
name string
input string
want1 int
want2 int
}{
{"actual both parts", util.ReadFile("input.txt"), 13882464, 11171160},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got1, got2 := cookieScience(tt.input)
if got1 != tt.want1 {
t.Errorf("cookieScience() part1 = %v, want %v", got1, tt.want1)
}
if got2 != tt.want2 {
t.Errorf("cookieScience() part2 = %v, want %v", got2, tt.want2)
}
})
}
}