advent-of-code/2019/03/src/main.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2019-12-03 23:33:31 +01:00
use std::collections::HashMap;
2019-12-03 22:40:49 +01:00
use std::io::{self, BufRead};
struct Movement {
pub direction: char,
pub distance: u16,
}
impl Movement {
pub fn new(raw: &str) -> Self {
Self {
direction: raw.chars().nth(0).unwrap(),
distance: raw.get(1..).unwrap().parse().unwrap(),
}
}
}
2019-12-03 23:33:31 +01:00
fn make_step(x: &mut i32, y: &mut i32, steps: &mut i32, dir: char) {
*steps += 1;
2019-12-03 22:40:49 +01:00
match dir {
'U' => *y += 1,
'D' => *y -= 1,
'L' => *x += 1,
_ => *x -= 1,
};
}
pub fn main() {
let stdin = io::stdin();
let mut input = stdin.lock().lines().map(|s| {
s.unwrap()
.split(',')
.map(|m| Movement::new(m))
.collect::<Vec<_>>()
});
let (first_wire, second_wire) = (input.next().unwrap(), input.next().unwrap());
2019-12-03 23:33:31 +01:00
let mut first_wire_positions = HashMap::new();
let (mut x, mut y, mut steps) = (0, 0, 0);
2019-12-03 22:40:49 +01:00
for m in first_wire {
for _ in 0..m.distance {
2019-12-03 23:33:31 +01:00
make_step(&mut x, &mut y, &mut steps, m.direction);
first_wire_positions.insert((x, y), steps);
2019-12-03 22:40:49 +01:00
}
}
x = 0;
y = 0;
2019-12-03 23:33:31 +01:00
steps = 0;
2019-12-03 22:40:49 +01:00
let mut intersections = HashMap::new();
for m in second_wire {
for _ in 0..m.distance {
2019-12-03 23:33:31 +01:00
make_step(&mut x, &mut y, &mut steps, m.direction);
first_wire_positions
.get(&(x, y))
.map(|s| intersections.insert((x, y), s + steps));
2019-12-03 22:40:49 +01:00
}
}
2019-12-03 23:33:31 +01:00
println!(
"Part 1: {}",
intersections
.keys()
.map(|(x, y)| x.abs() + y.abs())
.min()
.unwrap()
);
println!("Part 2: {}", intersections.values().min().unwrap());
2019-12-03 22:40:49 +01:00
}