From 1091076164a1ff7f5ab91b215fa1854a103d677c Mon Sep 17 00:00:00 2001 From: logaritmisk Date: Thu, 1 Dec 2016 09:56:11 +0100 Subject: [PATCH] 2016/01 --- 2016/01/Cargo.lock | 4 ++ 2016/01/input.txt | 1 + 2016/01/src/main.rs | 89 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 2016/01/Cargo.lock create mode 100644 2016/01/input.txt diff --git a/2016/01/Cargo.lock b/2016/01/Cargo.lock new file mode 100644 index 0000000..00e1fed --- /dev/null +++ b/2016/01/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "01" +version = "0.1.0" + diff --git a/2016/01/input.txt b/2016/01/input.txt new file mode 100644 index 0000000..4a9ac21 --- /dev/null +++ b/2016/01/input.txt @@ -0,0 +1 @@ +R4, R1, L2, R1, L1, L1, R1, L5, R1, R5, L2, R3, L3, L4, R4, R4, R3, L5, L1, R5, R3, L4, R1, R5, L1, R3, L2, R3, R1, L4, L1, R1, L1, L5, R1, L2, R2, L3, L5, R1, R5, L1, R188, L3, R2, R52, R5, L3, R79, L1, R5, R186, R2, R1, L3, L5, L2, R2, R4, R5, R5, L5, L4, R5, R3, L4, R4, L4, L4, R5, L4, L3, L1, L4, R1, R2, L5, R3, L4, R3, L3, L5, R1, R1, L3, R2, R1, R2, R2, L4, R5, R1, R3, R2, L2, L2, L1, R2, L1, L3, R5, R1, R4, R5, R2, R2, R4, R4, R1, L3, R4, L2, R2, R1, R3, L5, R5, R2, R5, L1, R2, R4, L1, R5, L3, L3, R1, L4, R2, L2, R1, L1, R4, R3, L2, L3, R3, L2, R1, L4, R5, L1, R5, L2, L1, L5, L2, L5, L2, L4, L2, R3 diff --git a/2016/01/src/main.rs b/2016/01/src/main.rs index e7a11a9..28847b3 100644 --- a/2016/01/src/main.rs +++ b/2016/01/src/main.rs @@ -1,3 +1,90 @@ +use std::io::{self, Read}; +use std::collections::HashSet; + + fn main() { - println!("Hello, world!"); + let mut buffer = String::new(); + + io::stdin().read_to_string(&mut buffer).unwrap(); + + let (end, real) = follow_instructions(&buffer); + + if let Some((x, y)) = end { + println!("end position: x={}, y={}", x, y); + } + if let Some((x, y)) = real { + println!("real position: x={}, y={}", x, y); + } +} + +fn follow_instructions(input: &str) -> (Option<(i32, i32)>, Option<(i32, i32)>) { + let instructions = input + .split(",") + .map(|x| x.trim()) + .map(|x| x.split_at(1)) + .map(|(x, y)| (x, y.parse::().unwrap())); + + let mut x = 0; + let mut y = 0; + + let mut delta = (0, 1); + + let mut visited = HashSet::new(); + let mut position: Option<(i32, i32)> = None; + + for (direction, steps) in instructions { + delta = match direction { + "R" => (delta.1, delta.0 * -1), + "L" => (delta.1 * -1, delta.0), + _ => delta, + }; + + for _ in 0..steps { + x += delta.0; + y += delta.1; + + if position.is_none() { + if visited.contains(&(x, y)) { + position = Some((x, y)); + } else { + visited.insert((x, y)); + } + } + } + } + + (Some((x, y)), position) +} + +#[cfg(test)] +mod tests { + use super::follow_instructions; + + #[test] + fn example_01() { + let input = "R2, L3"; + + assert_eq!((Some((2, 3)), None), follow_instructions(input)); + } + + #[test] + fn example_02() { + let input = "R2, R2, R2"; + + assert_eq!((Some((0, -2)), None), follow_instructions(input)); + } + + #[test] + fn example_03() { + let input = "R5, L5, R5, R3"; + + assert_eq!((Some((10, 2)), None), follow_instructions(input)); + } + + #[test] + fn example_04() { + let input = "R8, R4, R4, R8"; + + assert_eq!((Some((4, 4)), Some((4, 0))), follow_instructions(input)); + } }