day10: problem 2 solution
slightly cheated for ideas on part 2. still a pretty ugly solution.
This commit is contained in:
parent
26f990840a
commit
ebd1d0ff94
137
10/src/main.rs
137
10/src/main.rs
@ -18,20 +18,25 @@ fn main() {
|
||||
// PARSE
|
||||
|
||||
#[derive(Debug)]
|
||||
struct PipeSection {
|
||||
struct MapCell {
|
||||
dist: u64,
|
||||
kind: char,
|
||||
inside: bool,
|
||||
}
|
||||
|
||||
impl From<char> for PipeSection {
|
||||
impl From<char> for MapCell {
|
||||
fn from(c: char) -> Self {
|
||||
PipeSection { dist: 0, kind: c }
|
||||
MapCell {
|
||||
dist: 0,
|
||||
kind: c,
|
||||
inside: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct PipeMap {
|
||||
map: Vec<Vec<PipeSection>>,
|
||||
map: Vec<Vec<MapCell>>,
|
||||
start: (usize, usize),
|
||||
}
|
||||
|
||||
@ -100,15 +105,87 @@ impl PipeMap {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[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 {
|
||||
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 {
|
||||
map.push(line.unwrap().chars().map(PipeSection::from).collect());
|
||||
map.push(line.unwrap().chars().map(MapCell::from).collect());
|
||||
}
|
||||
|
||||
let start = map
|
||||
@ -123,7 +200,7 @@ impl<T: BufRead> From<Lines<T>> for PipeMap {
|
||||
|
||||
let mut cur_positions = pipemap.start_adjacencies();
|
||||
let mut cur_distance = 1;
|
||||
loop {
|
||||
loop {
|
||||
for pos in cur_positions {
|
||||
pipemap.map[pos.1][pos.0].dist = cur_distance;
|
||||
}
|
||||
@ -163,12 +240,39 @@ impl<T: BufRead> From<Lines<T>> for PipeMap {
|
||||
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
|
||||
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
|
||||
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)]
|
||||
@ -182,6 +286,17 @@ SJ.L7
|
||||
|F--J
|
||||
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]
|
||||
fn problem1_example() {
|
||||
let c = Cursor::new(EXAMPLE);
|
||||
@ -190,7 +305,7 @@ LJ...";
|
||||
|
||||
#[test]
|
||||
fn problem2_example() {
|
||||
let c = Cursor::new(EXAMPLE);
|
||||
assert_eq!(problem2(c.lines()), 0);
|
||||
let c = Cursor::new(EXAMPLE2);
|
||||
assert_eq!(problem2(c.lines()), 10);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user