day(18): refactor a bit
Not enough for part 2 though… Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
4ce1f28556
commit
2f324acea8
1 changed files with 73 additions and 54 deletions
|
@ -1,8 +1,9 @@
|
|||
use std::{collections::VecDeque, str::FromStr};
|
||||
|
||||
use aoc_2023::*;
|
||||
use itertools::iproduct;
|
||||
|
||||
use aoc_2023::*;
|
||||
|
||||
type Output1 = usize;
|
||||
type Output2 = Output1;
|
||||
|
||||
|
@ -70,18 +71,8 @@ fn flood_fill(m: &mut [Vec<char>], start: Vector2D<isize>, original: char, fill:
|
|||
filled_in
|
||||
}
|
||||
|
||||
struct Day18 {
|
||||
plan: Vec<Step>,
|
||||
}
|
||||
impl Solution<Output1, Output2> for Day18 {
|
||||
fn new<P: AsRef<Path>>(pathname: P) -> Self {
|
||||
Self {
|
||||
plan: file_to_structs(pathname),
|
||||
}
|
||||
}
|
||||
|
||||
fn part_1(&mut self) -> Output1 {
|
||||
let (min_x, max_x, min_y, max_y) = self.plan.iter().fold((0, 0, 0, 0), |dimensions, s| {
|
||||
fn solve(plan: &[Step]) -> usize {
|
||||
let (min_x, max_x, min_y, max_y) = plan.iter().fold((0, 0, 0, 0), |dimensions, s| {
|
||||
let (mut lx, mut ux, mut ly, mut uy) = dimensions;
|
||||
|
||||
match s._direction {
|
||||
|
@ -95,27 +86,18 @@ impl Solution<Output1, Output2> for Day18 {
|
|||
(lx, ux, ly, uy)
|
||||
});
|
||||
let (width, height) = (min_x + max_x + 1, min_y + max_y + 1);
|
||||
|
||||
debug!("Dimensions: {}×{}", width, height);
|
||||
|
||||
let mut map = vec![vec!['.'; width as usize]; height as usize];
|
||||
let mut position = Vector2D::new(min_x, min_y);
|
||||
// debug!("Initial position: {:?}", position);
|
||||
|
||||
// let (mut min_x, mut max_x, mut min_y, mut max_y) = (isize::MAX, isize::MIN, isize::MAX, isize::MIN);
|
||||
for s in &self.plan {
|
||||
for s in plan.iter() {
|
||||
let direction = s.direction();
|
||||
for _ in 0..s.count {
|
||||
// min_x = std::cmp::min(min_x, position.x());
|
||||
// max_x = std::cmp::max(max_x, position.x());
|
||||
// min_y = std::cmp::min(min_y, position.y());
|
||||
// max_y = std::cmp::max(max_y, position.y());
|
||||
|
||||
map[position] = '#';
|
||||
position = position + direction;
|
||||
}
|
||||
}
|
||||
// debug!("x ∈ ⟨{} ; {}⟩\ty ∈ ⟨{} ; {}⟩", min_x, max_x, min_y, max_y);
|
||||
|
||||
flood_fill(&mut map, Vector2D::new(0, 0), '.', '+');
|
||||
|
||||
|
@ -133,10 +115,47 @@ impl Solution<Output1, Output2> for Day18 {
|
|||
map.iter()
|
||||
.map(|r| r.iter().filter(|&&c| c == '#').count())
|
||||
.sum()
|
||||
}
|
||||
|
||||
struct Day18 {
|
||||
plan: Vec<Step>,
|
||||
}
|
||||
impl Solution<Output1, Output2> for Day18 {
|
||||
fn new<P: AsRef<Path>>(pathname: P) -> Self {
|
||||
Self {
|
||||
plan: file_to_structs(pathname),
|
||||
}
|
||||
}
|
||||
|
||||
fn part_1(&mut self) -> Output1 {
|
||||
solve(&self.plan)
|
||||
}
|
||||
|
||||
fn part_2(&mut self) -> Output2 {
|
||||
todo!()
|
||||
solve(
|
||||
&self
|
||||
.plan
|
||||
.iter()
|
||||
.map(|s| {
|
||||
let new_step = s.color.trim_matches('#');
|
||||
let number = isize::from_str_radix(new_step, 16).unwrap();
|
||||
|
||||
let _direction = match number % 16 {
|
||||
0 => (1, 0),
|
||||
1 => (0, 1),
|
||||
2 => (-1, 0),
|
||||
3 => (0, -1),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
|
||||
Step {
|
||||
_direction,
|
||||
count: number >> 8,
|
||||
color: String::new(),
|
||||
}
|
||||
})
|
||||
.collect_vec(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue