Skip to content

Commit

Permalink
day 4 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
krystiangryczon committed Dec 4, 2023
1 parent 02c1002 commit b8638c6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
3 changes: 2 additions & 1 deletion lib/advent_of_code/day_04.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion lib/code/day4_1.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 34 additions & 0 deletions lib/code/day4_2.ex
Original file line number Diff line number Diff line change
@@ -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
20 changes: 3 additions & 17 deletions test/advent_of_code/day_03_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule AdventOfCode.Day03Test do

import AdventOfCode.Day03

defp test_input1,
defp test_input,
do: """
467..114..
...*......
Expand All @@ -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
Expand Down
11 changes: 5 additions & 6 deletions test/advent_of_code/day_04_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit b8638c6

Please sign in to comment.