From ff102439d1a05baa09c79a45ff7b7e7366b3ca57 Mon Sep 17 00:00:00 2001 From: Simon Rainerson Date: Mon, 1 Jan 2024 15:46:17 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Add=20solution=20to=20Day=207=20par?= =?UTF-8?q?t=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- advent_of_code/lib/advent_of_code.ex | 1 + advent_of_code/lib/day_7.ex | 28 +++++++++++++++++++++++----- advent_of_code/test/day7_test.exs | 26 +++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/advent_of_code/lib/advent_of_code.ex b/advent_of_code/lib/advent_of_code.ex index 34e7a0e..81de69e 100644 --- a/advent_of_code/lib/advent_of_code.ex +++ b/advent_of_code/lib/advent_of_code.ex @@ -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 diff --git a/advent_of_code/lib/day_7.ex b/advent_of_code/lib/day_7.ex index ec214fc..f7e4432 100644 --- a/advent_of_code/lib/day_7.ex +++ b/advent_of_code/lib/day_7.ex @@ -1,12 +1,12 @@ 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" @@ -14,11 +14,21 @@ defmodule Day7 do 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 @@ -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 diff --git a/advent_of_code/test/day7_test.exs b/advent_of_code/test/day7_test.exs index e37850b..2800178 100644 --- a/advent_of_code/test/day7_test.exs +++ b/advent_of_code/test/day7_test.exs @@ -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 @@ -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