Day 2: Fulminating Farmland

Link to Day 2

Problem One

A robot follows a trail of colored waypoints. The waypoints are repeated in the following order: red (r), orange (o), yellow (y), green (g), blue (b), and violet (v). The robot has a series of instructions -- a program -- that it follows to tell it the color of waypoint to go next. The program may look like:

rbgvrobrbr...

The robot then goes to the next waypoint of the indicated color.

Assuming the robot starts from a red waypoint (r), write a program that, given any set of instructions in this format, will return the exact number of steps (1 step = the distance between two adjacent waypoints) the robot must travel to complete the entire process.

Solution
  1. Map the first letter of each color to an integer, given the order the colors in the trail (r-o-y-g-b-v).
    • r = 1, o = 2, y = 3, g = 4, b = 5, v = 6; this is easiest if you make it a dictionary or object:
      { r: 1, o: 2, y: 3, g: 4, b: 5, v: 6 }
    • these are relative positions, and can be used to find the number of steps between each waypoint
  2. Set a variable current = 1 which is the relative position for r. This is the initial position of the robot.
  3. Set a variable distance = 0, since the robot hasn't moved to start.
  4. Perform the following loop through each instruction of the input:
    • set a variable next as equal to your colormap value for that particular instruction: let next = colormap[instruction]
    • go through the following if/else if statements (adjusting for language):
      • if next > current then distance += next - current
      • else if next < current then distance += 6 - current + next
      • else if current == next then distance += 6
    • set current = next
  5. When the loop is complete, return distance.
function robotDistance(inputStr) {
    const input = inputStr.split("")
    const colormap = {
        r: 1,
        o: 2,
        y: 3,
        g: 4, 
        b: 5,
        v: 6
    }

    var distance = 0
    var cur = colormap["r"]

    for (let i of input) {
        let next = colormap[i]
        if (next > cur) {
            distance += next - cur
        } else if (next < cur) {
            distance += 6 - cur + next
        } else if (next === cur) {
            distance += 6
        }
        cur = next
    }
    return distance
}
import copy # we need to import a built-in module for this to work right as we will see.
def robotDistance(inputStr):
    input = list(inputStr)
    colormap = {
        "r": 1,
        "o": 2,
        "y": 3,
        "g": 4,
        "b": 5,
        "v": 6
    }
    stepcounts = list() # instead of initializing an integer that we can add to, 
    # we are going to be adding all the step counts between waypoints in a list. 
    current = int()

    for val in range(len(input)):
        if val == 0:
            current = colormap["r"]
        next = colormap[input[val]]
        if next > current:
            stepcounts.append(next - current)
        elif next < current:
            stepcounts.append(6 - current + next)
        elif next == current:
            stepcounts.append(6)
        current = copy.copy(next) # python binds variables on both sides together, 
        # so if we just set current = next, 
        # then changing current would change next and vice versa. 
        # Instead, we need to create a copy.  
    
    return sum(stepcounts)

Problem Two

Rearrange the letters in the input program so that the robot will cover the maximum distance possible.

Solution

Note: When the robot is at a waypoint of a particular color, the distance to the next waypoint of the same color is 6. This is the longest possible distance as long as there is still a waypoint of that color available. If there is no waypoint left of the same color, then the robot would need to travel to a waypoint that is at a distance of 5, or the next longest available.

  1. Like in part 1, create a color map to map each color to an integer: { r: 1, o: 2, y: 3, g: 4, b: 5, v: 6 }
  2. Extract the letters from the colors into a list: ["r", "o", "y", "g", "b", "v"]
  3. Remove the first letter of the list from step 2, then append it to the end of the list: ["o", "y", "g", "b", "v", "r"]
  4. Reverse the order of the list: ["r", "v", "b", "g", "y", "o"]
  5. Rearrange the "instructions" from the input so that they are sorted in the order of the list from step 4.
  6. Repeat the function from part 1 with the new "rearranged" instructions.
function rearrangeInstructions(inputStr) {
    const input = inputStr.split("")
    const colormap = {
        r: 1, o: 2, y: 3, g: 4, b: 5, v: 6
    }
    var colors = Object.keys(colormap)
    var r = colors.splice(0, 1)
    colors.push(r)
    colors.sort((a,b) => b - a)
    var rearranged = colors.flatMap(c => input.filter(letter => letter === c)); 
    var rearrangedStr = rearranged.join()
    robotDistance(rearrangedStr)
}
def rearrangeInstructions(inputStr):
    input = list(inputStr)
    colormap = {
        "r": 1,
        "o": 2,
        "y": 3,
        "g": 4,
        "b": 5,
        "v": 6
    }
    colors = list(colormap.keys())
    r = colors.pop(0)
    colors.append(r)
    colors.reverse()
    rearranged = sorted(input, key=colors.index)
    rearrangedStr = "".join(rearranged)
    robotDistance(rearrangedStr)