Initial commit
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
// Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's
|
||||
// oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter
|
||||
// Bunny. To save Christmas, Santa needs you to retrieve all fifty stars by December 25th.
|
||||
//
|
||||
// Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent
|
||||
// calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one
|
||||
// star. Good luck!
|
||||
//
|
||||
// You're airdropped near Easter Bunny Headquarters in a city somewhere. "Near", unfortunately, is
|
||||
// as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves
|
||||
// intercepted start here, and nobody had time to work them out further.
|
||||
//
|
||||
// The Document indicates that you should start at the given coordinates (where you just landed)
|
||||
// and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90
|
||||
// degrees, then walk forward the given number of blocks, ending at a new intersection.
|
||||
//
|
||||
// There's no time to follow such ridiculous instructions on foot, though, so you take a moment
|
||||
// and work out the destination. Given that you can only walk on the street grid of the city,
|
||||
// how far is the shortest path to the destination?
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away.
|
||||
// R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away.
|
||||
// R5, L5, R5, R3 leaves you 12 blocks away.
|
||||
// How many blocks away is Easter Bunny HQ?
|
||||
//
|
||||
|
||||
use std::{env, fs};
|
||||
use crate::CardinalDirection::*;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum CardinalDirection {
|
||||
North,
|
||||
South,
|
||||
East,
|
||||
West
|
||||
}
|
||||
|
||||
fn manhattan_distance(directions: &str) -> i32 {
|
||||
let (mut x_distance, mut y_distance, mut x_move, mut y_move) = (0i32,0i32, 0i32, 0i32);
|
||||
let mut current_direction = North;
|
||||
for next_direction in directions.split(", ") {
|
||||
let (direction, vector) = next_direction.split_at(1);
|
||||
let distance = vector.parse().unwrap();
|
||||
// print!("[{}] At {x_distance}x{y_distance}, FACING {current_direction:?}, TURN {} MOVE {} ::::::", next_direction, direction, distance);
|
||||
|
||||
x_move = 0; y_move = 0;
|
||||
|
||||
match direction {
|
||||
"R" => {
|
||||
match current_direction {
|
||||
North => {
|
||||
current_direction = East;
|
||||
x_move = distance;
|
||||
}
|
||||
South => {
|
||||
current_direction = West;
|
||||
x_move = distance * -1 ;
|
||||
}
|
||||
East => {
|
||||
current_direction = South;
|
||||
y_move = distance * -1;
|
||||
}
|
||||
West => {
|
||||
current_direction = North;
|
||||
y_move = distance;
|
||||
}
|
||||
}
|
||||
}
|
||||
"L" => {
|
||||
match current_direction {
|
||||
North => {
|
||||
current_direction = West;
|
||||
x_move = distance * -1;
|
||||
}
|
||||
South => {
|
||||
current_direction = East;
|
||||
x_move = distance;
|
||||
}
|
||||
East => {
|
||||
current_direction = North;
|
||||
y_move = distance;
|
||||
}
|
||||
West => {
|
||||
current_direction = South;
|
||||
y_move = distance * -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
println!("INVALID DIRECTION");
|
||||
}
|
||||
}
|
||||
x_distance += x_move;
|
||||
y_distance += y_move;
|
||||
|
||||
// println!("facing {:?} at {}x{} (moved {}x{})", current_direction, x_distance, y_distance, x_move, y_move);
|
||||
}
|
||||
x_distance.abs() + y_distance.abs()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
let directions = fs::read_to_string(format!("{}/data/01_data.txt", env::var("CARGO_MANIFEST_DIR").unwrap())).unwrap();
|
||||
let directions = directions.as_str();
|
||||
let parmas: Vec<(&str, i32)> = vec![
|
||||
("R2, L3", 5),
|
||||
("R2, R2, R2", 2),
|
||||
("R5, L5, R5, R3", 12),
|
||||
(directions, 252)];
|
||||
|
||||
for (param, expected) in parmas {
|
||||
println!("Manhattan Distance of {} Expected {}", manhattan_distance(param) , expected);
|
||||
}
|
||||
}
|
||||
|
||||
// 252
|
||||
Reference in New Issue
Block a user