diff --git a/day15/day.go b/day15/day.go new file mode 100644 index 0000000..af7410d --- /dev/null +++ b/day15/day.go @@ -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 +} diff --git a/inputs/15 b/inputs/15 new file mode 100644 index 0000000..e1ee186 --- /dev/null +++ b/inputs/15 @@ -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 \ No newline at end of file diff --git a/inputs/15_test b/inputs/15_test new file mode 100644 index 0000000..f72d8b9 --- /dev/null +++ b/inputs/15_test @@ -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 \ No newline at end of file diff --git a/main.go b/main.go index 557abc3..71531c4 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,7 @@ import ( "aoc2015/day12" "aoc2015/day13" "aoc2015/day14" + "aoc2015/day15" "flag" "fmt" "os" @@ -46,6 +47,7 @@ func initDayFunctions() { 12: {1: day12.Part1, 2: day12.Part2}, 13: {1: day13.Part1, 2: day13.Part2}, 14: {1: day14.Part1, 2: day14.Part2}, + 15: {1: day15.Part1, 2: day15.Part2}, } }