Advent of Code: Day 7
Some Assembly Required

First puzzle

As an electronic engineer, this puzzle is fascinating to me. A digital circuit needs to be simulated, and the puzzle solution is the value of one of the wires after everything is connected. This type of problems is very common while you are learning digital electronics. I encounter this again after so long, and the fact is that I have never tried to write code to do this. There was some software for simulating this or you need to do it yourself on a test. The puzzle input is the description of the circuit to be simulated. The circuit is described as a set of wires and gates connecting those wires. The values on the wires are represented as a 16-bit value. Here are the gates definitions:

The author provides a sample circuit to try your program. Very important because it is very easy to make a mistake:

Sample circuit:

Wire’s values:

We need to find the value of the wire “a” based on the puzzle input, but first, we need to parse and model the input to make our life easier. Using regular expressions, each gate connection is converted to a case class:

To calculate the final value of a wire, we can go backwards using recursion. Ex. Find the gate connected to the wire, then calculate the inputs and apply the gate operation:

Second puzzle

For the second puzzle, we are asked to reset all the wired to zero, and to assign the first puzzle solution to the wire “b”. It can be done creating another circuit where there are no values assigned to any wire by filtering the first puzzle circuit eliminating all the Value case classes, and adding an assignment to the wire “b”. The puzzle solution continues to be the value of wire “a”:

You can find this code along with my input and puzzle answers at here.

*****
Written by Darien Martinez Torres on 07 March 2016