Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 2023/10/src/puzzle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ fn find_starting_direction(grid: &Grid, starting_position: Position) -> Option<D
use Pipe::*;
Some(match patch_pipe(grid, starting_position)? {
Ground => unreachable!(),
NorthSouth => N,
NorthSouth => S,
EastWest => E,
NorthEast => N,
NorthWest => N,
NorthWest => W,
SouthWest => S,
SouthEast => S,
SouthEast => E,
Start => unreachable!(),
})
}
Expand Down
1 change: 1 addition & 0 deletions 2023/11/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
9 changes: 9 additions & 0 deletions 2023/11/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions 2023/11/data/example1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
140 changes: 140 additions & 0 deletions 2023/11/data/input

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions 2023/11/src/data.rs
Original file line number Diff line number Diff line change
@@ -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");
32 changes: 32 additions & 0 deletions 2023/11/src/distance_sum.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pub fn distance_sum(counts: impl IntoIterator<Item = u64>, 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);
}
}
11 changes: 11 additions & 0 deletions 2023/11/src/main.rs
Original file line number Diff line number Diff line change
@@ -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));
}
17 changes: 17 additions & 0 deletions 2023/11/src/part1/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
26 changes: 26 additions & 0 deletions 2023/11/src/part2/mod.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
42 changes: 42 additions & 0 deletions 2023/11/src/puzzle.rs
Original file line number Diff line number Diff line change
@@ -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<Cell>,
}

impl Puzzle {
fn galaxy_counts(&self) -> (Vec<u64>, Vec<u64>) {
let galaxies = &self.galaxies;
let mut rows = vec![0u64; galaxies.height()];
let mut cols = vec![0u64; galaxies.width()];
for (pos, cell) in galaxies.iter::<Position>() {
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)
}
}
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion new-day-2023.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Loading