1
0
Fork 0

solve 2023 7.1

This commit is contained in:
sloane 2023-12-07 10:39:05 -05:00
parent eb7c36c049
commit 6bc67ec21f
No known key found for this signature in database
4 changed files with 86 additions and 2 deletions

View file

@ -3,7 +3,7 @@
| S | M | T | W | T | F | S |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| | | | | | [1] | [2] |
| [3] | [4] | [5] | 6 | 7 | 8 | 9 |
| [3] | [4] | [5] | 6 | [7] | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | | | | | |
@ -14,3 +14,5 @@
[3]: ./lib/2023/3.ex
[4]: ./lib/2023/4.ex
[5]: ./lib/2023/5.ex
<!-- [6]: ./lib/2023/6.ex -->
[7]: ./lib/2023/7.ex

52
2023/lib/2023/7.ex Normal file
View file

@ -0,0 +1,52 @@
import AOC
import AOCHelpers
aoc 2023, 7 do
def p1(input) do
input
|> read_input()
|> Enum.map(fn [hand, bet] ->
counts =
hand
|> Enum.frequencies()
|> Map.values()
|> Enum.sort(:desc)
[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
def p2(_input) do
end
def read_input(input) do
input
|> lines()
|> Enum.map(fn line ->
line
|> words()
|> map_list([&to_numeric_hand/1, &String.to_integer/1])
end)
end
def to_numeric_hand(str) do
str
|> letters()
|> Enum.map(fn
"A" -> 14
"K" -> 13
"Q" -> 12
"J" -> 11
"T" -> 10
n -> String.to_integer(n)
end)
end
def compare_hands_1([counts, hand_a, _], [counts, hand_b, _]), do: hand_a <= hand_b
def compare_hands_1([counts_a, _, _], [counts_b, _, _]), do: counts_a <= counts_b
end

30
2023/lib/AOCHelpers.ex Normal file
View file

@ -0,0 +1,30 @@
defmodule AOCHelpers do
def lines(string) when is_binary(string) do
String.split(string, "\n")
end
def words(str) when is_binary(str) do
String.split(str)
end
def letters(str) when is_binary(str) do
String.split(str, "", trim: true)
end
@doc """
Take a list of terms and a list of 1-arity functions and apply each function
to the coresponding term in the list of terms.
## Example
iex> map_list([1, 2, 3], [&(&1 + 1), &(&1 * 100), &to_string/1])
[2, 200, "3"]
"""
def map_list(args, funs) do
Enum.zip_with(args, funs, fn arg, fun -> fun.(arg) end)
end
def id(x), do: x
def always(x), do: fn -> x end
end

View file

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