-= 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:
#: Gear.*: Light.S: The Starting Gear (there is exactly one).- Any other character: Irrelevant for now.
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:
- A gear rotating clockwise will rotate adjacent gears counter-clockwise.
- A gear rotating counter-clockwise will rotate adjacent gears clockwise.
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 (*).
- A light is high if it is adjacent to at least one clockwise (
R) gear. - A light is low if it is adjacent to at least one counter-clockwise (
L) gear. - A light not adjacent to any rotating gear remains off.
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:
- Off (not adjacent to any rotated gear)
- Low (adjacent to a counter-clockwise gear)
- Low
- Off
- Low
- High (adjacent to a clockwise gear).
- Low
- Off
- Low
- Off
- 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?