Hello again.

And I've already fallen behind on the problems. Time really does wait for no one, huh?

Anyway, the second problem wasn't so bad (at least, after I got some clarification on what the problem is asking from my friend, Rafael). Essentially, we have some pointer starting at the beginning of the repeating sequence roygbv. The input could be thought of as a tape of targets to go to from your current index, one step at a time. I.e., if our pointer started here...

roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
^

... and the program we receive is something like rrbvg, then the sequence of moves would be (I'm using + to denote the last previous position, * for previous positions in general)...

0. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   ^
(move to the next r)

1. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   +-----^

(move to the next r)

2. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   *-----+-----^

(move to the next b)

3. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   *-----*-----+---^

... and so on and so forth. The goal then, given a particular program, is to count the total distance taken to get from the first position to the very last.

The solution I came up with was this (for the first part, anyway):

use std::collections::HashSet; // ...is a surprise tool that will help us later.
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process;

fn char_to_idx(char: char) -> i32 {
    match char {
        'r' => 1,
        'o' => 2,
        'y' => 3,
        'g' => 4,
        'b' => 5,
        'v' => 6,
        _ => {
            eprintln!("Encountered unknown character: {}", char);
            process::exit(1);
        }
    }
}

fn main() {
    let path: PathBuf = match env::args().nth(1) {
        Some(arg) => PathBuf::from(arg),
        None => {
            let binary: String = env::args().next().unwrap();
            eprintln!("Usage: {} <file>", binary);
            process::exit(1);
        }
    };

    let contents = match fs::read_to_string(&path) {
        Ok(text) => text,
        Err(e) => {
            eprintln!("Couldn't read {}: {}", path.display(), e);
            process::exit(1);
        }
    };

    let chars = contents.trim().chars();

    let mut idx = 1;
    let distance = chars.clone().fold(0, |acc, x| {
        let target = char_to_idx(x);
        let step = (target - idx - 1).rem_euclid(6) + 1;
        idx = target;
        acc + step
    });

    println!("robot distaince: {}", distance);
}

I came to this because I realized I could probably calculate the distance between the target and the current index, and take the modulo (since the sequence always repeats, and with the length of this sequence, this value stays under mod 6). I then fold it into a final sum, and that yields the answer.

For part two, the problem asks us to maximize this distance by re-ordering the program.

My thinking was:

  • We always start on R, and the furthest you can travel in one move is 6 units, because that's the distance to the next R.
  • So we put all of the Rs at the beginning of the program.
  • But after we've exhausted all of the Rs, the next furthest letter to jump would be the one at the end of the sequence or, alternatively, the letter 1 position behind the one we're at within the repeating sequence. I.e., V.
  • So we put all the Vs next.
  • So on and so forth.

From this, the max distance starts looking like it should be 6 times the length of the program, because ideally, you're making a jump of 6 each move. However, this doesn't account for the transitions between letters. If you consider jumping to a V from an R, that's only 5 units apart.

I kind of cheesed the answer but I think I know what the proper answer would be:

...

// i don't think this works for all inputs but it worked for me so whoops
    let unique = chars.clone().collect::<HashSet<_>>().len();
    let length = chars.as_str().len();
    let max_dist = (6 * length) - unique;
    println!("max dist: {}", max_dist);

...

This did get me the answer, but I don't think it's fully correct. I think what I should've done is subtracted 6 * length by the "backwards distance" between R and the next closest letter. E.g., if our program was rrvv, the above gives us 22 as the answer. However, if we actually do the jumps...

1. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   ^
(move to the next r)

1. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   +-----^

(move to the next r)

2. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   *-----+-----^

(move to the next v)

3. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   *-----*-----+----^

(move to the next v)

4. roygbvroygbvroygbvroygbvroygbvroygbvroygbvroygbv...
   *-----*-----*----*-----^

...you'll see that we've actually travelled a distance of 23. If we look at the "backwards distance" between R and V, it's 1. (6 * 4) - 1) gives us the right answer.

I'm not certain about this though, so I'll revisit it. I'm leaving it like this for now because I think it's a little funny I managed to cheese the problem.

I did take a look at the third problem and it looks disgusting, honestly. Not sure when I'll get to it, but I will get to it eventually.

That's all for now. See ya when I try solving the next problem.