1
0
Fork 0

day(18): refactor a bit

Not enough for part 2 though…

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-12-18 18:44:41 +01:00
parent 4ce1f28556
commit 2f324acea8
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -1,8 +1,9 @@
use std::{collections::VecDeque, str::FromStr}; use std::{collections::VecDeque, str::FromStr};
use aoc_2023::*;
use itertools::iproduct; use itertools::iproduct;
use aoc_2023::*;
type Output1 = usize; type Output1 = usize;
type Output2 = Output1; type Output2 = Output1;
@ -70,18 +71,8 @@ fn flood_fill(m: &mut [Vec<char>], start: Vector2D<isize>, original: char, fill:
filled_in filled_in
} }
struct Day18 { fn solve(plan: &[Step]) -> usize {
plan: Vec<Step>, let (min_x, max_x, min_y, max_y) = plan.iter().fold((0, 0, 0, 0), |dimensions, s| {
}
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| {
let (mut lx, mut ux, mut ly, mut uy) = dimensions; let (mut lx, mut ux, mut ly, mut uy) = dimensions;
match s._direction { match s._direction {
@ -95,27 +86,18 @@ impl Solution<Output1, Output2> for Day18 {
(lx, ux, ly, uy) (lx, ux, ly, uy)
}); });
let (width, height) = (min_x + max_x + 1, min_y + max_y + 1); let (width, height) = (min_x + max_x + 1, min_y + max_y + 1);
debug!("Dimensions: {}×{}", width, height); debug!("Dimensions: {}×{}", width, height);
let mut map = vec![vec!['.'; width as usize]; height as usize]; let mut map = vec![vec!['.'; width as usize]; height as usize];
let mut position = Vector2D::new(min_x, min_y); 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 plan.iter() {
for s in &self.plan {
let direction = s.direction(); let direction = s.direction();
for _ in 0..s.count { 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] = '#'; map[position] = '#';
position = position + direction; position = position + direction;
} }
} }
// debug!("x ∈ ⟨{} ; {}⟩\ty ∈ ⟨{} ; {}⟩", min_x, max_x, min_y, max_y);
flood_fill(&mut map, Vector2D::new(0, 0), '.', '+'); flood_fill(&mut map, Vector2D::new(0, 0), '.', '+');
@ -133,10 +115,47 @@ impl Solution<Output1, Output2> for Day18 {
map.iter() map.iter()
.map(|r| r.iter().filter(|&&c| c == '#').count()) .map(|r| r.iter().filter(|&&c| c == '#').count())
.sum() .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 { 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(),
)
} }
} }