diff options
author | kdx <kikoodx@paranoici.org> | 2023-12-10 18:51:24 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-12-10 18:51:24 +0100 |
commit | e68b2c1e20187dc81d46478a284297f3d0651358 (patch) | |
tree | 9ef567b186e1be173092ac4a7bb4a50defab8666 | |
parent | 39f5b59e3e298d8a2d70b5a18beb38111c0c3652 (diff) | |
download | aoc23-e68b2c1e20187dc81d46478a284297f3d0651358.tar.gz |
10 rust
-rw-r--r-- | 10/rust/Cargo.lock | 7 | ||||
-rw-r--r-- | 10/rust/Cargo.toml | 8 | ||||
-rw-r--r-- | 10/rust/src/main.rs | 69 |
3 files changed, 84 insertions, 0 deletions
diff --git a/10/rust/Cargo.lock b/10/rust/Cargo.lock new file mode 100644 index 0000000..b21cc6a --- /dev/null +++ b/10/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/10/rust/Cargo.toml b/10/rust/Cargo.toml new file mode 100644 index 0000000..1ec6963 --- /dev/null +++ b/10/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/10/rust/src/main.rs b/10/rust/src/main.rs new file mode 100644 index 0000000..7c0c3fe --- /dev/null +++ b/10/rust/src/main.rs @@ -0,0 +1,69 @@ +use std::io::BufRead; + +struct Grid(Vec<String>); + +impl Grid { + fn at(&self, x: isize, y: isize) -> char { + return self.0[y as usize].chars().nth(x as usize).unwrap(); + } +} + +fn main() { + let mut grid: Grid = Grid(vec![]); + + for line in std::io::stdin().lock().lines() { + grid.0.push(line.unwrap()); + } + + let mut start_x = 0; + let mut start_y = 0; + 'outer: for (y, line) in grid.0.iter().enumerate() { + for (x, c) in line.chars().enumerate() { + if c == 'S' { + start_x = x; + start_y = y; + break 'outer; + } + } + } + assert!(start_x != 0); + assert!(start_y != 0); + + println!( + "{}", + traverse(&grid, start_x as isize, start_y as isize) / 2 + ); +} + +fn traverse(grid: &Grid, start_x: isize, start_y: isize) -> usize { + let mut x = start_x; + let mut y = start_y; + let mut dir_x = 0isize; + let mut dir_y = 0isize; + let mut steps = 1; + + if "-LF".contains(grid.at(x - 1, y)) { + dir_x = -1; + } else if "-J7".contains(grid.at(x + 1, y)) { + dir_x = 1; + } else if "|7F".contains(grid.at(x, y - 1)) { + dir_y = -1; + } else if "|JL".contains(grid.at(x, y + 1)) { + dir_y = 1; + } else { + panic!("no valid adjacent pipe found"); + } + + loop { + steps += 1; + x += dir_x; + y += dir_y; + match grid.at(x, y) { + '-' | '|' => (), + '7' | 'L' => (dir_x, dir_y) = (dir_y, dir_x), + 'F' | 'J' => (dir_x, dir_y) = (-dir_y, -dir_x), + 'S' => return steps, + _ => unreachable!(), + } + } +} |