Add day 3 part 1 in Rust

This commit is contained in:
kageru 2019-12-03 22:40:49 +01:00
parent 8a2b531c3f
commit 8b6f5708e5
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
2 changed files with 65 additions and 0 deletions

9
2019/03/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "d3"
version = "0.1.0"
authors = ["kageru <kageru@encode.moe>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

56
2019/03/src/main.rs Normal file
View File

@ -0,0 +1,56 @@
use std::collections::{HashMap, HashSet};
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(),
}
}
}
fn make_step(x: &mut i32, y: &mut i32, dir: char) {
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());
let mut first_wire_positions = HashSet::new();
let (mut x, mut y) = (0, 0);
for m in first_wire {
for _ in 0..m.distance {
make_step(&mut x, &mut y, m.direction);
first_wire_positions.insert((x, y));
}
}
x = 0;
y = 0;
let mut intersections = HashMap::new();
for m in second_wire {
for _ in 0..m.distance {
make_step(&mut x, &mut y, m.direction);
if first_wire_positions.contains(&(x, y)) {
intersections.insert((x, y), 0);
}
}
}
println!("Part 1: {}", intersections.keys().map(|(x, y)| x.abs() + y.abs()).min().unwrap());
}