Compare commits
No commits in common. "ba121691ba8812b17101e79221c63de754f05c05" and "2ffa87d872551607a97dba99c06dd2a135c63b2a" have entirely different histories.
ba121691ba
...
2ffa87d872
|
@ -1,7 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "day09"
|
|
||||||
version = "0.1.0"
|
|
|
@ -1,8 +0,0 @@
|
||||||
[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]
|
|
|
@ -1,91 +0,0 @@
|
||||||
use std::collections::HashSet;
|
|
||||||
use std::error;
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::BufRead;
|
|
||||||
use std::io::BufReader;
|
|
||||||
|
|
||||||
|
|
||||||
const DEBUG: bool = false;
|
|
||||||
|
|
||||||
|
|
||||||
struct Pos {
|
|
||||||
x: i32,
|
|
||||||
y: i32
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Pos {
|
|
||||||
// Move the position x into x direction and y into y direction
|
|
||||||
fn move_(&mut self, x: i32, y: i32) {
|
|
||||||
self.x += x;
|
|
||||||
self.y += y;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_into_direction(&mut self, direction: &str) -> Result<(), Box<dyn error::Error>> {
|
|
||||||
match direction {
|
|
||||||
"R" => self.move_(1, 0),
|
|
||||||
"L" => self.move_(-1, 0),
|
|
||||||
"U" => self.move_(0, 1),
|
|
||||||
"D" => self.move_(0, -1),
|
|
||||||
_ => Err(format!("Unknown direction {direction}."))?
|
|
||||||
};
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn move_after(&mut self, other: &Pos) {
|
|
||||||
let x_diff = other.x - self.x;
|
|
||||||
let y_diff = other.y - self.y;
|
|
||||||
|
|
||||||
// no movement if we're close enough
|
|
||||||
if x_diff.abs() <= 1 && y_diff.abs() <= 1 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if x_diff.abs() == y_diff.abs() {
|
|
||||||
self.move_((x_diff.abs() - 1) * x_diff.signum(), (y_diff.abs() - 1) * y_diff.signum());
|
|
||||||
} else if x_diff.abs() > y_diff.abs() {
|
|
||||||
self.move_((x_diff.abs() - 1) * x_diff.signum(), y_diff);
|
|
||||||
} else {
|
|
||||||
self.move_(x_diff, (y_diff.abs() - 1) * y_diff.signum());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn show(&self) {
|
|
||||||
println!("x: {} y: {}", self.x, self.y)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
|
||||||
let f = File::open("input.txt")?;
|
|
||||||
let reader = BufReader::new(f);
|
|
||||||
|
|
||||||
let mut knot_positions = vec![];
|
|
||||||
for _ in 0..10 {
|
|
||||||
knot_positions.push(Pos { x: 0, y: 0 });
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut visited_locations = HashSet::new();
|
|
||||||
for line in reader.lines() {
|
|
||||||
let line = line?;
|
|
||||||
let mut s_iter = line.split_whitespace();
|
|
||||||
let direction = s_iter.next().ok_or(format!("Could not split {line} on whitespace"))?;
|
|
||||||
let amount: i32 = s_iter.next().ok_or(format!("Could not split {line} into 2 parts."))?.parse()?;
|
|
||||||
for _ in 0..amount {
|
|
||||||
knot_positions[0].move_into_direction(direction)?;
|
|
||||||
for i in 1..knot_positions.len() {
|
|
||||||
let (head, tail) = knot_positions.split_at_mut(i);
|
|
||||||
tail[0].move_after(head.last().ok_or("head doesn't have an element")?);
|
|
||||||
}
|
|
||||||
if DEBUG {
|
|
||||||
knot_positions[0].show();
|
|
||||||
knot_positions[9].show();
|
|
||||||
}
|
|
||||||
visited_locations.insert((knot_positions[9].x.to_owned(), knot_positions[9].y.to_owned()));
|
|
||||||
}
|
|
||||||
if DEBUG {
|
|
||||||
println!("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("Visited {} locations", visited_locations.len());
|
|
||||||
Ok(())
|
|
||||||
}
|
|
Loading…
Reference in New Issue