-
Notifications
You must be signed in to change notification settings - Fork 2
/
aoc202304.ex
67 lines (60 loc) · 1.51 KB
/
aoc202304.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
defmodule AOC2023.Day04 do
@moduledoc """
Advent of Code 2023, day 4: Scratchcards.
"""
require AOC
@doc """
Parse input.
"""
def parse(puzzle_input) do
puzzle_input
|> String.split("\n")
|> Enum.map(fn line ->
line
|> String.split(":")
|> Enum.at(1)
|> String.split("|")
|> Enum.map(fn tickets -> tickets |> String.split() |> MapSet.new() end)
|> then(fn [win, own] -> MapSet.intersection(win, own) end)
|> MapSet.size()
end)
end
@doc """
Solve part 1.
"""
def part1(data) do
data
|> Enum.filter(&(&1 > 0))
|> Enum.map(&(2 ** (&1 - 1)))
|> Enum.sum()
end
@doc """
Solve part 2.
"""
def part2(data) do
data
|> Enum.with_index(1)
|> Enum.filter(fn {win, _} -> win > 0 end)
|> Enum.reduce(
1..length(data) |> Enum.map(fn idx -> {idx, 1} end) |> Enum.into(%{}),
fn {win, id}, counts ->
(id + 1)..(id + win)
|> Enum.reduce(counts, &win_new_scratchcards(&1, &2, Map.get(counts, id)))
end
)
|> Map.values()
|> Enum.sum()
end
@doc """
Update the list of scratchcards with new copies.
## Example:
iex> win_new_scratchcards(3, %{1 => 1, 2 => 2, 3 => 4, 4 => 7, 5 => 1}, 5)
%{1 => 1, 2 => 2, 3 => 9, 4 => 7, 5 => 1}
"""
def win_new_scratchcards(id, counts, copies) do
Map.update(counts, id, nil, fn count -> count + copies end)
end
def main(args) do
Enum.map(args, fn path -> AOC.solve(path, &parse/1, &part1/1, &part2/1) end)
end
end