This commit is contained in:
bluepython508
2023-12-07 11:51:22 +00:00
parent abbd8a263a
commit 863e1a5cb0
5 changed files with 81 additions and 0 deletions

73
lib/day7.ex Normal file
View File

@@ -0,0 +1,73 @@
defmodule Aoc2023.Day7 do
use Aoc2023
@ranking_1 %{
?A => 13,
?K => 12,
?Q => 11,
?J => 10,
?T => 9,
?9 => 8,
?8 => 7,
?7 => 6,
?6 => 5,
?5 => 4,
?4 => 3,
?3 => 2,
?2 => 1
}
@ranking_2 @ranking_1 |> Map.put(?J, 0)
def hand_type(values) do
for ty <- values, reduce: %{} do
m -> Map.update(m, ty, 1, &(&1 + 1))
end
|> Enum.map(pipe(elem(1)))
|> Enum.sort(:desc)
|> case do
[5] -> 6
[4, 1] -> 5
[3, 2] -> 4
[3, 1, 1] -> 3
[2, 2, 1] -> 2
[2, 1, 1, 1] -> 1
[1, 1, 1, 1, 1] -> 0
end
end
def parse(input) do
fn rankings, hand_rank ->
input
|> lines
|> Enum.map(&String.split/1)
|> Enum.map(fn [hand, bid] ->
{
hand |> String.to_charlist() |> Enum.map(&Map.get(rankings, &1)),
bid |> String.to_integer()
}
end)
|> Enum.sort_by(elem(0) |> hand_rank.() &&& elem(0), :asc)
|> Enum.with_index(1)
|> Enum.map(fn {{_, bid}, index} -> bid * index end)
|> Enum.sum()
end
end
def part1(input) do
input.(@ranking_1, &hand_type/1)
end
def part2(input) do
input.(
@ranking_2,
fn hand ->
@ranking_2
|> Enum.map(pipe(elem(1)))
|> Enum.map(fn j -> hand |> Enum.map(&if &1 == 0, do: j, else: &1) end)
|> Enum.map(&hand_type/1)
|> Enum.max()
end
)
end
end

View File

@@ -8,6 +8,7 @@ defmodule Mix.Tasks.Aoc do
defp module(4), do: Aoc2023.Day4
defp module(5), do: Aoc2023.Day5
defp module(6), do: Aoc2023.Day6
defp module(7), do: Aoc2023.Day7
# [MODULE INSERTION POINT]
defp base_dir(), do: System.get_env("AOC_BASE")

5
tests/day7/1 Normal file
View File

@@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

1
tests/day7/1.1 Normal file
View File

@@ -0,0 +1 @@
6440

1
tests/day7/1.2 Normal file
View File

@@ -0,0 +1 @@
5905