Day 9 - part 1

This commit is contained in:
JonOfUs 2022-12-09 13:16:25 +01:00
parent cdd1f5eb57
commit d63a8d37e5
3 changed files with 2171 additions and 1 deletions

2000
res/09/input.txt Normal file

File diff suppressed because it is too large Load diff

168
src/days/d09.rs Normal file
View file

@ -0,0 +1,168 @@
use std::fs;
const GRID_WIDTH: u32 = 1000;
const GRID_HEIGHT: u32 = 1000;
const START_X: u32 = 500;
const START_Y: u32 = 500;
pub fn solve() {
let path = "res/09/input.txt";
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
let lines: Vec<&str> = contents
.lines()
.filter(|line| *line != "")
.collect();
// task 1 - rope with 2 knots
let mut rope2 = Rope::new(2);
// task 2 - rope with 10 knots
let mut rope10 = Rope::new(10);
lines.iter().for_each(|line| {
// parse command
let cmd: Vec<&str> = line.split_whitespace().collect();
let dir = match cmd[0] {
"U" => Dir::Up,
"D" => Dir::Down,
"L" => Dir::Left,
"R" => Dir::Right,
_ => Dir::Up
};
let amount = cmd[1].parse::<u32>().unwrap();
// move head
for _ in 0..amount {
rope2.mv(dir);
rope10.mv(dir);
}
});
// task 1 res
let res = rope2.tail_visited_num();
println!("Result 1: {}", res);
// task 2 res
let res = rope10.tail_visited_num();
println!("Result 1: {}", res);
}
#[derive(Debug, Copy, Clone)]
enum Dir {
Up,
Down,
Left,
Right
}
struct Rope {
knots_x: Vec<u32>,
knots_y: Vec<u32>,
tail_visited_grid: Vec<Vec<bool>> // [Y][X]
}
impl Rope {
pub fn new(knots: u32) -> Self {
Rope {
knots_x: (0..knots).map(|_| START_X).collect(),
knots_y: (0..knots).map(|_| START_Y).collect(),
tail_visited_grid: (0..GRID_HEIGHT)
.map(|_| (0..GRID_WIDTH)
.map(|_| false).collect()
).collect()
}
}
// move head (first knot)
pub fn mv(&mut self, dir: Dir) {
match dir {
Dir::Up => {
self.knots_y[0] -= 1
},
Dir::Down => {
self.knots_y[0] += 1
},
Dir::Left => {
self.knots_x[0] -= 1
},
Dir::Right => {
self.knots_x[0] += 1
}
}
self.check_tail_pos()
}
// move tail if necessary
fn check_tail_pos(&mut self) {
// check pos of each knot except first
for i in 1..self.knots_x.len() {
// head is too far top/bottom
if self.knots_x[i-1].abs_diff(self.knots_x[i]) > 1 {
// if necessary, move on Y-axis
self.knots_y[i] = self.knots_y[i-1];
if self.knots_x[i-1] > self.knots_x[i] { // head below
self.knots_x[i] = self.knots_x[i-1]-1;
} else { // head above
self.knots_x[i] = self.knots_x[i-1]+1;
}
}
// head is too far left/right
else if self.knots_y[i-1].abs_diff(self.knots_y[i]) > 1 {
// if necessary, move on X-axis
self.knots_x[i] = self.knots_x[i-1];
if self.knots_y[i-1] > self.knots_y[i] { // head right
self.knots_y[i] = self.knots_y[i-1]-1;
} else { // head left
self.knots_y[i] = self.knots_y[i-1]+1;
}
}
}
// set current tail (last knot) pos as visited
self.tail_visited_grid[*self.knots_y.last().unwrap() as usize][*self.knots_x.last().unwrap() as usize] = true;
/*
let min_x = self.knots_x.iter().min().unwrap();
let max_x = self.knots_x.iter().max().unwrap();
let min_y = self.knots_y.iter().min().unwrap();
let max_y = self.knots_y.iter().max().unwrap();
let width = max_x - min_x + 1;
let height = max_y - min_y + 1;
let mut out: Vec<Vec<String>> = (0..height).map(|_| (0..width).map(|_| ".".to_string()).collect()).collect();
for i in (0..self.knots_x.len()).rev() {
let x = self.knots_x[i]-min_x;
let y = self.knots_y[i]-min_y;
out[y as usize][x as usize] = i.to_string();
}
println!("{}", min_y);
out.iter().for_each(|line| {
line.iter().for_each(|o| print!("{}",o));
println!();
});
println!();*/
}
// returns number of cells visited by tail
pub fn tail_visited_num(&self) -> u32 {
self.tail_visited_grid.iter()
.map(|row|
row.iter()
.map(|cell|
if *cell { 1 as u32 }
else { 0 as u32 }
).sum::<u32>()
).sum()
}
}

View file

@ -1,7 +1,7 @@
pub mod days;
fn main() {
_all_days()
days::d09::solve()
}
fn _all_days() {
@ -21,4 +21,6 @@ fn _all_days() {
days::d07::solve();
println!("\nDay 8");
days::d08::solve();
println!("\nDay 9");
days::d09::solve();
}