summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2023-12-14 20:38:18 +0100
committernekineki <nekineki@nekineki.net>2023-12-14 20:56:29 +0100
commit5acdb080e27c611d0f80f86977c0169d55f55474 (patch)
tree548b02886e982970094a179b502ae17d548e9136 /2023
parentb4a7a51404bc9242b8a54a4ce4fa72689ce4e075 (diff)
day10 part2
Diffstat (limited to '2023')
-rw-r--r--2023/day10.rs117
-rw-r--r--2023/in/day10.ref15
2 files changed, 108 insertions, 24 deletions
diff --git a/2023/day10.rs b/2023/day10.rs
index 919223c..4ad319e 100644
--- a/2023/day10.rs
+++ b/2023/day10.rs
@@ -1,6 +1,4 @@
#![allow(dead_code)]
-#![allow(unused_variables)]
-#![allow(unused_mut)]
use num::complex::Complex as c;
use num::Complex;
use std::fs::File;
@@ -18,6 +16,13 @@ enum Dir {
Ground,
}
+#[derive(Debug, PartialEq, Clone, Copy)]
+enum Flood {
+ Pipe,
+ Empty,
+ Full,
+}
+
const LUT: [(Dir, Complex<i32>, Complex<i32>, char, char); 8] = [
(Dir::UD, c::new(0, 1), c::new(0, -1), '|', '│'),
(Dir::LR, c::new(1, 0), c::new(-1, 0), '-', '─'),
@@ -48,9 +53,24 @@ fn print_m(m: &Vec<Vec<Dir>>) {
println!();
}
+fn print_m2(m: &Vec<Vec<Flood>>) {
+ for j in 0..m.len() {
+ for i in 0..m[0].len() {
+ let a = match m[j][i] {
+ Flood::Pipe => '#',
+ Flood::Empty => ' ',
+ Flood::Full => '.',
+ };
+ print!("{}", a);
+ }
+ println!();
+ }
+ println!();
+}
+
fn find_start(m: &Vec<Vec<Dir>>) -> Complex<i32> {
- for x in 0..m.len() {
- for y in 0..m[0].len() {
+ for y in 0..m.len() {
+ for x in 0..m[0].len() {
if m[y][x] == Dir::Start {
return (x as i32) + (y as i32) * c::i();
}
@@ -80,6 +100,24 @@ fn next(
(pos, dir)
}
+fn flood(mut m: Vec<Vec<Flood>>) -> (Vec<Vec<Flood>>, usize) {
+ let mut changes = 0;
+ for y in 1..m.len() - 1 {
+ for x in 1..m[0].len() - 1 {
+ if m[y][x] == Flood::Full {
+ for (yy, xx) in [(y - 1, x), (y + 1, x), (y, x + 1), (y, x - 1)] {
+ if m[yy][xx] == Flood::Empty {
+ changes += 1;
+ m[yy][xx] = Flood::Full;
+ }
+ }
+ }
+ }
+ }
+
+ (m, changes)
+}
+
fn main() {
// let filename = "in/day10.ref";
let filename = "in/day10.pzl";
@@ -89,38 +127,79 @@ fn main() {
f.read_to_string(&mut content).expect("cannot read file");
let lines = content.trim_end().split('\n');
- let mut m: Vec<Vec<Dir>> = Vec::new();
+ let mut m1: Vec<Vec<Dir>> = Vec::new();
for line in lines {
let mut r = Vec::new();
for c in line.chars() {
- for m in LUT {
- if m.3 == c {
- r.push(m.0);
+ for m1 in LUT {
+ if m1.3 == c {
+ r.push(m1.0);
}
}
}
- m.push(r);
+ m1.push(r);
}
+ // print_m(&m1);
- let mut res1 = 0;
- let mut res2 = 0;
-
- print_m(&m);
- let start = find_start(&m);
+ let start = find_start(&m1);
let mut pos = start;
let mut dir = c::new(1, 0);
- (pos, dir) = next(pos, dir, &m);
- res1 += 1;
+ let mut path: Vec<(usize, usize)> = Vec::new();
- while pos != start {
- (pos, dir) = next(pos, dir, &m);
+ let mut res1 = 0;
+ loop {
+ path.push((pos.re as usize, pos.im as usize));
+ (pos, dir) = next(pos, dir, &m1);
res1 += 1;
+ if pos == start {
+ break;
+ }
}
+ path.push((pos.re as usize, pos.im as usize));
res1 /= 2;
- println!("{} {}", pos, dir);
+
+ let mut m2: Vec<Vec<Flood>> = vec![vec![Flood::Empty; 2 * m1[0].len() + 4]; 2 * m1.len() + 4];
+
+ for ((x0, y0), (x1, y1)) in path.iter().zip(path.iter().skip(1)) {
+ m2[*y0 * 2 + 2][*x0 * 2 + 2] = Flood::Pipe;
+ m2[*y0 + *y1 + 2][*x0 + *x1 + 2] = Flood::Pipe;
+ }
+
+ for y in 0..m2.len() {
+ let l = m2[0].len() - 1;
+ m2[y][0] = Flood::Full;
+ m2[y][1] = Flood::Full;
+ m2[y][l - 1] = Flood::Full;
+ m2[y][l] = Flood::Full;
+ }
+ for x in 0..m2[0].len() {
+ let l = m2.len() - 1;
+ m2[0][x] = Flood::Full;
+ m2[1][x] = Flood::Full;
+ m2[l - 1][x] = Flood::Full;
+ m2[l][x] = Flood::Full;
+ }
+
+ // print_m2(&m2);
+ let mut changes = 1;
+ while changes != 0 {
+ (m2, changes) = flood(m2);
+ }
+ // print_m2(&m2);
+
+ let mut res2 = 0;
+ for y in (2..m2.len()).step_by(2) {
+ for x in (2..m2[0].len()).step_by(2) {
+ if m2[y][x] == Flood::Empty {
+ res2 += 1;
+ }
+ }
+ }
println!("res1: {}", res1);
println!("res2: {}", res2);
+ assert_eq!(res1, 6778);
+ assert_eq!(res2, 433);
}
diff --git a/2023/in/day10.ref b/2023/in/day10.ref
index 3aea4dd..adaae96 100644
--- a/2023/in/day10.ref
+++ b/2023/in/day10.ref
@@ -1,5 +1,10 @@
-7-F7-
-.FJ|7
-SJLL7
-|F--J
-LJ.LJ
+.F----7F7F7F7F-7....
+.|F--7||||||||FJ....
+.||.FJ||||||||L7....
+FJL7L7LJLJ||LJ.L-7..
+L--J.L7...LJS7F-7L7.
+....F-J..F7FJ|L7L7L7
+....L7.F7||L7|.L7L7|
+.....|FJLJ|FJ|F7|.LJ
+....FJL-7.||.||||...
+....L---J.LJ.LJLJ...