Skip to content

Commit

Permalink
Solve 2023/15
Browse files Browse the repository at this point in the history
  • Loading branch information
code-shoily committed Dec 15, 2023
1 parent 6a2d354 commit 63c7ce3
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ Note that you can call those commands independently and also set up pre-commit h

Note: All files mentioned above are autogenerated and are created by running `mix update_stats` task.

## :trophy: 275/450
## :trophy: 277/450

| Day | [2015](/lib/2015) | [2016](/lib/2016) | [2017](/lib/2017) | [2018](/lib/2018) | [2019](/lib/2019) | [2020](/lib/2020) | [2021](/lib/2021) | [2022](/lib/2022) | [2023](/lib/2023) |
|:---:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
| :star2: | 48 | 22 | 34 | 15 | 15 | 44 | 24 | 45 | 28 |
| :star2: | 48 | 22 | 34 | 15 | 15 | 44 | 24 | 45 | 30 |
| 1 | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
| 2 | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
| 3 | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
Expand All @@ -80,7 +80,7 @@ Note: All files mentioned above are autogenerated and are created by running `mi
| 12 | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | | | :1st_place_medal: | | :1st_place_medal: | :1st_place_medal: |
| 13 | :1st_place_medal: | | :1st_place_medal: | | | :1st_place_medal: | | :1st_place_medal: | :1st_place_medal: |
| 14 | :1st_place_medal: | | :2nd_place_medal: | | | :1st_place_medal: | | :1st_place_medal: | :1st_place_medal: |
| 15 | :1st_place_medal: | | :1st_place_medal: | | | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | |
| 15 | :1st_place_medal: | | :1st_place_medal: | | | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: | :1st_place_medal: |
| 16 | :1st_place_medal: | | :1st_place_medal: | | | :2nd_place_medal: | :1st_place_medal: | | |
| 17 | :1st_place_medal: | | :1st_place_medal: | | | | | | |
| 18 | :1st_place_medal: | | :2nd_place_medal: | | | :1st_place_medal: | | :1st_place_medal: | |
Expand Down
1 change: 1 addition & 0 deletions difficulties.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ optimize them and update this page with refined and more accurate data.
| 2023/2 | [Cube Conundrum](https://adventofcode.com/2023/day/2) | :1st_place_medal: | :snowflake: :snowflake: | [day_02.ex](/lib/2023/day_02.ex) | [day_02_test.exs](/test/2023/day_02_test.exs) | [reduction](/tags.md#reduction) |
| 2023/8 | [Haunted Wasteland](https://adventofcode.com/2023/day/8) | :1st_place_medal: | :snowflake: :snowflake: | [day_08.ex](/lib/2023/day_08.ex) | [day_08_test.exs](/test/2023/day_08_test.exs) | [arithmetic](/tags.md#arithmetic), [sequence](/tags.md#sequence) |
| 2023/11 | [Cosmic Expansion](https://adventofcode.com/2023/day/11) | :1st_place_medal: | :snowflake: :snowflake: | [day_11.ex](/lib/2023/day_11.ex) | [day_11_test.exs](/test/2023/day_11_test.exs) | [grid](/tags.md#grid), [measurement](/tags.md#measurement) |
| 2023/15 | [Lens Library](https://adventofcode.com/2023/day/15) | :1st_place_medal: | :snowflake: :snowflake: | [day_15.ex](/lib/2023/day_15.ex) | [day_15_test.exs](/test/2023/day_15_test.exs) | [hash](/tags.md#hash), [ordered-map](/tags.md#ordered-map) |

## m

Expand Down
3 changes: 2 additions & 1 deletion lib/2023/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[2015](/lib/2015) | [2016](/lib/2016) | [2017](/lib/2017) | [2018](/lib/2018) | [2019](/lib/2019) | [2020](/lib/2020) | [2021](/lib/2021) | [2022](/lib/2022) | 2023


## :trophy: 28/50
## :trophy: 30/50

| Day | Problem Page | Status | Difficulty | Solution Page | Test Page | Tags |
| :---: | :------: | :---: | :---: | :---: | :---: | :---: |
Expand All @@ -24,4 +24,5 @@
| 12 | [Hot Springs](https://adventofcode.com/2023/day/12) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_12.ex](/lib/2023/day_12.ex) | [day_12_test.exs](/test/2023/day_12_test.exs) | [memoization](/tags.md#memoization), [vector](/tags.md#vector) |
| 13 | [Point of Incidence](https://adventofcode.com/2023/day/13) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: | [day_13.ex](/lib/2023/day_13.ex) | [day_13_test.exs](/test/2023/day_13_test.exs) | [sliding-window](/tags.md#sliding-window), [palindrome](/tags.md#palindrome) |
| 14 | [Parabolic Reflector Dish](https://adventofcode.com/2023/day/14) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: | [day_14.ex](/lib/2023/day_14.ex) | [day_14_test.exs](/test/2023/day_14_test.exs) | [grid](/tags.md#grid), [grid-rotation](/tags.md#grid-rotation), [modular-arithmetic](/tags.md#modular-arithmetic), [memoization](/tags.md#memoization) |
| 15 | [Lens Library](https://adventofcode.com/2023/day/15) | :1st_place_medal: | :snowflake: :snowflake: | [day_15.ex](/lib/2023/day_15.ex) | [day_15_test.exs](/test/2023/day_15_test.exs) | [hash](/tags.md#hash), [ordered-map](/tags.md#ordered-map) |

63 changes: 63 additions & 0 deletions lib/2023/day_15.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule AdventOfCode.Y2023.Day15 do
@moduledoc """
--- Day 15: Lens Library ---
Problem Link: https://adventofcode.com/2023/day/15
Difficulty: s
Tags: hash ordered-map
"""
alias AdventOfCode.Helpers.InputReader
alias Aja.OrdMap

def input, do: InputReader.read_from_file(2023, 15)

def run(input \\ input()) do
input = parse_1(input)
{run_1(input), run_2(input)}
end

defp run_1(input), do: Enum.reduce(input, 0, &(&2 + hash(&1)))

defp run_2(input) do
input
|> parse_2()
|> Enum.reduce(%{}, fn
{op, b, len}, acc ->
hash = hash(b)

case op do
:add -> Map.update(acc, hash, OrdMap.new(%{b => len}), &OrdMap.put(&1, b, len))
:remove -> Map.update(acc, hash, OrdMap.new(%{}), &OrdMap.drop(&1, [b]))
end
end)
|> total_focus()
end

defp total_focus(map) do
Enum.reduce(map, 0, fn {box, boxes}, acc ->
acc +
(boxes
|> OrdMap.to_list()
|> Enum.with_index(1)
|> Enum.reduce(0, &(&2 + row_focus(&1, box))))
end)
end

defp row_focus({{_, len}, slot}, box), do: (box + 1) * slot * len

def parse_1(input \\ input()), do: String.split(input, ",", trim: true)

def parse_2(commands) do
Enum.map(commands, fn cmd ->
case Regex.run(~r{([a-zA-Z]+)(=|-)([0-9]*)}, cmd) do
[_, label, "=", len] -> {:add, label, String.to_integer(len)}
[_, label, "-", ""] -> {:remove, label, 0}
end
end)
end

def hash(lst) do
for ch <- String.to_charlist(lst), reduce: 0 do
acc -> rem((acc + ch) * 17, 256)
end
end
end
1 change: 1 addition & 0 deletions priv/input_files/2023_15.txt

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ optimize them and update this page with refined and more accurate data.
| :---: | :------: | :---: | :---: | :---: | :---: | :---: |
| 2017/10 | [Knot Hash](https://adventofcode.com/2017/day/10) | :1st_place_medal: | :snowflake: :snowflake: | [day_10.ex](/lib/2017/day_10.ex) | [day_10_test.exs](/test/2017/day_10_test.exs) | [hash](/tags.md#hash) |
| 2017/14 | [Disk Defragmentation](https://adventofcode.com/2017/day/14) | :2nd_place_medal: | :snowflake: :snowflake: :snowflake: | [day_14.ex](/lib/2017/day_14.ex) | [day_14_test.exs](/test/2017/day_14_test.exs) | [not-fast-enough](/tags.md#not-fast-enough), [half-done](/tags.md#half-done), [hash](/tags.md#hash) |
| 2023/15 | [Lens Library](https://adventofcode.com/2023/day/15) | :1st_place_medal: | :snowflake: :snowflake: | [day_15.ex](/lib/2023/day_15.ex) | [day_15_test.exs](/test/2023/day_15_test.exs) | [hash](/tags.md#hash), [ordered-map](/tags.md#ordered-map) |

## hexagon

Expand Down Expand Up @@ -442,6 +443,12 @@ optimize them and update this page with refined and more accurate data.
| 2022/19 | [Not Enough Minerals](https://adventofcode.com/2022/day/19) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: :snowflake: :snowflake: | [day_19.ex](/lib/2022/day_19.ex) | [day_19_test.exs](/test/2022/day_19_test.exs) | [erlang](/tags.md#erlang), [slow](/tags.md#slow), [needs-improvement](/tags.md#needs-improvement), [grid](/tags.md#grid), [walk](/tags.md#walk), [optimization](/tags.md#optimization) |
| 2022/24 | [Blizzard Basin](https://adventofcode.com/2022/day/24) | :1st_place_medal: | :snowflake: :snowflake: :snowflake: | [day_24.ex](/lib/2022/day_24.ex) | [day_24_test.exs](/test/2022/day_24_test.exs) | [erlang](/tags.md#erlang), [slow](/tags.md#slow), [optimization](/tags.md#optimization), [grid](/tags.md#grid), [search](/tags.md#search) |

## ordered-map

| Day | Problem Page | Status | Difficulty | Solution Page | Test Page | Tags |
| :---: | :------: | :---: | :---: | :---: | :---: | :---: |
| 2023/15 | [Lens Library](https://adventofcode.com/2023/day/15) | :1st_place_medal: | :snowflake: :snowflake: | [day_15.ex](/lib/2023/day_15.ex) | [day_15_test.exs](/test/2023/day_15_test.exs) | [hash](/tags.md#hash), [ordered-map](/tags.md#ordered-map) |

## palindrome

| Day | Problem Page | Status | Difficulty | Solution Page | Test Page | Tags |
Expand Down
12 changes: 12 additions & 0 deletions test/2023/day_15_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule AdventOfCode.Y2023.Day15Test do
@moduledoc false

use ExUnit.Case
@moduletag :y2315

alias AdventOfCode.Y2023.Day15, as: Solution

test "Year 2023, Day 15 run/1" do
assert Solution.run() == {510_273, 212_449}
end
end

0 comments on commit 63c7ce3

Please sign in to comment.