Skip to content

Commit

Permalink
2023 day 15, part 2
Browse files Browse the repository at this point in the history
That was suspiciously easy...
  • Loading branch information
sevenseacat committed Dec 15, 2023
1 parent 1cba17a commit 01e3e92
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
My Elixir solutions for [Advent of Code](https://adventofcode.com/) (all years).

<!-- stars start -->
<p><a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=29%20stars&style=for-the-badge&color=yellow" alt="29 stars" /></a><br />
<p><a href="./lib/y2023/"><img src="https://img.shields.io/static/v1?label=2023&message=30%20stars&style=for-the-badge&color=yellow" alt="30 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 />
<a href="./lib/y2020/"><img src="https://img.shields.io/static/v1?label=2020&message=39%20stars&style=for-the-badge&color=yellow" alt="39 stars" /></a><br />
Expand Down
4 changes: 3 additions & 1 deletion lib/y2023/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 2023](https://adventofcode.com/2023).

<!-- stars 2023 start --><img src="https://img.shields.io/static/v1?label=2023&message=29%20stars&style=for-the-badge&color=yellow" alt="29 stars" /><!-- stars 2023 end -->
<!-- stars 2023 start --><img src="https://img.shields.io/static/v1?label=2023&message=30%20stars&style=for-the-badge&color=yellow" alt="30 stars" /><!-- stars 2023 end -->

## Benchmarks

Expand Down Expand Up @@ -43,4 +43,6 @@ day 13, part 1 150.89 6.63 ms ±5.72% 6.62 ms 7.
day 13, part 2 3.68 271.37 ms ±1.28% 270.72 ms 283.72 ms
day 14, part 1 18.61 53.73 ms ±9.41% 53.71 ms 67.81 ms
day 14, part 2 0.63 1575.73 ms ±3.34% 1569.17 ms 1644.07 ms
day 15, part 1 1.97 K 0.51 ms ±58.77% 0.47 ms 1.38 ms
day 15, part 2 0.39 K 2.57 ms ±9.13% 2.56 ms 3.05 ms
```
75 changes: 60 additions & 15 deletions lib/y2023/day15.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ defmodule Y2023.Day15 do
use Advent.Day, no: 15

@doc """
iex> Day15.part1(['rn=1', 'cm-', 'qp=3', 'cm=2', 'qp-', 'pc=4', 'ot=9',
...> 'ab=5', 'pc-', 'pc=6', 'ot=7'])
iex> Day15.part1(~W(rn=1 cm- qp=3 cm=2 qp- pc=4 ot=9 ab=5 pc- pc=6 ot=7))
1320
"""
def part1(input) do
Expand All @@ -12,41 +11,87 @@ defmodule Y2023.Day15 do
|> Enum.sum()
end

# @doc """
# iex> Day15.part2("update or delete me")
# "update or delete me"
# """
# def part2(input) do
# input
# end
@doc """
iex> Day15.part2(~W(rn=1 cm- qp=3 cm=2 qp- pc=4 ot=9 ab=5 pc- pc=6 ot=7))
145
"""
def part2(input) do
input
|> Enum.reduce(%{}, &place_lens/2)
|> to_focussing_power()
end

defp place_lens(lens, map) do
{label, op} = parse_lens(lens)
box_no = hash(label)

case op do
"-" ->
Map.update(map, box_no, [], fn box ->
Enum.reject(box, fn {x, _} -> x == label end)
end)

{"=", focal_length} ->
entry = {label, focal_length}

Map.update(map, box_no, [entry], fn list ->
index = Enum.find_index(list, fn {x, _} -> x == label end)

if index do
List.replace_at(list, index, entry)
else
list ++ [entry]
end
end)
end
end

defp parse_lens(string) do
if String.contains?(string, "-") do
label = String.split(string, "-") |> hd()
{label, "-"}
else
[label, num] = String.split(string, "=", parts: 2)
{label, {"=", String.to_integer(num)}}
end
end

defp to_focussing_power(map) do
Enum.reduce(map, 0, fn {box_no, lenses}, acc ->
Enum.reduce(lenses, {1, acc}, fn {_label, length}, {index, acc2} ->
{index + 1, acc2 + (box_no + 1) * index * length}
end)
|> elem(1)
end)
end

@doc """
iex> Day15.hash('rn=1')
iex> Day15.hash("rn=1")
30
iex> Day15.hash('cm-')
iex> Day15.hash("cm-")
253
iex> Day15.hash('qp=3')
iex> Day15.hash("qp=3")
97
"""
def hash(string) do
string
|> String.to_charlist()
|> Enum.reduce(0, fn char, acc ->
rem((acc + char) * 17, 256)
end)
end

@doc """
iex> Day15.parse_input("rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7")
['rn=1', 'cm-', 'qp=3', 'cm=2', 'qp-', 'pc=4', 'ot=9', 'ab=5', 'pc-', 'pc=6', 'ot=7']
~W(rn=1 cm- qp=3 cm=2 qp- pc=4 ot=9 ab=5 pc- pc=6 ot=7)
"""
def parse_input(input) do
input
|> String.split(",", trim: true)
|> Enum.map(&String.to_charlist/1)
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/y2023/day15_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ defmodule Y2023.Day15Test do
doctest Day15

test "verification, part 1", do: assert(Day15.part1_verify() == 513_172)
# test "verification, part 2", do: assert(Day15.part2_verify() == "update or delete me")
test "verification, part 2", do: assert(Day15.part2_verify() == 237_806)
end

0 comments on commit 01e3e92

Please sign in to comment.