-= Puzzle 6: Gears And Lights =-

Ring ring!

You pick up the phone and hear an angry voice on the other end.

"Where are you!? Did noone brief you that you still need to work on weekends!?" they say.

I guess this is actually your sixth day at BitFlop Laboratories, and in fact not time to relax yet.

Anyways, arriving at work, you are assigned to help fix a machine in the production line.

This machine, which is responsible for displaying all the alarm and warning lights in the factory, has been malfunctioning and is not displaying the correct lights.

They ask you for help, and give you a printout of the machine's internal map (your puzzle input), which is a grid of gears and lights and other components which are not relevant right now.

The layout is represented as a grid of characters, where each character represents a component:

For example:

;&%,&/<.%~&|-!-;-+`.
=#######:@#=*3333,%!
@*+;|.|####.!3,33A&^
-<a|*!~#`!#~`3*-3|/;
S##########*@/!`-|`-
,,|@:#./,@#,,@B=@!%@
<3C!*#`~=*#;./333*.@
%3@&/#*:`~#^|/+3<!&=
|33*><:b###<<c333*~|
<&&@/:!|``:/:&:&&`,&

For clarity, let's only focus on the relevant components:

....................
.#######..#.*.......
.*.....####.........
....*..#..#...*.....
S##########*........
.....#....#.........
....*#...*#......*..
.....#*...#.........
...*....###......*..
....................

Each gear can either rotate clockwise or counter-clockwise.

Rotating a gear causes every adjacent gear (up, down, left, and right only) to rotate in the opposite direction:

The starting gear S is the first gear that is rotated and it's rotated counter-clockwise.

For the example above, the resulting gear rotations are:

....................
.LRLRLRL..R.*.......
.*.....RLRL.........
....*..L..R...*.....
LRLRLRLRLRL*........
.....L....R.........
....*R...*L......*..
.....L*...R.........
...*....LRL......*..
....................

Here, R indicates a gear is turning clockwise and L indicates a gear is turning counter-clockwise.

Your task is to determine the final state of every light (*).

Once every light has been evaluated, read the lights from left to right, top to bottom (row by row), and record whether each light is high or low.

Ignore lights that are off.

The resulting sequence of highs and lows can be interpreted as a binary number, where high is 1 and low is 0.

Convert this binary number to decimal to get the final answer.

For the example, the lights (read from left to right, top to bottom) are:

  1. Off (not adjacent to any rotated gear)
  2. Low (adjacent to a counter-clockwise gear)
  3. Low
  4. Off
  5. Low
  6. High (adjacent to a clockwise gear).
  7. Low
  8. Off
  9. Low
  10. Off
  11. Off

Then combining all the highs and lows (and ignoring the lights that are off), we get the binary number 000100 which means the answer for this example is 4 in decimal.

What is the decimal value of the binary number formed by the final state of all the lights after processing the gear rotations?

You must login to get your input and play!