day 9 part 1
This commit is contained in:
25
day9/Cargo.lock
generated
Normal file
25
day9/Cargo.lock
generated
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "day9"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.10.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
9
day9/Cargo.toml
Normal file
9
day9/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[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]
|
||||||
|
itertools = "0.10.5"
|
||||||
2000
day9/input
Normal file
2000
day9/input
Normal file
File diff suppressed because it is too large
Load Diff
63
day9/src/main.rs
Normal file
63
day9/src/main.rs
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
use itertools::Itertools;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{prelude::*, BufReader};
|
||||||
|
|
||||||
|
fn main() -> std::io::Result<()> {
|
||||||
|
//Read in file
|
||||||
|
let file = File::open("input")?;
|
||||||
|
let reader = BufReader::new(file);
|
||||||
|
|
||||||
|
let mut tail_pos: Vec<(i32, i32)> = vec![(0, 0)];
|
||||||
|
let mut head_pos: Vec<(i32, i32)> = vec![(0, 0)];
|
||||||
|
|
||||||
|
//read in input
|
||||||
|
reader.lines().for_each(|line| {
|
||||||
|
let line = line.unwrap();
|
||||||
|
let (direction, steps) = match line.split(' ').collect::<Vec<&str>>()[..] {
|
||||||
|
[dir, step] => (dir, step.parse::<usize>().unwrap()),
|
||||||
|
_ => panic!("failed parseing line {}", line),
|
||||||
|
};
|
||||||
|
for _ in 0..steps {
|
||||||
|
let (mut cur_head_x, mut cur_head_y) = head_pos.last().unwrap();
|
||||||
|
let (mut cur_tail_x, mut cur_tail_y) = tail_pos.last().unwrap();
|
||||||
|
match direction {
|
||||||
|
"L" => cur_head_x -= 1,
|
||||||
|
"R" => cur_head_x += 1,
|
||||||
|
"U" => cur_head_y += 1,
|
||||||
|
"D" => cur_head_y -= 1,
|
||||||
|
x => panic!("invalid movement {}", x),
|
||||||
|
};
|
||||||
|
let x_offset = cur_head_x - cur_tail_x;
|
||||||
|
let y_offset = cur_head_y - cur_tail_y;
|
||||||
|
if x_offset > 1 {
|
||||||
|
if y_offset != 0 {
|
||||||
|
cur_tail_y = cur_head_y;
|
||||||
|
}
|
||||||
|
cur_tail_x += 1
|
||||||
|
} else if x_offset < -1 {
|
||||||
|
if y_offset != 0 {
|
||||||
|
cur_tail_y = cur_head_y;
|
||||||
|
}
|
||||||
|
cur_tail_x -= 1
|
||||||
|
} else if y_offset > 1 {
|
||||||
|
if x_offset != 0 {
|
||||||
|
cur_tail_x = cur_head_x;
|
||||||
|
}
|
||||||
|
cur_tail_y += 1
|
||||||
|
} else if y_offset < -1 {
|
||||||
|
if x_offset != 0 {
|
||||||
|
cur_tail_x = cur_head_x;
|
||||||
|
}
|
||||||
|
cur_tail_y -= 1
|
||||||
|
}
|
||||||
|
let new_tail = (cur_tail_x, cur_tail_y);
|
||||||
|
if *tail_pos.last().unwrap() != new_tail {
|
||||||
|
tail_pos.push(new_tail);
|
||||||
|
}
|
||||||
|
head_pos.push((cur_head_x, cur_head_y));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let part1 = tail_pos.iter().unique().count();
|
||||||
|
println!("Part 1: {}", part1);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user