Compare commits

...

2 Commits

Author SHA1 Message Date
MasterofJOKers ba121691ba day09: Chage stuff for part 2
Part 1 was missing the "y_diff.abs() == x_diff.abs()" condition in
Pos.move_after(), which made the program show the wrong output at first.
2022-12-24 15:04:11 +01:00
MasterofJOKers ae6ac6979a day09: Add part 1 2022-12-24 14:29:57 +01:00
3 changed files with 106 additions and 0 deletions

7
day09/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day09"
version = "0.1.0"

8
day09/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[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]

91
day09/src/main.rs Normal file
View File

@ -0,0 +1,91 @@
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(())
}