|
| 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