1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
ingredients =
File.stream!("inputs/day15.txt")
|> Enum.filter(fn line -> String.trim(line) != "" end)
|> Enum.map(fn line ->
Regex.run(
~r/\w+: capacity ([+-]?\d+), durability ([+-]?\d+), flavor ([+-]?\d+), texture ([+-]?\d+), calories ([+-]?\d+)/,
line
)
|> Stream.drop(1)
|> Stream.map(&Integer.parse/1)
|> Enum.map(fn {i, _} -> i end)
end)
ingredientsWithoutCalories =
Enum.map(ingredients, fn line ->
{_calories, withoutCalories} = List.pop_at(line, -1)
withoutCalories
end)
ingredientsWithCalories =
Enum.map(ingredients, fn line ->
List.pop_at(line, -1)
end)
defmodule RecipeFinder do
def findTastiestRecipe([lastIngredient], remainingSpoons, currentMixture) do
Enum.zip(lastIngredient, currentMixture)
|> Enum.map(fn {next, current} -> next * remainingSpoons + current end)
|> Enum.map(&max(&1, 0))
|> Enum.product()
end
def findTastiestRecipe([nextIngredient | remainingIngredients], remainingSpoons, currentMixture) do
Enum.map(0..remainingSpoons, fn nextSpoons ->
findTastiestRecipe(
remainingIngredients,
remainingSpoons - nextSpoons,
Enum.zip(nextIngredient, currentMixture)
|> Enum.map(fn {next, current} -> next * nextSpoons + current end)
)
end)
|> Enum.max()
end
def findTastiestRecipeWithExactCalories(
[{lastIngredientCalories, _}],
remainingSpoons,
remainingCalories,
_
)
when lastIngredientCalories * remainingSpoons != remainingCalories do
nil
end
def findTastiestRecipeWithExactCalories(
[{_, lastIngredient}],
remainingSpoons,
_,
currentMixture
) do
Enum.zip(lastIngredient, currentMixture)
|> Enum.map(fn {next, current} -> next * remainingSpoons + current end)
|> Enum.map(&max(&1, 0))
|> Enum.product()
end
def findTastiestRecipeWithExactCalories(
[{nextIngredientCalories, nextIngredient} | remainingIngredients],
remainingSpoons,
remainingCalories,
currentMixture
) do
maxSpoons = min(remainingSpoons, div(remainingCalories, nextIngredientCalories))
Enum.map(0..maxSpoons, fn nextSpoons ->
findTastiestRecipeWithExactCalories(
remainingIngredients,
remainingSpoons - nextSpoons,
remainingCalories - nextIngredientCalories * nextSpoons,
Enum.zip(nextIngredient, currentMixture)
|> Enum.map(fn {next, current} -> next * nextSpoons + current end)
)
end)
|> Enum.filter(&(!is_nil(&1)))
|> Enum.max(&>=/2, fn -> nil end)
end
end
tastiest = RecipeFinder.findTastiestRecipe(ingredientsWithoutCalories, 100, [0, 0, 0, 0])
IO.puts("Tastiest: #{tastiest}")
healthiest =
RecipeFinder.findTastiestRecipeWithExactCalories(ingredientsWithCalories, 100, 500, [0, 0, 0, 0])
IO.puts("Healthiest: #{healthiest}")
|