From 5241fbed1b54af2fee9d07fdead72c156cc3d401 Mon Sep 17 00:00:00 2001 From: Matt Harden Date: Wed, 1 Oct 2025 14:29:12 -0700 Subject: [PATCH 1/2] Update the new-day script --- Cargo.lock | 8 -------- new-day-2023.sh | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3d32b16..c50214a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -797,14 +797,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "y2023d11" -version = "0.1.0" -dependencies = [ - "anyhow", - "itertools", -] - [[package]] name = "y2024d01" version = "0.1.0" diff --git a/new-day-2023.sh b/new-day-2023.sh index 16e85bb..a6ae095 100755 --- a/new-day-2023.sh +++ b/new-day-2023.sh @@ -12,6 +12,6 @@ if [[ -a $dir ]]; then exit 1 fi -jj new -m "${message}" || exit +jj new -m "${message}" trunk || exit cp -r template "${dir}" || exit cargo init --name "${name}" "${dir}" From cbd94535ed9b30eef594fcf9fe2cef6ebb6d8ce9 Mon Sep 17 00:00:00 2001 From: Matt Harden Date: Wed, 1 Oct 2025 14:32:05 -0700 Subject: [PATCH 2/2] 2023 day 11. --- 2023/10/src/puzzle.rs | 6 +- 2023/11/.gitignore | 1 + 2023/11/Cargo.toml | 9 +++ 2023/11/data/example1 | 10 +++ 2023/11/data/input | 140 ++++++++++++++++++++++++++++++++++++ 2023/11/src/data.rs | 5 ++ 2023/11/src/distance_sum.rs | 32 +++++++++ 2023/11/src/main.rs | 11 +++ 2023/11/src/part1/mod.rs | 17 +++++ 2023/11/src/part2/mod.rs | 26 +++++++ 2023/11/src/puzzle.rs | 42 +++++++++++ Cargo.lock | 9 +++ 12 files changed, 305 insertions(+), 3 deletions(-) create mode 100644 2023/11/.gitignore create mode 100644 2023/11/Cargo.toml create mode 100644 2023/11/data/example1 create mode 100644 2023/11/data/input create mode 100644 2023/11/src/data.rs create mode 100644 2023/11/src/distance_sum.rs create mode 100644 2023/11/src/main.rs create mode 100644 2023/11/src/part1/mod.rs create mode 100644 2023/11/src/part2/mod.rs create mode 100644 2023/11/src/puzzle.rs diff --git a/2023/10/src/puzzle.rs b/2023/10/src/puzzle.rs index 7a69d9a..23472d5 100644 --- a/2023/10/src/puzzle.rs +++ b/2023/10/src/puzzle.rs @@ -45,12 +45,12 @@ fn find_starting_direction(grid: &Grid, starting_position: Position) -> Option unreachable!(), - NorthSouth => N, + NorthSouth => S, EastWest => E, NorthEast => N, - NorthWest => N, + NorthWest => W, SouthWest => S, - SouthEast => S, + SouthEast => E, Start => unreachable!(), }) } diff --git a/2023/11/.gitignore b/2023/11/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/2023/11/.gitignore @@ -0,0 +1 @@ +/target diff --git a/2023/11/Cargo.toml b/2023/11/Cargo.toml new file mode 100644 index 0000000..74f6d21 --- /dev/null +++ b/2023/11/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "y2023d11" +version = "0.1.0" +edition = "2024" + +[dependencies] +game-grid.workspace = true +position.workspace = true +parse-display.workspace = true diff --git a/2023/11/data/example1 b/2023/11/data/example1 new file mode 100644 index 0000000..a0bda53 --- /dev/null +++ b/2023/11/data/example1 @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... \ No newline at end of file diff --git a/2023/11/data/input b/2023/11/data/input new file mode 100644 index 0000000..d8632fe --- /dev/null +++ b/2023/11/data/input @@ -0,0 +1,140 @@ +...........#..........................#..................................................................#......#......#.................... +...#....................#..........................................#..............#...........#............................................. +.............................................................................................................................#.............. +........#...........#.............#..........#.....#...............................................................#........................ +.............................#.............................................................................................................. +........................................................................................................#..................................# +.....#..........#....................#.................#............................#....................................................... +.....................................................................#...................#.......#....................................#..... +............................................................................................................................................ +........................................#....................................................................#..............#............... +...........#........................................#............#.....................................................#...........#........ +......................#.............#..........................................................#.................#.......................... +.............................................#..........................................................#................................... +.#.............................#............................#...............#.............#...................................#............. +............................................................................................................................................ +...................................................................................#........................................................ +.......#.......................................#...............#..........................................#.........#................#...... +....................#..............#....................................................#........#........................................#. +...................................................#...................#.....................................................#.............. +..........................#....................................................#.............................#.............................. +............................................................................................................................................ +.........#.....#...........................#...............................................#.......................#........................ +..........................................................................................................................#.............#... +.............................................................#..............#.........#...........................................#......... +..................................#....................................................................#.................................... +.................................................................................................................#.......................... +.............#......................................................#.............................#..................................#...... +......................#.....#...........#........#.........#.....................#.........................#...............#................ +................................................................#................................................................#.......... +.....#.......................................#.......................................................................#...................... +.........................................................................#................#..........#...................................... +..................#.....#.....................................................#..............................#.............................. +.....................................#..................................................................................................#... +.............................................................#..........................................#......................#............ +.....................................................#..............#................#............#...................#..................... +............................................................................#........................................................#...... +...#.......#................................................................................................................................ +.................#............................#..............................................................#............#................. +...........................#.............................................................#......#........................................... +...............................................................#........#...........#....................................................... +......#......................................................................................................................#......#......# +........................................................#................................................#.................................. +...............................#................#.............................#.............#............................................... +.........................................#...........................#...................................................................... +.#.........................#........#...........................#..................#..................#..................................... +.....................................................#........................................................#...........#................. +.......#..........#......................................................................#..........................#.............#.......#. +............................................................................................................................................ +............................................................................................................................................ +..................................................................................................#......................................... +..................................#.............................#......#..............#..............................................#...... +............................................................................................................................................ +.............................................#...................................#........................#................................. +.....#......#............#.....................................................................................#..................#......... +...........................................................#.................................................................#.............. +................................#...................................................................................#....................... +................................................#..............................................#........#................................... +..........#..........#.....#........................................................#...........................................#........... +......................................#........................................#..........#..............................................#.. +...................................................................#.....#.............................................#.................... +.....#..........#.........................................#................................................................................. +.............................................#....................................#...........#............................................. +..........................................................................................................#......................#.........# +.#.................#............#........#.................................................................................................. +...........................#..............................................................#..................................#.............. +.............#......................#.................................................................#.................................#... +.......................#............................#......#....................................#........................................... +........#......................................#....................#....................................................................... +...............................#...............................................................................#............................ +................................................................#.................#........#................................................ +.#.....................................................................................................................#.................... +................#..........#.......................#....................................................#...................#........#...... +..............................................................................................#............................................. +..........#............#.............................................#...................................................................... +...............................#.....#....................#................................................................................. +.....................................................................................#.............................................#........ +..........................................................................................#........................#........................ +...#....................................#..........................#........................................................................ +............................#.......................................................................#....................................... +..............................................#............................................................................................. +..................#.....#.......................................#...........#.........................................................#..... +#...........#.......................................................................................................#....................... +..........................................#............#.........................#......................#................................... +.............................................................#............................#................................................. +.........#..........#...............................................#...........................#.......................#..................# +..........................#......................#....................................#......................................#.............. +.....................................#.............................................................................................#........ +.............#..................#........................................................................................................... +.......#..................................#..............................#.................#.....................#......................#... +.....................#......................................#............................................................................... +.............................#.........................#..........................#..............#.......................................... +.......................................#................................................................#...........#....................... +............................................................................................................................................ +............................................................................................................................................ +..................#............#............#.......#.......................#...............................................#............... +.#........#...........................................................#...............................................#..................... +...............................................................................................................#............................ +...................................................................................#.......................................................# +................................................#...........#............................................................................... +...................................................................................................#..............#......................... +.........#............#..............#.................................#...................#...................................#............ +................#..............#...................#.............................#.......................................................... +.............................................#................#........................................#..................#................. +.....#.........................................................................................................#...................#........ +...................................#......................#........#......................................................................#. +........................#.............................................................#........#............................................ +..#......................................#....................................#............................................................. +................#...............#.....................#..........................................................#.......................... +.....................................................................................................#.......................#.............. +...............................................#...........................................................................................# +.........#...........#.....................................#................#........................................#...................... +...#..........#.........................#............................#...............................................................#...... +............................................................................................................#.............#................. +..........................#...................................#............................#...................................#............ +.....................................................................................#...................................................... +..........#..............................................................................................................................#.. +................#..............#.............#.........................#................................#...........................#....... +....#...............................#.......................#....................#.......................................................... +....................................................#...........................................................#............#.............. +.................................................................................................#.......................................... +............................................................................................................................................ +.........#...........#.....................#................................#..........#...........................#........................ +...................................#........................................................................................................ +..................................................................................#................................................#........ +..............#.........................................................#.................................#...................#.........#... +..#...........................................#............................................................................................. +...........................#.........................................................................................#...................... +...............................................................#............................................................................ +.........#..........#....................................................................#.......#......#........................#.......... +...................................#.................#...........................#.............................#............................ +.....#.............................................................#........................................................................ +.............................#.............................................................................................................. +..............#.........................#...........................................................#..........................#.........#.. +................................................................#........................................................................... +..#.........................................#.................................#..................................#.......................... +.......................#.......#.......................................#....................#..............................#................ +....................................................................................#...................#..........................#........ +.....#.....#....................................#...................................................................#....................... +..........................................#............#.................................................................................... +...............#............................................#..................................#............................................ \ No newline at end of file diff --git a/2023/11/src/data.rs b/2023/11/src/data.rs new file mode 100644 index 0000000..5b7bd47 --- /dev/null +++ b/2023/11/src/data.rs @@ -0,0 +1,5 @@ +#[cfg(test)] +pub const EXAMPLE1: &'static str = include_str!("../data/example1"); + +#[allow(unused)] +pub const INPUT: &'static str = include_str!("../data/input"); diff --git a/2023/11/src/distance_sum.rs b/2023/11/src/distance_sum.rs new file mode 100644 index 0000000..5590da7 --- /dev/null +++ b/2023/11/src/distance_sum.rs @@ -0,0 +1,32 @@ +pub fn distance_sum(counts: impl IntoIterator, factor: u64) -> u64 { + let mut pos: u64 = 0; + let mut sum: u64 = 0; + let mut sum_of_pos: u64 = 0; + let mut count: u64 = 0; + for c in counts { + if c == 0 { + pos += factor; + continue; + } + pos += 1; + sum_of_pos += pos * c; + sum += pos * (count * 2 + c - 1) * c; + count += c; + } + sum - sum_of_pos * (count - 1) +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_distance_sum_1() { + assert_eq!(distance_sum([2, 1, 0, 1], 2), 13); + } + + #[test] + fn test_distance_sum_2() { + assert_eq!(distance_sum([1, 0, 1, 2], 2), 13); + } +} diff --git a/2023/11/src/main.rs b/2023/11/src/main.rs new file mode 100644 index 0000000..819d409 --- /dev/null +++ b/2023/11/src/main.rs @@ -0,0 +1,11 @@ +mod data; +mod distance_sum; +mod part1; +mod part2; +mod puzzle; + +fn main() { + use data::INPUT; + println!("Part 1: {}", part1::run(INPUT)); + println!("Part 2: {}", part2::run(INPUT)); +} diff --git a/2023/11/src/part1/mod.rs b/2023/11/src/part1/mod.rs new file mode 100644 index 0000000..333319e --- /dev/null +++ b/2023/11/src/part1/mod.rs @@ -0,0 +1,17 @@ +use crate::puzzle::Puzzle; + +pub fn run(input: &str) -> u64 { + let puzzle: Puzzle = input.parse().expect("parse failed"); + puzzle.distance_sum_2d(2) +} + +#[cfg(test)] +mod test { + use super::*; + use crate::data::EXAMPLE1; + + #[test] + fn test1() { + assert_eq!(run(EXAMPLE1), 374); + } +} diff --git a/2023/11/src/part2/mod.rs b/2023/11/src/part2/mod.rs new file mode 100644 index 0000000..48d2501 --- /dev/null +++ b/2023/11/src/part2/mod.rs @@ -0,0 +1,26 @@ +use crate::puzzle::Puzzle; + +fn run_with_factor(input: &str, factor: u64) -> u64 { + let puzzle: Puzzle = input.parse().expect("parse failed"); + puzzle.distance_sum_2d(factor) +} + +pub fn run(input: &str) -> u64 { + run_with_factor(input, 1000000) +} + +#[cfg(test)] +mod test { + use super::*; + use crate::data::EXAMPLE1; + + #[test] + fn test1() { + assert_eq!(run_with_factor(EXAMPLE1, 10), 1030); + } + + #[test] + fn test2() { + assert_eq!(run_with_factor(EXAMPLE1, 100), 8410); + } +} diff --git a/2023/11/src/puzzle.rs b/2023/11/src/puzzle.rs new file mode 100644 index 0000000..de6e549 --- /dev/null +++ b/2023/11/src/puzzle.rs @@ -0,0 +1,42 @@ +use crate::distance_sum::distance_sum; +use game_grid::{Grid, GridCell, ParseCellError}; +use parse_display::FromStr; +use position::Position; + +#[derive(GridCell, Copy, Clone, Debug, PartialEq, Eq, Default)] +pub enum Cell { + #[cell('#')] + Galaxy, + #[cell('.')] + #[default] + Empty, +} + +#[derive(Debug, FromStr)] +#[display("{galaxies}")] +pub struct Puzzle { + pub galaxies: Grid, +} + +impl Puzzle { + fn galaxy_counts(&self) -> (Vec, Vec) { + let galaxies = &self.galaxies; + let mut rows = vec![0u64; galaxies.height()]; + let mut cols = vec![0u64; galaxies.width()]; + for (pos, cell) in galaxies.iter::() { + if cell != Cell::Galaxy { + continue; + } + let col: usize = pos.x.try_into().unwrap(); + let row: usize = pos.y.try_into().unwrap(); + rows[row] += 1; + cols[col] += 1; + } + (rows, cols) + } + + pub fn distance_sum_2d(&self, factor: u64) -> u64 { + let (rows, cols) = self.galaxy_counts(); + distance_sum(rows, factor) + distance_sum(cols, factor) + } +} diff --git a/Cargo.lock b/Cargo.lock index c50214a..08d09ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -797,6 +797,15 @@ dependencies = [ "thiserror", ] +[[package]] +name = "y2023d11" +version = "0.1.0" +dependencies = [ + "game-grid", + "parse-display", + "position", +] + [[package]] name = "y2024d01" version = "0.1.0"