From b8638c6d185dd0bbcfa27a67ca0963374b7ca301 Mon Sep 17 00:00:00 2001 From: Krystian Date: Mon, 4 Dec 2023 19:12:16 +0100 Subject: [PATCH] day 4 part 2 --- lib/advent_of_code/day_04.ex | 3 ++- lib/code/day4_1.ex | 1 - lib/code/day4_2.ex | 34 +++++++++++++++++++++++++++++ test/advent_of_code/day_03_test.exs | 20 +++-------------- test/advent_of_code/day_04_test.exs | 11 +++++----- 5 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 lib/code/day4_2.ex diff --git a/lib/advent_of_code/day_04.ex b/lib/advent_of_code/day_04.ex index 37fd81e..402460d 100644 --- a/lib/advent_of_code/day_04.ex +++ b/lib/advent_of_code/day_04.ex @@ -3,6 +3,7 @@ defmodule AdventOfCode.Day04 do Advent.Code4_1.execute(inp) end - def part2(_args) do + def part2(inp) do + Advent.Code4_2.execute(inp) end end diff --git a/lib/code/day4_1.ex b/lib/code/day4_1.ex index 5c30811..7c0f134 100644 --- a/lib/code/day4_1.ex +++ b/lib/code/day4_1.ex @@ -19,6 +19,5 @@ defmodule Advent.Code4_1 do |> Enum.map(fn el -> length(MapSet.to_list(el)) end) |> Enum.filter(fn el -> el != 0 end) |> Enum.reduce(0, fn el, acc -> (acc + :math.pow(2, el - 1)) |> round end) - |> dbg() end end diff --git a/lib/code/day4_2.ex b/lib/code/day4_2.ex new file mode 100644 index 0000000..e5c45a3 --- /dev/null +++ b/lib/code/day4_2.ex @@ -0,0 +1,34 @@ +defmodule Advent.Code4_2 do + def find_winning_cards_count([], _current), do: 0 + def find_winning_cards_count(_list, nil), do: 0 + + def find_winning_cards_count(list, curr_el) do + Enum.slice(list, (curr_el[:id] + 1)..(curr_el[:id] + curr_el[:wins])) + |> Enum.reduce(1, fn el, ac -> ac + find_winning_cards_count(list, el) end) + end + + def execute(inp) do + parsed = + inp + |> Enum.map(fn el -> + [_, data] = String.split(el, ": ", trim: true) + data + end) + |> Enum.map(fn el -> String.split(el, " | ", trim: true) end) + |> Enum.map(fn sublist -> + Enum.map(sublist, fn e -> String.split(e, " ", trim: true) end) + end) + |> Enum.map(fn [el, mn] -> %{winning: el, my: mn} end) + |> Enum.map(fn row -> + w = MapSet.new(row[:winning]) + m = MapSet.new(row[:my]) + + MapSet.intersection(w, m) + end) + |> Enum.with_index() + |> Enum.map(fn {el, idx} -> %{id: idx, wins: MapSet.size(el)} end) + + parsed + |> Enum.reduce(0, fn el, acc -> acc + find_winning_cards_count(parsed, el) end) + end +end diff --git a/test/advent_of_code/day_03_test.exs b/test/advent_of_code/day_03_test.exs index be81731..179f00f 100644 --- a/test/advent_of_code/day_03_test.exs +++ b/test/advent_of_code/day_03_test.exs @@ -3,7 +3,7 @@ defmodule AdventOfCode.Day03Test do import AdventOfCode.Day03 - defp test_input1, + defp test_input, do: """ 467..114.. ...*...... @@ -18,29 +18,15 @@ defmodule AdventOfCode.Day03Test do """ test "part1" do - input = test_input1() + input = test_input() result = part1(input) assert result == 4361 end - defp test_input2, - do: """ - 467..114.. - ...*...... - ..35..633. - ......#... - 617*...... - .....+.58. - ..592..... - ......755. - ...$.*.... - .664.598.. - """ - @tag :skip test "part2" do - input = test_input2() + input = test_input() result = part2(input) assert result == 467_835 diff --git a/test/advent_of_code/day_04_test.exs b/test/advent_of_code/day_04_test.exs index c9857a8..f4ca671 100644 --- a/test/advent_of_code/day_04_test.exs +++ b/test/advent_of_code/day_04_test.exs @@ -3,7 +3,7 @@ defmodule AdventOfCode.Day04Test do import AdventOfCode.Day04 - def test_input1(), + def test_input(), do: """ Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 @@ -14,17 +14,16 @@ defmodule AdventOfCode.Day04Test do """ test "part1" do - input = test_input1() + input = test_input() result = part1(My.Utils.split_file(input, "\n")) assert result == 13 end - @tag :skip test "part2" do - input = nil - result = part2(input) + input = test_input() + result = part2(My.Utils.split_file(input, "\n")) - assert result + assert result == 30 end end