diff options
author | mhsn <mail@mhsn.net> | 2025-08-07 21:35:55 +0100 |
---|---|---|
committer | mhsn <mail@mhsn.net> | 2025-08-07 21:35:55 +0100 |
commit | 10bb2a67c5bb005b304508eb2026780531132f82 (patch) | |
tree | 4e4f72ea6a1fee203fa6c34ed6cc5b570d507100 | |
parent | 81232bba7f13c0cb2929f8ab44b789020ccb978f (diff) | |
download | aoc-10bb2a67c5bb005b304508eb2026780531132f82.tar.gz aoc-10bb2a67c5bb005b304508eb2026780531132f82.zip |
2024-06 rust p1
-rw-r--r-- | 2024/06/rust/Cargo.lock | 14 | ||||
-rw-r--r-- | 2024/06/rust/Cargo.toml | 7 | ||||
-rw-r--r-- | 2024/06/rust/src/main.rs | 43 | ||||
-rw-r--r-- | lib/rust/src/grid.rs | 16 |
4 files changed, 74 insertions, 6 deletions
diff --git a/2024/06/rust/Cargo.lock b/2024/06/rust/Cargo.lock new file mode 100644 index 0000000..a049cf0 --- /dev/null +++ b/2024/06/rust/Cargo.lock @@ -0,0 +1,14 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aoc" +version = "0.1.0" + +[[package]] +name = "aoc_2024-06" +version = "0.1.0" +dependencies = [ + "aoc", +] diff --git a/2024/06/rust/Cargo.toml b/2024/06/rust/Cargo.toml new file mode 100644 index 0000000..f478719 --- /dev/null +++ b/2024/06/rust/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "aoc_2024-06" +version = "0.1.0" +edition = "2021" + +[dependencies] +aoc = { version = "0.1.0", path = "../../../lib/rust" } diff --git a/2024/06/rust/src/main.rs b/2024/06/rust/src/main.rs new file mode 100644 index 0000000..92ac02d --- /dev/null +++ b/2024/06/rust/src/main.rs @@ -0,0 +1,43 @@ +use std::io; + +use aoc::grid::{Direction, Grid}; +use std::collections::HashSet; + +fn main() -> io::Result<()> { + let grid: Vec<Vec<char>> = io::stdin() + .lines() + .map(|line| line.unwrap().chars().collect()) + .collect(); + let grid = Grid::new(grid); + let start = grid + .enumerate() + .filter(|(_, c)| **c == '^') + .next() + .unwrap() + .0; + + let silver: usize = silver(&grid, start); + let gold: usize = gold(&grid); + println!("silver: {silver}"); + println!("gold: {gold}"); + + return Ok(()); +} + +fn silver(grid: &Grid<char>, start: (usize, usize)) -> usize { + let mut pos = start; + let mut dir = Direction { x: 0, y: -1 }; + let mut seen = HashSet::<(usize, usize)>::new(); + loop { + seen.insert(pos); + match grid.move_pos(pos, dir, 1).and_then(|p| grid.at(p)) { + Some(&'#') => dir = dir.cw(), + Some(_) => pos = grid.move_pos(pos, dir, 1).unwrap(), + None => break seen.len(), + } + } +} + +fn gold(grid: &Grid<char>) -> usize { + 0 +} diff --git a/lib/rust/src/grid.rs b/lib/rust/src/grid.rs index 1b43852..671c7de 100644 --- a/lib/rust/src/grid.rs +++ b/lib/rust/src/grid.rs @@ -9,8 +9,8 @@ type Point = (usize, usize); #[derive(PartialEq, Eq, Clone, Hash, Debug, Copy)] pub struct Direction { - x: i64, - y: i64, + pub x: i64, + pub y: i64, } impl From<(i64, i64)> for Direction { @@ -20,12 +20,12 @@ impl From<(i64, i64)> for Direction { } impl Direction { - fn cw(self) -> Self { - (self.y, -self.x).into() - } - fn acw(self) -> Self { + pub fn cw(self) -> Self { (-self.y, self.x).into() } + pub fn acw(self) -> Self { + (self.y, -self.x).into() + } } pub fn chebyshev_ball(n: i64) -> impl Iterator<Item = Direction> { @@ -58,6 +58,10 @@ impl<T> Grid<T> { (0..self.width).flat_map(|x| (0..self.height).map(move |y| (x, y))) } + pub fn enumerate(&self) -> impl Iterator<Item = (Point, &T)> { + self.points().map(|p| (p, self.at(p).unwrap())) + } + pub fn move_pos(&self, (x, y): Point, d: Direction, n: i64) -> Option<Point> { let x = (i64::try_from(x).ok()? + n * d.x).try_into().ok()?; let y = (i64::try_from(y).ok()? + n * d.y).try_into().ok()?; |