Skip to content

Commit

Permalink
2024 day 9 part 2 - this needs optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Dec 9, 2024
1 parent 8d59eb6 commit 8e747d1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years).

<!-- stars start -->
<p><img src="https://img.shields.io/static/v1?label=Total&message=436%20stars&style=for-the-badge&color=green" alt="436 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=17%20stars&style=for-the-badge&color=orange" alt="17 stars" /></a><br />
<p><img src="https://img.shields.io/static/v1?label=Total&message=437%20stars&style=for-the-badge&color=green" alt="437 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=18%20stars&style=for-the-badge&color=orange" alt="18 stars" /></a><br />
<a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=44%20stars&style=for-the-badge&color=green" alt="44 stars" /></a><br />
<a href="./lib/y2022/"><img src="https://img.shields.io/static/v1?label=2022&message=%E2%AD%90%EF%B8%8F%2050%20stars%20%E2%AD%90%EF%B8%8F&style=for-the-badge&color=brightgreen" alt="50 stars" /></a><br />
<a href="./lib/y2021/"><img src="https://img.shields.io/static/v1?label=2021&message=46%20stars&style=for-the-badge&color=green" alt="46 stars" /></a><br />
Expand Down
3 changes: 2 additions & 1 deletion lib/y2024/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

My Elixir solutions for [Advent of Code 2024](https://adventofcode.com/2024).

<!-- stars 2024 start --><img src="https://img.shields.io/static/v1?label=2024&message=17%20stars&style=for-the-badge&color=orange" alt="17 stars" /><!-- stars 2024 end -->
<!-- stars 2024 start --><img src="https://img.shields.io/static/v1?label=2024&message=18%20stars&style=for-the-badge&color=orange" alt="18 stars" /><!-- stars 2024 end -->

## Benchmarks

Expand Down Expand Up @@ -31,4 +31,5 @@ day 07, part 2 14.76 67.73 ms ±1.35% 67.51 ms 71.
day 08, part 1 1.07 K 0.93 ms ±6.91% 0.90 ms 1.07 ms
day 08, part 2 0.83 K 1.20 ms ±7.78% 1.17 ms 1.38 ms
day 09, part 1 9.27 107.87 ms ±0.84% 107.61 ms 111.83 ms
day 09, part 2 0.23 4.43 s ±0.12% 4.43 s 4.43 s
```
67 changes: 59 additions & 8 deletions lib/y2024/day09.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,64 @@ defmodule Y2024.Day09 do
|> elem(1)
end

# @doc """
# iex> Day09.part2("update or delete me")
# "update or delete me"
# """
# def part2(input) do
# input
# end
@doc """
iex> Day09.part2(Integer.digits(2333133121414131402))
2858
"""
def part2(input) do
input
|> Enum.reduce({0, true, []}, fn num, {file_id, fill?, list} ->
type = if fill?, do: :fill, else: :gap
next_file_id = if fill?, do: file_id + 1, else: file_id
{next_file_id, !fill?, [%{type: type, file_id: file_id, num: num} | list]}
end)
|> elem(2)
|> Enum.reverse()
|> defrag([])
|> Enum.reduce({0, 0}, fn record, {index, acc} ->
if record.type == :gap do
{index + record.num, acc}
else
acc =
Enum.reduce(0..(record.num - 1), acc, fn sub_index, acc ->
acc + (index + sub_index) * record.file_id
end)

{index + record.num, acc}
end
end)
|> elem(1)
end

defp defrag([], list), do: Enum.reverse(list)

defp defrag([%{type: :fill} = head | tail], list) do
defrag(tail, [head | list])
end

defp defrag([%{type: :gap, num: num} = head | tail], list) do
filler =
Enum.find(Enum.reverse(tail), fn %{num: check_num, type: type} ->
type == :fill && check_num <= num
end)

if filler do
list = [filler | list]

tail =
Enum.map(tail, fn record ->
if record == filler, do: %{type: :gap, num: filler.num}, else: record
end)

if num - filler.num == 0 do
defrag(tail, list)
else
defrag([%{type: :gap, num: num - filler.num} | tail], list)
end
else
defrag(tail, [head | list])
end
end

def parse_input(input) do
input
Expand All @@ -58,5 +109,5 @@ defmodule Y2024.Day09 do
end

def part1_verify, do: input() |> parse_input() |> part1()
# def part2_verify, do: input() |> parse_input() |> part2()
def part2_verify, do: input() |> parse_input() |> part2()
end
2 changes: 1 addition & 1 deletion test/y2024/day09_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ defmodule Y2024.Day09Test do
doctest Day09

test "verification, part 1", do: assert(Day09.part1_verify() == 6_241_633_730_082)
# test "verification, part 2", do: assert(Day09.part2_verify() == "update or delete me")
test "verification, part 2", do: assert(Day09.part2_verify() == 6_265_268_809_555)
end

0 comments on commit 8e747d1

Please sign in to comment.