Skip to content

Commit

Permalink
Day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Nov 7, 2024
1 parent 752475e commit 1bf1955
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
4 changes: 4 additions & 0 deletions d15/input.txt
Original file line number Diff line number Diff line change
@@ -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
124 changes: 124 additions & 0 deletions d15/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package main

import (
"aoc2015/util"
"regexp"
"slices"
)

type Ingredient struct {
Name string
Capacity int
Durability int
Flavor int
Texture int
Calories int
}

func part1(ingredients []Ingredient) int {
ingredientsCount := len(ingredients)

combinations := util.CombinationsWithSum(ingredientsCount, 100)
sums := util.ArrayMap(combinations, func(combination []int) int {
capacity := 0
durability := 0
flavor := 0
texture := 0

for i := 0; i < ingredientsCount; i++ {
capacity += combination[i] * ingredients[i].Capacity
durability += combination[i] * ingredients[i].Durability
flavor += combination[i] * ingredients[i].Flavor
texture += combination[i] * ingredients[i].Texture
}

if capacity < 0 {
capacity = 0
}

if durability < 0 {
durability = 0
}

if flavor < 0 {
flavor = 0
}

if texture < 0 {
texture = 0
}

return capacity * durability * flavor * texture
})

return slices.Max(sums)
}

func part2(ingredients []Ingredient) int {
ingredientsCount := len(ingredients)

combinations := util.CombinationsWithSum(ingredientsCount, 100)
sums := util.ArrayMap(combinations, func(combination []int) int {
capacity := 0
durability := 0
flavor := 0
texture := 0
calories := 0

for i := 0; i < ingredientsCount; i++ {
capacity += combination[i] * ingredients[i].Capacity
durability += combination[i] * ingredients[i].Durability
flavor += combination[i] * ingredients[i].Flavor
texture += combination[i] * ingredients[i].Texture
calories += combination[i] * ingredients[i].Calories
}

if capacity < 0 {
capacity = 0
}

if durability < 0 {
durability = 0
}

if flavor < 0 {
flavor = 0
}

if texture < 0 {
texture = 0
}

if calories != 500 {
return 0
}

return capacity * durability * flavor * texture
})

return slices.Max(sums)
}

// Lol, it's basically a SAT solver
func main() {
lineRegexp := regexp.MustCompile(`^(\w+): \w+ ([0-9-]+), \w+ ([0-9-]+), \w+ ([0-9-]+), \w+ ([0-9-]+), \w+ ([0-9-]+)$`)
reader := func(name string) []Ingredient {
lines := util.ReadLines(name)

return util.ArrayMap(lines, func(line string) Ingredient {
match := lineRegexp.FindStringSubmatch(line)

return Ingredient{
Name: match[1],
Capacity: util.StringToInt(match[2]),
Durability: util.StringToInt(match[3]),
Flavor: util.StringToInt(match[4]),
Texture: util.StringToInt(match[5]),
Calories: util.StringToInt(match[6]),
}
})
}

util.TestRuns("15", reader, part1, part2)
util.SolutionRuns("15", reader, part1, part2)
}
2 changes: 2 additions & 0 deletions d15/test-runs/0.txt
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions util/combinatorics.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,31 @@ func Permutations[T any](array []T) (permutations [][]T) {

return res
}

// Returns all combinations of N numbers that sums to M
func CombinationsWithSum(length, targetSum int) [][]int {
var results [][]int
var current []int

var backtrack func(length, targetSum, start int, current []int)
backtrack = func(length, targetSum, start int, current []int) {
if len(current) == length {
if Sum(current) == targetSum {
cpy := make([]int, len(current))
copy(cpy, current)
results = append(results, cpy)
}
return
}

for i := 0; i <= targetSum; i++ {
current = append(current, i)
backtrack(length, targetSum, start+i, current)
current = current[:len(current)-1]
}
}

backtrack(length, targetSum, 0, current)

return results
}

0 comments on commit 1bf1955

Please sign in to comment.