Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

AoC 2023 Day 15

2023/day15.livemd

AoC 2023 Day 15

Mix.install([
  {:kino_aoc, "~> 0.1.5"}
])

Input

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2023", "15", System.fetch_env!("LB_AOC_COOKIE_SECRET"))
defmodule HASH do
  def hash(string) when is_binary(string), do: hash(String.to_charlist(string))

  def hash(string) do
    for char <- string, reduce: 0 do
      acc -> hash(char, acc)
    end
  end

  def hash(num, current) do
    next = (current + num) * 17
    Integer.mod(next, 256)
  end
end

Part 1

puzzle_input
|> String.split(",", trim: true)
|> Enum.map(&amp;HASH.hash/1)
|> Enum.sum()

Part 2