Skip to content

Commit 9b42645

Browse files
committed
day4
1 parent ae43111 commit 9b42645

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

04/sample_input

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
2+
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
3+
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
4+
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
5+
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
6+
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11

04/sol.exs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
defmodule Day04 do
2+
def read_input(file_path) do
3+
case File.read(file_path) do
4+
{:ok, content} ->
5+
content
6+
|> String.split(~r/\n/, trim: true)
7+
|> Enum.map(&read_line/1)
8+
9+
{:error, reason} ->
10+
raise "Oh no! #{reason}"
11+
end
12+
end
13+
14+
def read_line(str) do
15+
[_, b, c] =
16+
String.split(str, ~r/:|\|/, trim: true) |> Enum.map(&String.split(&1, " ", trim: true))
17+
18+
{1, MapSet.new(Enum.map(b, &String.to_integer/1)),
19+
MapSet.new(Enum.map(c, &String.to_integer/1))}
20+
end
21+
22+
def partA(file_path) do
23+
file_path
24+
|> read_input
25+
|> Enum.map(&number_of_matches/1)
26+
|> Enum.map(&point/1)
27+
|> Enum.sum()
28+
end
29+
30+
def partB(file_path) do
31+
rem = file_path |> read_input
32+
33+
add_duplicates(hd(rem), tl(rem), [])
34+
|> Enum.map(&elem(&1, 0))
35+
|> Enum.sum()
36+
end
37+
38+
def number_of_matches({_, x, y}), do: MapSet.intersection(x, y) |> MapSet.size()
39+
40+
def point(0), do: 0
41+
def point(num_of_matches), do: 2 ** (num_of_matches - 1)
42+
43+
def add_to_next_N(0, _, l, acc), do: Enum.reverse(acc) ++ l
44+
def add_to_next_N(_, _, [], acc), do: Enum.reverse(acc)
45+
46+
def add_to_next_N(n, val, [{a, b, c} | rest], acc) do
47+
add_to_next_N(n - 1, val, rest, [{a + val, b, c} | acc])
48+
end
49+
50+
def add_duplicates(current_card, [], acc), do: Enum.reverse([current_card | acc])
51+
52+
def add_duplicates({k, x, y}, remaining_cards, acc) do
53+
new_rem = add_to_next_N(number_of_matches({k, x, y}), k, remaining_cards, [])
54+
add_duplicates(hd(new_rem), tl(new_rem), [{k, x, y} | acc])
55+
end
56+
end
57+
58+
IO.puts(Day04.partA("./input"))
59+
IO.puts(Day04.partB("./input"))

0 commit comments

Comments
 (0)