days 7-10
This commit is contained in:
Generated
+16
@@ -0,0 +1,16 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day09"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"rustc-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
|
||||
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day09"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rustc-hash = "1.1.0"
|
||||
@@ -0,0 +1,73 @@
|
||||
use rustc_hash::FxHashSet;
|
||||
use std::cmp::Ordering;
|
||||
use std::io::{self, BufRead};
|
||||
|
||||
fn main() {
|
||||
let lines = io::stdin().lock().lines().map(|l| l.unwrap());
|
||||
|
||||
let mut head = (0, 0);
|
||||
let mut knots: Vec<(isize, isize)> = vec![(0, 0); 9];
|
||||
let mut visited_p1: FxHashSet<(isize, isize)> = FxHashSet::default();
|
||||
let mut visited_p2: FxHashSet<(isize, isize)> = FxHashSet::default();
|
||||
|
||||
visited_p1.insert((0, 0));
|
||||
visited_p2.insert((0, 0));
|
||||
|
||||
for line in lines {
|
||||
let (dir, len) = line.split_once(' ').unwrap();
|
||||
let position = (dir, len.parse::<isize>().unwrap());
|
||||
let mut current;
|
||||
|
||||
for _ in 0..position.1 {
|
||||
step_head(&mut head, position.0);
|
||||
current = head;
|
||||
|
||||
for i in 0..9 {
|
||||
let knot = knots.get_mut(i).unwrap();
|
||||
follow(i, current, knot, &mut visited_p1, &mut visited_p2);
|
||||
current = *knot;
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("P1: {}, P2: {}", visited_p1.len(), visited_p2.len());
|
||||
}
|
||||
|
||||
fn step_head(head: &mut (isize, isize), dir: &str) {
|
||||
match dir {
|
||||
"L" => head.1 -= 1,
|
||||
"R" => head.1 += 1,
|
||||
"U" => head.0 -= 1,
|
||||
"D" => head.0 += 1,
|
||||
invalid => panic!("invalid direction '{}'", invalid),
|
||||
}
|
||||
}
|
||||
|
||||
fn follow(
|
||||
idx: usize,
|
||||
head: (isize, isize),
|
||||
tail: &mut (isize, isize),
|
||||
visited_p1: &mut FxHashSet<(isize, isize)>,
|
||||
visited_p2: &mut FxHashSet<(isize, isize)>,
|
||||
) {
|
||||
let diffx = (tail.0 - head.0).abs();
|
||||
let diffy = (tail.1 - head.1).abs();
|
||||
if (diffx == 1 || diffx == 0) && (diffy == 1 || diffy == 0) {
|
||||
return;
|
||||
}
|
||||
match tail.0.cmp(&head.0) {
|
||||
Ordering::Greater => tail.0 -= 1,
|
||||
Ordering::Less => tail.0 += 1,
|
||||
_ => (),
|
||||
}
|
||||
match tail.1.cmp(&head.1) {
|
||||
Ordering::Greater => tail.1 -= 1,
|
||||
Ordering::Less => tail.1 += 1,
|
||||
_ => (),
|
||||
}
|
||||
if idx == 0 {
|
||||
visited_p1.insert(*tail);
|
||||
}
|
||||
if idx == 8 {
|
||||
visited_p2.insert(*tail);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user