-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.ex
115 lines (96 loc) · 2.34 KB
/
2.ex
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import AOC
aoc 2023, 2 do
import NimbleParsec
dice =
integer(min: 1)
|> ignore(string(" "))
|> choice([
string("blue"),
string("green"),
string("red")
])
|> label("die")
|> optional(ignore(string(", ")))
|> times(min: 1, max: 3)
|> label("dice")
game =
ignore(string("Game "))
|> integer(min: 1)
|> ignore(string(": "))
|> label("game")
defparsec(:game_p, game)
defparsec(:dice_p, dice)
def read_game(input) do
case game_p(input) do
{:ok, [id], rest, _, _, _} ->
dice_and_rest =
Stream.unfold(rest, fn
nil ->
nil
rest ->
case dice_p(rest) do
{:ok, dice, rest, _, _, _} ->
dice =
dice
|> Enum.chunk_every(2)
|> Enum.map(&Enum.reverse/1)
|> Enum.map(&List.to_tuple/1)
|> Enum.into(%{})
{dice, String.replace_prefix(rest, "; ", "")}
{:error, _, rest, _, _, _} ->
{rest, nil}
end
end)
|> Enum.to_list()
[rest | dice] = Enum.reverse(dice_and_rest)
{:ok, {id, dice, rest}}
_ ->
:halt
end
end
def read_games(input) do
Stream.unfold(input, fn rest ->
input =
rest
|> String.replace_prefix("\n", "")
case read_game(input) do
{:ok, {id, dice, rest}} ->
{{id, dice}, rest}
_ ->
nil
end
end)
end
def p1(input) do
red_max = 12
green_max = 13
blue_max = 14
input
|> read_games()
|> Stream.reject(fn {_id, dice} ->
dice
|> Enum.any?(fn d ->
Map.get(d, "red", 0) > red_max or Map.get(d, "green", 0) > green_max or
Map.get(d, "blue", 0) > blue_max
end)
end)
|> Stream.map(&elem(&1, 0))
|> Enum.sum()
end
def p2(input) do
input
|> read_games()
|> Stream.map(&elem(&1, 1))
|> Stream.map(&calculate_power/1)
|> Enum.sum()
end
defp calculate_power(dice) do
{reds, greens, blues} =
dice
|> Enum.reduce({0, 0, 0}, fn d, {r, g, b} ->
{max(Map.get(d, "red", 0), r), max(Map.get(d, "green", 0), g),
max(Map.get(d, "blue", 0), b)}
end)
reds * blues * greens
end
end