Merge remote-tracking branch 'origin/main'

This commit is contained in:
max.nuding 2022-12-09 07:22:40 +01:00
commit 982dac721f
Failed to extract signature
5 changed files with 2274 additions and 1017 deletions

File diff suppressed because it is too large Load Diff

2000
input/09.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,26 +1,83 @@
use crate::read; use crate::read;
use indextree::Arena;
use itertools::Itertools;
#[derive(Debug)]
struct Directory {
pub size: usize,
pub name: String
}
pub fn run() { pub fn run() {
for line in read("08").lines() { let grid: Vec<Vec<u32>> = read("08")
.lines()
.map(|l| {
l.chars()
.map(|c| c.to_digit(10).unwrap())
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
} let width = grid.first().unwrap().len();
let res = grid
.iter()
.enumerate()
.skip(1)
.flat_map(|(row, row_vec)| {
row_vec
.iter()
.enumerate()
.skip(1)
.map(|(col, height)| {
let values = [
if let Some(sc_score) = (0..col)
.rev()
.map(|col_left| (col - col_left, row_vec[col_left]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, col)
},
if let Some(sc_score) = (col + 1..width)
.map(|col_right| (col_right - col, row_vec[col_right]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, width - col - 1)
},
if let Some(sc_score) = (0..row)
.rev()
.map(|row_above| (row - row_above, grid[row_above][col]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, row)
},
if let Some(sc_score) = grid
.iter()
.enumerate()
.skip(row + 1)
.map(|(row_below, l)| (row_below - row, l[col]))
.find(|(_sc_score, other_height)| other_height >= height)
{
(1, sc_score.0)
} else {
(0, grid.len() - row - 1)
},
];
let is_visible = values.map(|e| e.0).iter().sum::<i32>() < 4;
let scenic = values.map(|e| e.1).iter().product::<usize>();
(is_visible, scenic)
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
#[cfg(feature = "part1")] #[cfg(feature = "part1")]
{ {
println!("Day 8, Part 01: {}", "TODO"); let count = res.iter().filter(|e| e.0).count();
println!("Day 8, Part 01: {}", count);
} }
#[cfg(feature = "part2")] #[cfg(feature = "part2")]
{ {
println!("Day 8, Part 02: {}", "TODO"); let max_score = res.iter().map(|e| e.1).max().unwrap();
println!("Day 8, Part 02: {}", max_score);
} }
} }

101
src/day09/mod.rs Normal file
View File

@ -0,0 +1,101 @@
use crate::read;
use itertools::Itertools;
use std::collections::HashSet;
#[derive(Debug, PartialEq, Eq, Default, Clone, Copy, Hash)]
struct Position {
pub x: i32,
pub y: i32,
}
impl Position {
pub fn to_right(&self, count: i32) -> Self {
Self {
x: self.x + count,
y: self.y,
}
}
pub fn to_left(&self, count: i32) -> Self {
Self {
x: self.x - count,
y: self.y,
}
}
pub fn to_up(&self, count: i32) -> Self {
Self {
x: self.x,
y: self.y - count,
}
}
pub fn to_down(&self, count: i32) -> Self {
Self {
x: self.x,
y: self.y + count,
}
}
pub fn follow_head(&self, head: &Position) -> Self {
if head.x == self.x && head.y > self.y + 1 {
self.to_down(1)
} else if head.x == self.x && head.y < self.y - 1 {
self.to_up(1)
} else if head.x > self.x + 1 && head.y == self.y {
self.to_right(1)
} else if head.x < self.x - 1 && head.y == self.y {
self.to_left(1)
} else if (head.x - self.x).abs() == 1 && (head.y - self.y).abs() == 1 {
*self
} else if head.x < self.x && head.y < self.y {
self.to_left(1).to_up(1)
} else if head.x > self.x && head.y < self.y {
self.to_right(1).to_up(1)
} else if head.x > self.x && head.y > self.y {
self.to_right(1).to_down(1)
} else if head.x < self.x && head.y > self.y {
self.to_left(1).to_down(1)
} else {
*self
}
}
}
pub fn run() {
#[cfg(feature = "part1")]
{
println!("Day 9, Part 01: {}", simulate(2));
}
#[cfg(feature = "part2")]
{
println!("Day 9, Part 02: {}", simulate(10));
}
}
pub fn simulate(num_knots: usize) -> usize {
let mut visited: HashSet<Position> = HashSet::new();
let mut knots = vec![Position::default(); num_knots];
let input = read("09");
let lines = input.lines();
for line in lines {
if line.is_empty() {
continue;
}
let (dir, count) = line.split_once(' ').unwrap();
let count = count.parse().unwrap();
for _c in 0..count {
knots[0] = match dir {
"R" => knots[0].to_right(1),
"U" => knots[0].to_up(1),
"L" => knots[0].to_left(1),
"D" => knots[0].to_down(1),
_ => unreachable!(),
};
for i in 1..knots.len() {
knots[i] = knots[i].follow_head(&knots[i - 1]);
}
visited.insert(*knots.last().unwrap());
}
}
visited.len()
}

View File

@ -9,6 +9,7 @@ mod day05;
mod day06; mod day06;
mod day07; mod day07;
mod day08; mod day08;
mod day09;
fn main() { fn main() {
let today = Local::now().day(); let today = Local::now().day();
@ -37,6 +38,9 @@ fn main() {
if cfg!(feature = "day08") || (cfg!(feature = "today") && today == 8) { if cfg!(feature = "day08") || (cfg!(feature = "today") && today == 8) {
day08::run(); day08::run();
} }
if cfg!(feature = "day09") || (cfg!(feature = "today") && today == 9) {
day09::run();
}
println!("Finished, time taken: {:?}", now.elapsed()) println!("Finished, time taken: {:?}", now.elapsed())
} }