-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a2d354
commit 63c7ce3
Showing
7 changed files
with
89 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |