Day 3: Perfectly Spherical Houses in a Vacuum

link to problem

The Problem

Santa is delivering presents to an infinite two-dimensional grid of houses.

He begins by delivering a present to the house at his starting location, and then an elf at the North Pole calls him via radio and tells him where to move next. Moves are always exactly one house to the north (^), south (v), east (>), or west (<). After each move, he delivers another present to the house at his new location.

However, the elf back at the north pole has had a little too much eggnog, and so his directions are a little off, and Santa ends up visiting some houses more than once. How many houses receive at least one present?

For example:

_> delivers presents to 2 houses: one at the starting location, and one to the east.

^>v< delivers presents to 4 houses in a square, including twice to the house at his starting/ending location.

^v^v^v^v^v delivers a bunch of presents to some very lucky children at only 2 houses.

Thoughts

If you treat the directions as 2d grid coordinates and then put the seen coordinates into a set, you are pretty much done. The only part I was confused about was whether or not I would make the borrow checker angry by passing my varaibles (that I still plan on using) to the HashSet. Upon reviewing the Rust Book, I performed a "Stack-Only Data Copy", which means that, for integers that exist entirely on the stack, a copy is made when you assign them to another variable leaving both variable valid.

use std::collections::HashSet;
use std::io;

fn main() {
    let mut up_down = 0;
    let mut left_right = 0;
    let mut set = HashSet::new();

    set.insert((0, 0));

    for line in io::stdin().lines() {
        for character in line.unwrap().chars() {
            if character.eq(&'^') {
                up_down += 1;
            } else if character.eq(&'v') {
                up_down -= 1;
            } else if character.eq(&'<') {
                left_right -= 1;
            } else if character.eq(&'>') {
                left_right += 1;
            }

            set.insert((up_down, left_right));
        }
    }

    println!("Santa delivered presents to {} homes", set.len());
}

Problem 2

The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him.

Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year.

This year, how many houses receive at least one present?

For example:

^v delivers presents to 3 houses, because Santa goes north, and then Robo-Santa goes south.

^>v< now delivers presents to 3 houses, and Santa and Robo-Santa end up back where they started.

^v^v^v^v^v now delivers presents to 11 houses, with Santa going one direction and Robo-Santa going the other.

Thoughts

The problem seems more or less the same, with the difference being that even instructions go to Santa and the odd instructions go to the Robot.

For a moment there, I was going to try to think of clever ways to split up the program, but the simple solution of maintaining two set of grid coordinates seems fine.

use std::collections::HashSet;
use std::io;

fn update_coordinates(
    direction: &char,
    x: &mut isize,
    y: &mut isize,
    set: &mut HashSet<(isize, isize)>,
) {
    if direction.eq(&'^') {
        *x += 1;
    } else if direction.eq(&'v') {
        *x -= 1;
    } else if direction.eq(&'<') {
        *y -= 1;
    } else if direction.eq(&'>') {
        *y += 1;
    }

    set.insert((*x, *y));
}

fn main() {
    let mut santa_up_down = 0;
    let mut santa_left_right = 0;
    let mut robot_up_down = 0;
    let mut robot_left_right = 0;

    let mut set = HashSet::new();

    set.insert((0, 0));

    for line in io::stdin().lines() {
        for (index, character) in line.unwrap().chars().enumerate() {
            if index % 2 == 0 {
                update_coordinates(
                    &character,
                    &mut santa_up_down,
                    &mut santa_left_right,
                    &mut set,
                );
            } else {
                update_coordinates(
                    &character,
                    &mut robot_up_down,
                    &mut robot_left_right,
                    &mut set,
                );
            }
        }
    }

    println!("Santa delivered presents to {} homes", set.len());
}

During my first pass at it, I put all the code in main. Though the answer was right, the repetition of code within the if blocks was hurting my eyes. So I pulled that code out into its own function. At the end of the day it is the same code, but the function of all reference does give the impression that I know what I am doing. hahaha