2022-11-29 — 3 min read

Advent of Code 2015, Day 7 – In Ruby

Let’s solve the puzzle for day 7 of 2015!

The first half of the puzzle

This year, Santa brought little Bobby Tables a set of wires and bitwise logic gates! Unfortunately, little Bobby is a little under the recommended age range, and he needs help assembling the circuit.

Each wire has an identifier (some lowercase letters) and can carry a 16-bit signal (a number from 0 to 65535). A signal is provided to each wire by a gate, another wire, or some specific value. Each wire can only get a signal from one source, but can provide its signal to multiple destinations. A gate provides no signal until all of its inputs have a signal.

The included instructions booklet describes how to connect the parts together: x AND y -> z means to connect wires x and y to an AND gate, and then connect its output to wire z.

For example:

  • 123 -> x means that the signal 123 is provided to wire x.
  • x AND y -> z means that the bitwise AND of wire x and wire y is provided to wire z.
  • p LSHIFT 2 -> q means that the value from wire p is left-shifted by 2 and then provided to wire q.
  • NOT e -> f means that the bitwise complement of the value from wire e is provided to wire f.

Other possible gates include OR (bitwise OR) and RSHIFT (right-shift). If, for some reason, you’d like to emulate the circuit instead, almost all programming languages (for example, C, JavaScript, or Python) provide operators for these gates.

For example, here is a simple circuit:

123 -> x
456 -> y
x AND y -> d
x OR y -> e
x LSHIFT 2 -> f
y RSHIFT 2 -> g
NOT x -> h
NOT y -> i

After it is run, these are the signals on the wires:

d: 72
e: 507
f: 492
g: 114
h: 65412
i: 65079
x: 123
y: 456

In little Bobby’s kit’s instructions booklet (provided as your puzzle input), what signal is ultimately provided to wire a?

The solution:

#!/usr/bin/env ruby

class Circuit
  TRANSFORMATIONS = {
    'LSHIFT' => '<<',
    'RSHIFT' => '>>',
    'NOT'    => '~',
    'AND'    => '&',
    'OR'     => '|',
    /\b([a-z]+)\b/  => '\\1_'
  }

  def add(line)
    TRANSFORMATIONS.each do |from, to|
      line.gsub!(from, to)
    end

    exp, name = line.strip.split(' -> ')

    method = "def #{name}; @#{name} ||= #{exp}; end"

    instance_eval method
  end
end

circuit = Circuit.new

ARGF.each_line do |line|
  circuit.add(line)
end

puts "The signal provided to wire a: #{circuit.a_}"

The basic idea is to generate a method for each wire, then at the end, call the method for wire a, which will recursively compute the necessary intermediate values. The catch is that some of these computations are deeply recursive, and are also used multiple times in the computations, so we’ll need to cache their values to cut back on the time it takes to get the answer.

First, we create the Circuit class, then define the necessary transformations to convert the textual instructions to Ruby bitwise operators. The last transformation is necessary because some of the wire names (do, if, in) clash with Ruby keywords, so we append an underscore to every wire name.

For each input line, we run the transformations, then split out the expression part and the method name part. Next, we construct the method definition string, then evaluate it, which will create the method in the Circuit class. Lastly, we call the method for wire a, which will recursively compute everything.

The second half of the puzzle

Now, take the signal you got on wire a, override wire b to that signal, and reset the other wires (including wire a). What new signal is ultimately provided to wire a?

The solution:

#!/usr/bin/env ruby

class Circuit
  TRANSFORMATIONS = {
    'LSHIFT' => '<<',
    'RSHIFT' => '>>',
    'NOT'    => '~',
    'AND'    => '&',
    'OR'     => '|',
    /\b([a-z]+)\b/  => '\\1_'
  }

  def add(line)
    TRANSFORMATIONS.each do |from, to|
      line.gsub!(from, to)
    end

    exp, name = line.strip.split(' -> ')

    method = "def #{name}; @#{name} ||= #{exp}; end"

    instance_eval method
  end
end

circuit = Circuit.new

ARGF.each_line do |line|
  circuit.add(line)
end

circuit.add('16076 -> b')

puts "The signal provided to wire a: #{circuit.a_}"

We only need to add a single line: once we parsed the input, we overwrite the definition of b with the value of a from the first part. Since nothing is computed at this point, once we call the a_ method, everything will be computed with the new value of b.

The code with my input text is available in the GitHub repo.

Thanks for reading! If you have any comments, additions, or corrections, feel free to reach me via e-mail.

Copyright © 2023 csm.hu
Contact