This commit is contained in:
Stefan Harmuth 2020-12-28 07:57:08 +01:00
parent 8c2f62a2d5
commit 9ae42f7890
4 changed files with 117 additions and 0 deletions

109
day15/day.go Normal file
View File

@ -0,0 +1,109 @@
package day15
import (
"strconv"
"strings"
"tools"
)
func getIngredientsFromInput(input []string) ([]string, map[string]map[string]int) {
ingredients := make(map[string]map[string]int)
var ingredientList []string
for _, line := range input {
lineParts := strings.Split(line, " ")
ingredient := lineParts[0][:len(lineParts[0])-1]
ingredientList = append(ingredientList, ingredient)
ingredients[ingredient] = make(map[string]int)
ingredients[ingredient][lineParts[1]], _ = strconv.Atoi(lineParts[2][:len(lineParts[2])-1])
ingredients[ingredient][lineParts[3]], _ = strconv.Atoi(lineParts[4][:len(lineParts[4])-1])
ingredients[ingredient][lineParts[5]], _ = strconv.Atoi(lineParts[6][:len(lineParts[6])-1])
ingredients[ingredient][lineParts[7]], _ = strconv.Atoi(lineParts[8][:len(lineParts[8])-1])
ingredients[ingredient][lineParts[9]], _ = strconv.Atoi(lineParts[10])
}
return ingredientList, ingredients
}
func getCombinations(sum int, count int) (combinations [][]int) {
if count == 1 {
return [][]int{
{sum},
}
}
for i := sum - count + 1; i > 0; i-- {
subCombinations := getCombinations(sum-i, count-1)
for _, v := range subCombinations {
subCombination := append([]int{i}, v...)
combinations = append(combinations, subCombination)
}
}
return combinations
}
func Part1(puzzle tools.AoCPuzzle) interface{} {
ingredientList, ingredients := getIngredientsFromInput(puzzle.GetInputArray())
maxScore := 0
multiplierCombinations := getCombinations(100, len(ingredients))
properties := []string{"capacity", "durability", "flavor", "texture"}
for _, multiplier := range multiplierCombinations {
sums := make(map[string]int)
for _, propName := range properties {
for index, ingredient := range ingredientList {
sums[propName] += ingredients[ingredient][propName] * multiplier[index]
}
}
thisScore := 1
for _, v := range sums {
if v <= 0 {
thisScore = 0
} else {
thisScore *= v
}
}
if thisScore > maxScore {
maxScore = thisScore
}
}
return maxScore
}
func Part2(puzzle tools.AoCPuzzle) interface{} {
ingredientList, ingredients := getIngredientsFromInput(puzzle.GetInputArray())
maxScore := 0
multiplierCombinations := getCombinations(100, len(ingredients))
properties := []string{"capacity", "durability", "flavor", "texture", "calories"}
for _, multiplier := range multiplierCombinations {
sums := make(map[string]int)
for _, propName := range properties {
for index, ingredient := range ingredientList {
sums[propName] += ingredients[ingredient][propName] * multiplier[index]
}
}
if sums["calories"] != 500 {
continue
}
thisScore := 1
for prop, v := range sums {
if prop == "calories" {
continue
}
if v <= 0 {
thisScore = 0
} else {
thisScore *= v
}
}
if thisScore > maxScore {
maxScore = thisScore
}
}
return maxScore
}

4
inputs/15 Normal file
View File

@ -0,0 +1,4 @@
Sprinkles: capacity 2, durability 0, flavor -2, texture 0, calories 3
Butterscotch: capacity 0, durability 5, flavor -3, texture 0, calories 3
Chocolate: capacity 0, durability 0, flavor 5, texture -1, calories 8
Candy: capacity 0, durability -1, flavor 0, texture 5, calories 8

2
inputs/15_test Normal file
View File

@ -0,0 +1,2 @@
Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8
Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3

View File

@ -15,6 +15,7 @@ import (
"aoc2015/day12" "aoc2015/day12"
"aoc2015/day13" "aoc2015/day13"
"aoc2015/day14" "aoc2015/day14"
"aoc2015/day15"
"flag" "flag"
"fmt" "fmt"
"os" "os"
@ -46,6 +47,7 @@ func initDayFunctions() {
12: {1: day12.Part1, 2: day12.Part2}, 12: {1: day12.Part1, 2: day12.Part2},
13: {1: day13.Part1, 2: day13.Part2}, 13: {1: day13.Part1, 2: day13.Part2},
14: {1: day14.Part1, 2: day14.Part2}, 14: {1: day14.Part1, 2: day14.Part2},
15: {1: day15.Part1, 2: day15.Part2},
} }
} }