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

Advent of Code 2024 - Day 24 - fork

2024/day-24-fork.livemd

Advent of Code 2024 - Day 24 - fork

Mix.install([
  {:kino_aoc, "~> 0.1.7"},
  {:libgraph, "~> 0.16.0"}
])

Section

import Bitwise
{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2024", "24", System.fetch_env!("LB_AOC_SESSION"))

Part 1

{inputs, ["" | exprs]} =
  puzzle_input
  |> String.split("\n")
  |> Enum.split_while(& &1 != "")
graph =
  exprs
  |> Enum.flat_map_reduce(0, fn line, seq ->
    [wire1, op, wire2, "->", wire3] = String.split(line)

    {[
      {wire1, wire3, {:"b#{String.downcase(op)}", seq}},
      {wire2, wire3, {:"b#{String.downcase(op)}", seq}}
    ], seq + 1}
  end)
  |> elem(0)
  |> Enum.reduce(Graph.new(), fn {in_wire, out_wire, gate}, graph ->
    Graph.add_edge(graph, in_wire, out_wire, label: gate)
  end)

Part 2

Notations

$$\odot$$ = AND

$$\oplus$$ = OR

$$\otimes$$ = XOR

$$c_i$$ is the carry bit generated at the i-th position

$$z_0 = x_0 \otimes y_0$$

$$z{45} = c{44}$$

$$zi = (x_i \otimes y_i) \otimes c{i-1}$$

$$c_0 = x_0 \odot y_0$$

$$ci = (x_i \odot y_i) \oplus ((x_i \otimes y_i) \odot c{i-1})$$