2016/01
This commit is contained in:
4
2016/01/Cargo.lock
generated
Normal file
4
2016/01/Cargo.lock
generated
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[root]
|
||||||
|
name = "01"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
1
2016/01/input.txt
Normal file
1
2016/01/input.txt
Normal file
@@ -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
|
||||||
@@ -1,3 +1,90 @@
|
|||||||
|
use std::io::{self, Read};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
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::<i32>().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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user