Skip to content

Commit

Permalink
2024 day 9, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenseacat committed Dec 9, 2024
1 parent 1c2ef0f commit 8d59eb6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 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=435%20stars&style=for-the-badge&color=green" alt="435 stars" /></p>
<p><a href="./lib/y2024/"><img src="https://img.shields.io/static/v1?label=2024&message=16%20stars&style=for-the-badge&color=orange" alt="16 stars" /></a><br />
<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 />
<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=16%20stars&style=for-the-badge&color=orange" alt="16 stars" /><!-- stars 2024 end -->
<!-- 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 -->

## Benchmarks

Expand Down Expand Up @@ -30,4 +30,5 @@ day 07, part 1 103.05 9.70 ms ±2.34% 9.69 ms 10.
day 07, part 2 14.76 67.73 ms ±1.35% 67.51 ms 71.95 ms
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
```
62 changes: 62 additions & 0 deletions lib/y2024/day09.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule Y2024.Day09 do
use Advent.Day, no: 09

@doc """
iex> Day09.part1(Integer.digits(2333133121414131402))
1928
"""
def part1(input) do
# This is how long the final list should be - the sum of all of the lengths of non-gaps
finished_length = input |> Enum.take_every(2) |> Enum.sum()

# build up the 00..111.222.. string
list =
input
|> Enum.reduce({0, true, []}, fn num, {file_id, fill?, list} ->
fill_value = if fill?, do: file_id, else: nil
next_file_id = if fill?, do: file_id + 1, else: file_id
list = if num == 0, do: list, else: [for(_i <- 1..num, do: fill_value) | list]
{next_file_id, !fill?, list}
end)
|> elem(2)
|> Enum.reverse()
|> List.flatten()

# Shorten that list and reverse it to use while backfilling
to_use = Enum.reject(list, &(!&1)) |> Enum.reverse()

Enum.reduce_while(list, {0, [], to_use}, fn num, {length, built, list} ->
if length == finished_length do
{:halt, Enum.reverse(built)}
else
if num do
{:cont, {length + 1, [num | built], list}}
else
{:cont, {length + 1, [hd(list) | built], tl(list)}}
end
end
end)
|> Enum.reduce({0, 0}, fn num, {index, acc} ->
{index + 1, acc + index * num}
end)
|> elem(1)
end

# @doc """
# iex> Day09.part2("update or delete me")
# "update or delete me"
# """
# def part2(input) do
# input
# end

def parse_input(input) do
input
|> String.trim()
|> String.to_integer()
|> Integer.digits()
end

def part1_verify, do: input() |> parse_input() |> part1()
# def part2_verify, do: input() |> parse_input() |> part2()
end
Binary file added lib/y2024/input/day09.txt
Binary file not shown.
8 changes: 8 additions & 0 deletions test/y2024/day09_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule Y2024.Day09Test do
use ExUnit.Case, async: true
alias Y2024.Day09
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")
end

0 comments on commit 8d59eb6

Please sign in to comment.