Advent of Code 2015 Day 15 Part 1
Mix.install([
{:kino_aoc, "~> 0.1"},
{:nx, "~> 0.9.2"}
])
Get Inputs
{:ok, puzzle_input} =
KinoAOC.download_puzzle("2015", "15", System.fetch_env!("LB_SESSION"))
My answer
Regex.named_captures(
~r/(?[a-zA-Z]+): .+ (?\-*\d+), .+ (?\-*\d+), .+ (?\-*\d+), .+ (?\-*\d+), .+ (?\-*\d+)/,
"Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8"
)
get_ingredients_tensor = fn rows ->
rows
|> Enum.map(fn row ->
Regex.named_captures(
~r/(?[a-zA-Z]+): .+ (?\-*\d+), .+ (?\-*\d+), .+ (?\-*\d+), .+ (?\-*\d+), .+ (?\-*\d+)/,
row
)
|> then(fn %{"capacity" => capacity, "durability" => durability, "flavor" => flavor, "texture" => texture} ->
[
String.to_integer(capacity),
String.to_integer(durability),
String.to_integer(flavor),
String.to_integer(texture)
]
end)
end)
|> Nx.tensor()
end
ingredients =
[
"Butterscotch: capacity -1, durability -2, flavor 6, texture 3, calories 8",
"Cinnamon: capacity 2, durability 3, flavor -2, texture -1, calories 3"
]
|> get_ingredients_tensor.()
nums = Nx.tensor([[44, 56]])
Nx.dot(nums, ingredients)
nums
|> Nx.dot(ingredients)
|> Nx.product(axes: [1])
ingredients =
puzzle_input
|> String.split("\n")
|> get_ingredients_tensor.()
all_combinations =
for sprinkles <- 0..100,
peanut_butter <- 0..100,
frosting <- 0..100,
sugar <- 0..100,
sprinkles + peanut_butter + frosting + sugar == 100 do
[sprinkles, peanut_butter, frosting, sugar]
end
all_combinations
|> Nx.tensor()
|> Nx.dot(ingredients)
all_combinations
|> Nx.tensor()
|> Nx.dot(ingredients)
|> Nx.max(0)
all_combinations
|> Nx.tensor()
|> Nx.dot(ingredients)
|> Nx.max(0)
|> Nx.product(axes: [1])
all_combinations
|> Nx.tensor()
|> Nx.dot(ingredients)
|> Nx.max(0)
|> Nx.product(axes: [1])
|> Nx.reduce_max()