-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc201809.ex
198 lines (159 loc) · 4.97 KB
/
aoc201809.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
defmodule AOC2018.Day09 do
@moduledoc """
Advent of Code 2018, day 9: Marble Mania.
With apologies to Saša Jurić
https://github.com/sasa1977/aoc/blob/master/lib/2018/day9.ex
"""
require AOC
alias AOC2018.Day09.CircularList
@doc """
Parse input.
"""
def parse(puzzle_input) do
puzzle_input
|> String.split(" ")
|> Enum.map(&Integer.parse/1)
|> Enum.reject(fn num -> num == :error end)
|> Enum.map(fn {num, _} -> num end)
|> List.to_tuple()
end
@doc """
Solve part 1.
"""
def part1({players, marbles}), do: play(players, marbles) |> max_score()
@doc """
Solve part 2.
"""
def part2({players, marbles}), do: play(players, marbles * 100) |> max_score()
@doc """
Play a round of marbles.
## Example:
iex> play(3, 33)
%{
player: 1,
players: 3,
scores: %{2 => 32},
state: {
[33, 13, 3, 14, 7, 15],
[6, 32, 12, 31, 1, 30, 11, 29, 22, 28, 5, 27, 21, 26, 10, 25, 20, 24, 2, 19, 18, 4, 17, 8, 16, 0]
}
}
"""
def play(players, marbles) do
1..marbles
|> Enum.reduce(
%{state: CircularList.new([0]), player: 1, players: players, scores: %{}},
&move(&2, &1)
)
end
@doc """
Make one move in the game of marbles.
## Examples:
iex> move(%{state: {[3], [1, 2, 0]}, player: 2, players: 3, scores: %{}}, 4)
%{state: {[4, 2, 1, 3], [0]}, player: 0, players: 3, scores: %{}}
iex> move(%{state: {[4, 2, 1, 3], [0]}, player: 0, players: 3, scores: %{}}, 23)
%{state: {[], [1, 2, 4, 0]}, player: 1, players: 3, scores: %{0 => 26}}
"""
def move(game = %{state: state, scores: scores, player: player}, marble)
when rem(marble, 23) == 0 do
{score, state} =
Enum.reduce(1..7, state, fn _, state -> CircularList.previous(state) end)
|> CircularList.pop()
%{
game
| state: state,
scores: scores |> Map.update(player, score + marble, &(&1 + score + marble))
}
|> next_player()
end
def move(game = %{state: state}, marble) do
state =
state
|> CircularList.next()
|> CircularList.next()
|> CircularList.insert(marble)
%{game | state: state} |> next_player()
end
@doc """
Prepare for the next player.
## Examples:
iex> next_player(%{state: {[0], []}, player: 0, players: 3, scores: %{}})
%{state: {[0], []}, player: 1, players: 3, scores: %{}}
iex> next_player(%{state: {[0], []}, player: 2, players: 3, scores: %{}})
%{state: {[0], []}, player: 0, players: 3, scores: %{}}
"""
def next_player(game = %{player: player, players: players}) do
%{game | player: rem(player + 1, players)}
end
@doc """
Calculate the max score after a game of marbles.
## Example:
iex> max_score(%{state: {[0], []}, player: 0, players: 3, scores: %{0 => 99, 1 => 77, 2 => 42}})
99
"""
def max_score(%{scores: scores}), do: scores |> Map.values() |> Enum.max()
def main(args) do
Enum.map(args, fn path -> AOC.solve(path, &parse/1, &part1/1, &part2/1) end)
end
end
defmodule AOC2018.Day09.CircularList do
@moduledoc """
A circular list where the first element follows the last one.
Implemented as a tuple of lists containing the next elements and the
elements before the pointer, respectively.
"""
@doc """
Create a new circular list.
## Example:
iex> new([1, 2, 3])
{[1, 2, 3], []}
"""
def new(elements), do: {elements, []}
@doc """
Move to the next element in the list.
## Example:
iex> numbers = new([1, 2, 3])
iex> next(numbers)
{[2, 3], [1]}
iex> numbers |> next() |> next() |> next()
{[], [3, 2, 1]}
iex> numbers |> next() |> next() |> next() |> next()
{[2, 3], [1]}
"""
def next({[], previous}), do: {Enum.reverse(previous), []} |> next()
def next({[current | next], previous}), do: {next, [current | previous]}
@doc """
Move to the previous element in the list.
## Example:
iex> numbers = new([1, 2, 3])
iex> previous(numbers)
{[3], [2, 1]}
iex> numbers |> previous() |> previous()
{[2, 3], [1]}
"""
def previous({next, []}), do: {[], Enum.reverse(next)} |> previous()
def previous({next, [last | previous]}), do: {[last | next], previous}
@doc """
Insert an element into the list.
## Example:
iex> numbers = new([1, 2, 3])
iex> numbers |> insert(4)
{[4, 1, 2, 3], []}
iex> numbers |> next() |> next() |> insert(4)
{[4, 3], [2, 1]}
"""
def insert({next, previous}, element), do: {[element | next], previous}
@doc """
Pop the current element from the list.
## Example:
iex> numbers = new([1, 2, 3])
iex> numbers |> pop()
{1, {[2, 3], []}}
iex> numbers |> next() |> next() |> pop()
{3, {[], [2, 1]}}
iex> numbers |> previous() |> pop()
{3, {[], [2, 1]}}
"""
def pop({[], [last | previous]}), do: {last, {[], previous}}
def pop({[current | next], previous}), do: {current, {next, previous}}
end