1
0
Fork 0

solve 2023 7.2

This commit is contained in:
sloane 2023-12-07 10:49:26 -05:00
parent 6bc67ec21f
commit dcbdbcf128
No known key found for this signature in database
2 changed files with 46 additions and 14 deletions

View file

@ -4,7 +4,14 @@ import AOCHelpers
aoc 2023, 7 do aoc 2023, 7 do
def p1(input) do def p1(input) do
input input
|> read_input() |> read_input(fn
"A" -> 14
"K" -> 13
"Q" -> 12
"J" -> 11
"T" -> 10
n -> String.to_integer(n)
end)
|> Enum.map(fn [hand, bet] -> |> Enum.map(fn [hand, bet] ->
counts = counts =
hand hand
@ -21,30 +28,55 @@ aoc 2023, 7 do
|> Enum.sum() |> Enum.sum()
end end
def p2(_input) do def p2(input) do
input
|> read_input(fn
"J" -> -1
"A" -> 14
"K" -> 13
"Q" -> 12
"T" -> 10
n -> String.to_integer(n)
end)
|> Enum.map(fn [hand, bet] ->
{jokers, hand_without_jokers} = Enum.split_with(hand, &(&1 === -1))
number_of_jokers = Enum.count(jokers)
counts_without_jokers =
hand_without_jokers
|> Enum.frequencies()
|> Map.values()
|> Enum.sort(:desc)
counts = [
List.first(counts_without_jokers, 0) + number_of_jokers
| Enum.drop(counts_without_jokers, 1)
]
[counts, hand, bet]
end)
|> Enum.sort(&compare_hands_1/2)
|> Enum.map(&Enum.at(&1, 2))
|> Enum.with_index(1)
|> Enum.map(fn {bet, rank} -> bet * rank end)
|> Enum.sum()
end end
def read_input(input) do def read_input(input, card_to_number) do
input input
|> lines() |> lines()
|> Enum.map(fn line -> |> Enum.map(fn line ->
line line
|> words() |> words()
|> map_list([&to_numeric_hand/1, &String.to_integer/1]) |> map_list([&to_numeric_hand(&1, card_to_number), &String.to_integer/1])
end) end)
end end
def to_numeric_hand(str) do def to_numeric_hand(str, card_to_number) do
str str
|> letters() |> letters()
|> Enum.map(fn |> Enum.map(card_to_number)
"A" -> 14
"K" -> 13
"Q" -> 12
"J" -> 11
"T" -> 10
n -> String.to_integer(n)
end)
end end
def compare_hands_1([counts, hand_a, _], [counts, hand_b, _]), do: hand_a <= hand_b def compare_hands_1([counts, hand_a, _], [counts, hand_b, _]), do: hand_a <= hand_b

View file

@ -14,7 +14,7 @@
1. [2020] **17/50** 🌟 1. [2020] **17/50** 🌟
1. [2021] **43/50** 🌟 1. [2021] **43/50** 🌟
1. [2022] **14/50** 🌟 1. [2022] **14/50** 🌟
1. [2023] **10/50** 🌟 1. [2023] **11/50** 🌟
[2015]: ./2015 [2015]: ./2015
[2017]: ./2017 [2017]: ./2017