day10: problem 2 solution

slightly cheated for ideas on part 2. still a pretty ugly solution.
This commit is contained in:
Keenan Tims 2023-12-10 00:02:29 -08:00
parent 26f990840a
commit ebd1d0ff94
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -18,20 +18,25 @@ fn main() {
// PARSE // PARSE
#[derive(Debug)] #[derive(Debug)]
struct PipeSection { struct MapCell {
dist: u64, dist: u64,
kind: char, kind: char,
inside: bool,
} }
impl From<char> for PipeSection { impl From<char> for MapCell {
fn from(c: char) -> Self { fn from(c: char) -> Self {
PipeSection { dist: 0, kind: c } MapCell {
dist: 0,
kind: c,
inside: false,
}
} }
} }
#[derive(Debug)] #[derive(Debug)]
struct PipeMap { struct PipeMap {
map: Vec<Vec<PipeSection>>, map: Vec<Vec<MapCell>>,
start: (usize, usize), start: (usize, usize),
} }
@ -100,15 +105,87 @@ impl PipeMap {
} }
} }
} }
[adj_positions[0], adj_positions[1]] [adj_positions[0], adj_positions[1]]
} }
fn mark_inside(&mut self) {
let start_adj = self.start_adjacencies();
let start_kind = if start_adj.contains(
&self
.apply_adj(self.start, (0, -1))
.unwrap_or((99999, 99999)),
) && start_adj
.contains(&self.apply_adj(self.start, (0, 1)).unwrap_or((99999, 99999)))
{
'|'
} else if start_adj.contains(
&self
.apply_adj(self.start, (-1, 0))
.unwrap_or((99999, 99999)),
) && start_adj
.contains(&self.apply_adj(self.start, (1, 0)).unwrap_or((99999, 99999)))
{
'-'
} else if start_adj.contains(
&self
.apply_adj(self.start, (0, -1))
.unwrap_or((99999, 99999)),
) && start_adj
.contains(&self.apply_adj(self.start, (1, 0)).unwrap_or((99999, 99999)))
{
'L'
} else if start_adj.contains(
&self
.apply_adj(self.start, (0, -1))
.unwrap_or((99999, 99999)),
) && start_adj.contains(
&self
.apply_adj(self.start, (-1, 0))
.unwrap_or((99999, 99999)),
) {
'J'
} else if start_adj.contains(&self.apply_adj(self.start, (0, 1)).unwrap_or((99999, 99999)))
&& start_adj.contains(
&self
.apply_adj(self.start, (-1, 0))
.unwrap_or((99999, 99999)),
)
{
'7'
} else if start_adj.contains(&self.apply_adj(self.start, (0, 1)).unwrap_or((99999, 99999)))
&& start_adj.contains(&self.apply_adj(self.start, (1, 0)).unwrap_or((99999, 99999)))
{
'F'
} else {
panic!("invalid start");
};
for row in &mut self.map {
let mut inside = false;
for cell in row {
let mut kind = cell.kind;
if cell.dist == 0 && kind != 'S' {
cell.inside = inside;
} else {
if kind == 'S' {
kind = start_kind;
}
if kind == '|' || kind == 'L' || kind == 'J' {
inside = !inside;
}
}
}
}
}
} }
impl<T: BufRead> From<Lines<T>> for PipeMap { impl<T: BufRead> From<Lines<T>> for PipeMap {
fn from(lines: Lines<T>) -> Self { fn from(lines: Lines<T>) -> Self {
let mut map: Vec<Vec<PipeSection>> = Vec::new(); let mut map: Vec<Vec<MapCell>> = Vec::new();
for line in lines { for line in lines {
map.push(line.unwrap().chars().map(PipeSection::from).collect()); map.push(line.unwrap().chars().map(MapCell::from).collect());
} }
let start = map let start = map
@ -163,12 +240,39 @@ impl<T: BufRead> From<Lines<T>> for PipeMap {
fn problem1<T: BufRead>(input: Lines<T>) -> u64 { fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
let map = PipeMap::from(input); let map = PipeMap::from(input);
map.map.iter().map(|row| row.iter().map(|cell| cell.dist).max().unwrap()).max().unwrap() map.map
.iter()
.map(|row| row.iter().map(|cell| cell.dist).max().unwrap())
.max()
.unwrap()
} }
// PROBLEM 2 solution // PROBLEM 2 solution
fn problem2<T: BufRead>(input: Lines<T>) -> u64 { fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
0 let mut map = PipeMap::from(input);
map.mark_inside();
for row in &map.map {
for cell in row {
print!("{}", cell.kind);
}
print!(" ");
for cell in row {
print!(
"{}",
match cell.inside {
true => 'I',
false => 'O',
}
);
}
println!();
}
map.map
.iter()
.map(|row| row.iter().filter(|cell| cell.inside).count())
.sum::<usize>() as u64
} }
#[cfg(test)] #[cfg(test)]
@ -182,6 +286,17 @@ SJ.L7
|F--J |F--J
LJ..."; LJ...";
const EXAMPLE2: &str = &"FF7FSF7F7F7F7F7F---7
L|LJ||||||||||||F--J
FL-7LJLJ||||||LJL-77
F--JF--7||LJLJ7F7FJ-
L---JF-JLJ.||-FJLJJ7
|F|F-JF---7F7-L7L|7|
|FFJF7L7F-JF7|JL---7
7-L-JL7||F7|L7F-7F7|
L.L7LFJ|||||FJL7||LJ
L7JLJL-JLJLJL--JLJ.L";
#[test] #[test]
fn problem1_example() { fn problem1_example() {
let c = Cursor::new(EXAMPLE); let c = Cursor::new(EXAMPLE);
@ -190,7 +305,7 @@ LJ...";
#[test] #[test]
fn problem2_example() { fn problem2_example() {
let c = Cursor::new(EXAMPLE); let c = Cursor::new(EXAMPLE2);
assert_eq!(problem2(c.lines()), 0); assert_eq!(problem2(c.lines()), 10);
} }
} }