solve 2023 5.1
This commit is contained in:
parent
30003a8f39
commit
ef3788fe91
3 changed files with 68 additions and 2 deletions
|
@ -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 | | | | | |
|
||||
|
@ -13,3 +13,4 @@
|
|||
[2]: ./lib/2023/2.ex
|
||||
[3]: ./lib/2023/3.ex
|
||||
[4]: ./lib/2023/4.ex
|
||||
[5]: ./lib/2023/5.ex
|
||||
|
|
65
2023/lib/2023/5.ex
Normal file
65
2023/lib/2023/5.ex
Normal file
|
@ -0,0 +1,65 @@
|
|||
import AOC
|
||||
|
||||
aoc 2023, 5 do
|
||||
def p1(input) do
|
||||
{seeds, maps} = read_input(input)
|
||||
|
||||
seeds
|
||||
|> Enum.map(&traverse_maps(maps, &1))
|
||||
|> Enum.min()
|
||||
end
|
||||
|
||||
def p2(_input) do
|
||||
end
|
||||
|
||||
def traverse_maps(maps, start) do
|
||||
Enum.reduce(maps, start, &traverse_map/2)
|
||||
end
|
||||
|
||||
def traverse_map(ranges_and_deltas, start) do
|
||||
case Enum.find(ranges_and_deltas, fn {range, _} -> start in range end) do
|
||||
{_, delta} -> start - delta
|
||||
nil -> start
|
||||
end
|
||||
end
|
||||
|
||||
def read_input(input) do
|
||||
["seeds: " <> seeds_str, _ | map_strs] = String.split(input, "\n")
|
||||
|
||||
map_chunks =
|
||||
map_strs
|
||||
|> chunk_by_removing("")
|
||||
|> Enum.map(&Enum.drop(&1, 1))
|
||||
|> map_map(&to_integer_list/1)
|
||||
|> map_map(&to_range_and_delta/1)
|
||||
|
||||
{to_integer_list(seeds_str), map_chunks}
|
||||
end
|
||||
|
||||
def chunk_by_removing(enum, pattern) do
|
||||
Enum.chunk_while(
|
||||
enum,
|
||||
[],
|
||||
fn
|
||||
^pattern, acc -> {:cont, Enum.reverse(acc), []}
|
||||
elem, acc -> {:cont, [elem | acc]}
|
||||
end,
|
||||
&{:cont, Enum.reverse(&1), []}
|
||||
)
|
||||
end
|
||||
|
||||
def to_integer_list(str) do
|
||||
str
|
||||
|> String.split()
|
||||
|> Enum.map(&String.to_integer/1)
|
||||
end
|
||||
|
||||
def to_range_and_delta([dest_start, source_start, len]) do
|
||||
source_range = source_start..(source_start + len)
|
||||
delta = source_start - dest_start
|
||||
|
||||
{source_range, delta}
|
||||
end
|
||||
|
||||
def map_map(enum_of_enums, fun), do: Enum.map(enum_of_enums, fn enum -> Enum.map(enum, fun) end)
|
||||
end
|
|
@ -14,7 +14,7 @@
|
|||
1. [2020] **17/50** 🌟
|
||||
1. [2021] **43/50** 🌟
|
||||
1. [2022] **14/50** 🌟
|
||||
1. [2023] **8/50** 🌟
|
||||
1. [2023] **9/50** 🌟
|
||||
|
||||
[2015]: ./2015
|
||||
[2017]: ./2017
|
||||
|
|
Loading…
Reference in a new issue