Created
December 15, 2023 06:07
-
-
Save rugyoga/04f3efd8f0066a11ddca988a052e1111 to your computer and use it in GitHub Desktop.
Advent of Code 2023 day 15
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
import AOC | |
aoc 2023, 15 do | |
def p1(input) do | |
input |> String.split(",", trim: true) |> Enum.map(&hash/1) |> Enum.sum() | |
end | |
def hash(string) do | |
string | |
|> to_charlist() | |
|> Enum.reduce(0, fn ascii, hash -> rem((hash + ascii) * 17, 256) end) | |
end | |
def p2(input) do | |
input | |
|> String.split(",", trim: true) | |
|> Enum.reduce(%{}, | |
fn command, boxes -> | |
if String.contains?(command, "=") do | |
[target, n] = String.split(command, "=") | |
box = hash(target) | |
n = String.to_integer(n) | |
stack = Map.get(boxes, box, []) | |
index = Enum.find_index(stack, fn {label, _} -> label == target end) | |
stack = if is_nil(index) do | |
[{target, n} | stack] | |
else | |
List.replace_at(stack, index, {target, n}) | |
end | |
Map.put(boxes, box, stack) | |
else | |
target = String.trim(command, "-") | |
box = hash(target) | |
stack = Map.get(boxes, box, [])|> Enum.reject(fn {label, _} -> label == target end) | |
Map.put(boxes, box, stack) | |
end | |
end) | |
|> Enum.flat_map(fn {box, stack} -> stack |> Enum.reverse() |> Enum.with_index(1) |> Enum.map(fn {{_, focal}, slot} -> focal * slot * (box+1) end) end) | |
|> Enum.sum() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment