You are in a locked room. A cell? A box, of some sort? Walls, ceiling and floor are all the same worn industrial metal. There is a small door, but it seems secured with some old kind of mechanism. One of those old safebox locks where you spin a knob alternatively left and right to get to the right sequence. "Who puts the lock on the inside of a cell?" you ponder.
"It doesn't matter. I need to get out of here."
There are scribblings and scratching on the walls and on the floor. Mainly in one of the corners, the corner where you found yourself sitting just a while ago. Only one thing stands out; a long series of letters, your puzzle inputs.
As you examine the lock's mechanism, it is clear to you that it is ancient. It is covered by corrosion, and its dial only seems to go in one direction, to your right. Something in the lock seems broken, or loose inside of it, as there seem to be something stopping it from rotating to your left. The face of the dial has numbers on it, labelled 1 through 9.
"Time to get working on getting this thing opened and find out what the hell is going on."
The dial starts out pointing at label 1
. The scribblings that you found is a long sequence of the characters R
and L
. These seem to be the sequence which you need to turn the dial of the lock in order to unlock it. There is a problem, however; the dial cannot move to the left.
This can be solved by translating any instruction to turn the dial to the left, to a corresponding right turn. A left turn of 11
, for example, would leave the dial at label 8
, which means that the smallest number of turns to the right that is its equivalent, is 7
to the right.
After each step, you will have turned the dial to the right a certain number of steps ending up at a specific number label.
For each step, multiply the number of right-turns done by the label it ends up on. The sum of these products is your answer!
Here is an example given the input RRRRLLRRRRRRRRRRLLLLLL
:
Start at 1
RRRR turn the dial 4 to the right having it land on label 5
LL turn the dial 2 to the left, is equivalent to turning it 7 right, to label 3
RRRRRRRRRR turn the dial 10 steps to the right, to label 4
LLLLLL turn the dial 6 steps to the left, equivalent to turning it 3 right, to label 7
Which would sum up to: (4 * 5) + (7 * 3) + (10 * 4) + (3 * 7) = 102
Some example tests:
package dialingsafe
import kotlin.test.assertEquals
import org.junit.Test
class DialingSafeTest {
@Test
fun oneRight() {
assertEquals(2, solve("R"))
}
@Test
fun twoRight() {
assertEquals(6, solve("RR"))
}
@Test
fun exampleFromDescription() {
assertEquals(102, solve("RRRRLLRRRRRRRRRRLLLLLL"))
}
@Test
fun mix() {
assertEquals(80, solve("LLLLLLLLLLLRRRRRRRRRRRR"))
}
@Test
fun veryLong() {
assertEquals(995, solve("LRRRLLLLLLLLLLLLLRRRRRRLRRLLRLLRLRRLLLLRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRLLLLRRLLRRRRLLLRRRLRLRRRRRLRRRLLLLLLLLLLLLLLLLLLLLLLLLLLLLRLRRRLRRLRLLRLLLRLLRRRRLLRLLLLRRRRLLRL"))
}
}