Add solution for Day 01 of 2016.

This commit is contained in:
Bert Peters
2016-12-01 13:18:17 +01:00
parent b37fd44fa7
commit 9f0b9b4ef8
3 changed files with 88 additions and 0 deletions

1
2016/day-01/input.txt Normal file
View File

@@ -0,0 +1 @@
R4, R3, L3, L2, L1, R1, L1, R2, R3, L5, L5, R4, L4, R2, R4, L3, R3, L3, R3, R4, R2, L1, R2, L3, L2, L1, R3, R5, L1, L4, R2, L4, R3, R1, R2, L5, R2, L189, R5, L5, R52, R3, L1, R4, R5, R1, R4, L1, L3, R2, L2, L3, R4, R3, L2, L5, R4, R5, L2, R2, L1, L3, R3, L4, R4, R5, L1, L1, R3, L5, L2, R76, R2, R2, L1, L3, R189, L3, L4, L1, L3, R5, R4, L1, R1, L1, L1, R2, L4, R2, L5, L5, L5, R2, L4, L5, R4, R4, R5, L5, R3, L1, L3, L1, L1, L3, L4, R5, L3, R5, R3, R3, L5, L5, R3, R4, L3, R3, R1, R3, R2, R2, L1, R1, L3, L3, L3, L1, R2, L1, R4, R4, L1, L1, R3, R3, R4, R1, L5, L2, R2, R3, R2, L3, R4, L5, R1, R4, R5, R4, L4, R1, L3, R1, R3, L2, L3, R1, L2, R3, L3, L1, L3, R4, L4, L5, R3, R5, R4, R1, L2, R3, R5, L5, L4, L1, L1

1
2016/day-01/sample.txt Normal file
View File

@@ -0,0 +1 @@
R8, R4, R4, R8

86
2016/day-01/solution.rs Normal file
View File

@@ -0,0 +1,86 @@
use std::collections::HashSet;
use std::error::Error;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::str;
fn dist(pos: (i32, i32)) -> i32 {
let (x, y) = pos;
return x.abs() + y.abs()
}
fn main() {
let args: Vec<String> = env::args().collect();
let path = Path::new(&args[1]);
let display = path.display();
// Open the path in read-only mode, returns `io::Result<File>`
let mut file = match File::open(&path) {
// The `description` method of `io::Error` returns a string that
// describes the error
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Ok(file) => file,
};
let mut content = String::new();
match file.read_to_string(&mut content) {
Err(why) => panic!("couldn't open {}: {}", display,
why.description()),
Ok(_) => {},
};
let mut posx = 0;
let mut posy = 0;
let mut dir = 0;
let mut found = false;
let mut positions = HashSet::new();
positions.insert((posx, posy));
for instruction in content.split(", ") {
let turn = &instruction[..1];
let steps_opt: Option<i32> = instruction[1..].trim().parse().ok();
let steps = match steps_opt {
Some(num) => num,
None => panic!("Could note parse number of steps"),
};
if turn == "R" {
dir += 1;
} else {
dir -= 1;
}
dir = (dir + 4) % 4;
let backwards = if dir & 2 == 2 { -1 } else { 1 };
if dir & 1 == 1 {
// Move in y direction
for y in 1..(steps + 1) {
if positions.contains(&(posx, posy + y * backwards)) && !found {
found = true;
println!("Arrived at the same position, dist {}", dist((posx, posy + y * backwards)));
} else {
positions.insert((posx, posy + y * backwards));
}
}
posy += steps * backwards;
} else {
// Move in x direction
for x in 1..(steps + 1) {
if positions.contains(&(posx + x * backwards, posy)) && !found {
println!("Arrived at the same position, dist {}", dist((posx + x * backwards, posy)));
found = true;
} else {
positions.insert((posx + x * backwards, posy));
}
}
posx += steps * backwards;
}
}
println!("Total distance is {}", dist((posx, posy)))
}