d9p1
This commit is contained in:
parent
82f4503b3e
commit
bfdd227743
7
day9/Cargo.lock
generated
Normal file
7
day9/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day9"
|
||||||
|
version = "0.1.0"
|
8
day9/Cargo.toml
Normal file
8
day9/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "day9"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2000
day9/input.txt
Normal file
2000
day9/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
39
day9/src/main.rs
Normal file
39
day9/src/main.rs
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{self, BufRead};
|
||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let file = File::open(&env::args().nth(1).unwrap()).unwrap();
|
||||||
|
let reader = io::BufReader::new(file);
|
||||||
|
let (mut hx, mut hy, mut tx, mut ty) = (0, 0, 0, 0);
|
||||||
|
let mut visited: HashSet<(i64, i64)> = vec![(0, 0)].into_iter().collect();
|
||||||
|
|
||||||
|
for line in reader.lines().flatten() {
|
||||||
|
let (movx, movy) = match line.chars().nth(0) {
|
||||||
|
Some('L') => (-1, 0),
|
||||||
|
Some('R') => (1, 0),
|
||||||
|
Some('U') => (0, -1),
|
||||||
|
Some('D') => (0, 1),
|
||||||
|
_ => (999, 999),
|
||||||
|
};
|
||||||
|
let count = line[2..].parse::<i64>().unwrap();
|
||||||
|
for _ in 0..count {
|
||||||
|
(hx, hy) = (hx + movx, hy + movy);
|
||||||
|
(tx, ty) = move_tail(hx, hy, tx, ty).unwrap();
|
||||||
|
visited.insert((tx, ty));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("{:?}", visited.len());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn move_tail(hx: i64, hy: i64, tx: i64, ty: i64) -> Option<(i64, i64)> {
|
||||||
|
match (hx - tx, hy - ty) {
|
||||||
|
(-1..=1, -1..=1) => Some((tx, ty)),
|
||||||
|
(-1..=1, 2) => Some((hx, ty + 1)),
|
||||||
|
(-1..=1, -2) => Some((hx, ty - 1)),
|
||||||
|
(2, -1..=1) => Some((tx + 1, hy)),
|
||||||
|
(-2, -1..=1) => Some((tx - 1, hy)),
|
||||||
|
_ => None
|
||||||
|
}
|
||||||
|
}
|
8
day9/test.txt
Normal file
8
day9/test.txt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
R 4
|
||||||
|
U 4
|
||||||
|
L 3
|
||||||
|
D 1
|
||||||
|
R 4
|
||||||
|
D 1
|
||||||
|
L 5
|
||||||
|
R 2
|
Loading…
Reference in New Issue
Block a user