-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday_15.ex
63 lines (53 loc) · 1.57 KB
/
day_15.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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