Skip to content

Commit

Permalink
⚡ Add solution to Day 7 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrainerson committed Jan 1, 2024
1 parent 726577b commit ff10243
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions advent_of_code/lib/advent_of_code.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ defmodule AdventOfCode do
print(6, 1, Day6.part1())
print(6, 2, Day6.part2())
print(7, 1, Day7.part1())
print(7, 2, Day7.part2())
end
end
28 changes: 23 additions & 5 deletions advent_of_code/lib/day_7.ex
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
defmodule Day7 do
def classify_hand({hand, bid}) do
def classify_hand({hand, bid}, jokers \\ false) do
hand =
hand
|> String.to_charlist()
|> Enum.map(
&case &1 do
?T -> "A"
?J -> "B"
?J -> if jokers, do: "1", else: "B"
?Q -> "C"
?K -> "D"
?A -> "E"
x -> x
end
)

{jokers, non_jokers} = hand |> Enum.split_with(&(&1 == "1"))
jokers = jokers |> Enum.count()

type =
hand
non_jokers
|> Enum.frequencies()
|> Map.values()
|> Enum.sort(:desc)
|> Kernel.then(fn list ->
if list == [] do
[5]
else
[hd(list) + jokers | tl(list)]
end
end)
|> case do
[5] -> 7
[4, 1] -> 6
Expand All @@ -38,13 +48,21 @@ defmodule Day7 do
|> Stream.map(fn [hand, bid] -> {hand, String.to_integer(bid)} end)
end

def part1(stream \\ Inputs.stream(7)) do
def solve(stream, jokers \\ false) do
stream
|> parse_hands()
|> Stream.map(&classify_hand/1)
|> Stream.map(&classify_hand(&1, jokers))
|> Enum.sort()
|> Enum.with_index(1)
|> Enum.map(fn {{_, _, bid}, rank} -> bid * rank end)
|> Enum.sum()
end

def part1(stream \\ Inputs.stream(7)) do
stream |> solve()
end

def part2(stream \\ Inputs.stream(7)) do
stream |> solve(true)
end
end
26 changes: 25 additions & 1 deletion advent_of_code/test/day7_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ defmodule Day7Test do
] == hands
end

test "Day7.classify_hand/1" do
test "Day7.classify_hand/2" do
assert {2, "32A3D", 765} = Day7.classify_hand({"32T3K", 765})
assert {4, "A55B5", 684} = Day7.classify_hand({"T55J5", 684})
assert {3, "DD677", 28} = Day7.classify_hand({"KK677", 28})
assert {3, "DABBA", 220} = Day7.classify_hand({"KTJJT", 220})
assert {4, "CCCBE", 483} = Day7.classify_hand({"QQQJA", 483})

assert {2, "32A3D", 765} = Day7.classify_hand({"32T3K", 765}, true)
assert {6, "A5515", 684} = Day7.classify_hand({"T55J5", 684}, true)
assert {3, "DD677", 28} = Day7.classify_hand({"KK677", 28}, true)
assert {6, "DA11A", 220} = Day7.classify_hand({"KTJJT", 220}, true)
assert {6, "CCC1E", 483} = Day7.classify_hand({"QQQJA", 483}, true)
end

test "sorting" do
Expand All @@ -46,10 +52,28 @@ defmodule Day7Test do
{3, "DABBA", 220},
{2, "32A3D", 765}
] == hands

hands =
@example_input
|> Day7.parse_hands()
|> Stream.map(&Day7.classify_hand(&1, true))
|> Enum.sort(:desc)

assert [
{6, "DA11A", 220},
{6, "CCC1E", 483},
{6, "A5515", 684},
{3, "DD677", 28},
{2, "32A3D", 765}
] == hands
end

test "Day7.part1/1" do
assert 6440 == Day7.part1(@example_input)
end

test "Day7.part2/1" do
assert 5905 == Day7.part2(@example_input)
end
end
end

0 comments on commit ff10243

Please sign in to comment.