Skip to content

Commit e5ef45f

Browse files
committed
2023 day 11.
1 parent 3fd8cbe commit e5ef45f

File tree

12 files changed

+305
-3
lines changed

12 files changed

+305
-3
lines changed

2023/10/src/puzzle.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ fn find_starting_direction(grid: &Grid, starting_position: Position) -> Option<D
4545
use Pipe::*;
4646
Some(match patch_pipe(grid, starting_position)? {
4747
Ground => unreachable!(),
48-
NorthSouth => N,
48+
NorthSouth => S,
4949
EastWest => E,
5050
NorthEast => N,
51-
NorthWest => N,
51+
NorthWest => W,
5252
SouthWest => S,
53-
SouthEast => S,
53+
SouthEast => E,
5454
Start => unreachable!(),
5555
})
5656
}

2023/11/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

2023/11/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "y2023d11"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
game-grid.workspace = true
8+
position.workspace = true
9+
parse-display.workspace = true

2023/11/data/example1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
...#......
2+
.......#..
3+
#.........
4+
..........
5+
......#...
6+
.#........
7+
.........#
8+
..........
9+
.......#..
10+
#...#.....

2023/11/data/input

Lines changed: 140 additions & 0 deletions
Large diffs are not rendered by default.

2023/11/src/data.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[cfg(test)]
2+
pub const EXAMPLE1: &'static str = include_str!("../data/example1");
3+
4+
#[allow(unused)]
5+
pub const INPUT: &'static str = include_str!("../data/input");

2023/11/src/distance_sum.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
pub fn distance_sum(counts: impl IntoIterator<Item = u64>, factor: u64) -> u64 {
2+
let mut pos: u64 = 0;
3+
let mut sum: u64 = 0;
4+
let mut sum_of_pos: u64 = 0;
5+
let mut count: u64 = 0;
6+
for c in counts {
7+
if c == 0 {
8+
pos += factor;
9+
continue;
10+
}
11+
pos += 1;
12+
sum_of_pos += pos * c;
13+
sum += pos * (count * 2 + c - 1) * c;
14+
count += c;
15+
}
16+
sum - sum_of_pos * (count - 1)
17+
}
18+
19+
#[cfg(test)]
20+
mod test {
21+
use super::*;
22+
23+
#[test]
24+
fn test_distance_sum_1() {
25+
assert_eq!(distance_sum([2, 1, 0, 1], 2), 13);
26+
}
27+
28+
#[test]
29+
fn test_distance_sum_2() {
30+
assert_eq!(distance_sum([1, 0, 1, 2], 2), 13);
31+
}
32+
}

2023/11/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
mod data;
2+
mod distance_sum;
3+
mod part1;
4+
mod part2;
5+
mod puzzle;
6+
7+
fn main() {
8+
use data::INPUT;
9+
println!("Part 1: {}", part1::run(INPUT));
10+
println!("Part 2: {}", part2::run(INPUT));
11+
}

2023/11/src/part1/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use crate::puzzle::Puzzle;
2+
3+
pub fn run(input: &str) -> u64 {
4+
let puzzle: Puzzle = input.parse().expect("parse failed");
5+
puzzle.distance_sum_2d(2)
6+
}
7+
8+
#[cfg(test)]
9+
mod test {
10+
use super::*;
11+
use crate::data::EXAMPLE1;
12+
13+
#[test]
14+
fn test1() {
15+
assert_eq!(run(EXAMPLE1), 374);
16+
}
17+
}

2023/11/src/part2/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::puzzle::Puzzle;
2+
3+
fn run_with_factor(input: &str, factor: u64) -> u64 {
4+
let puzzle: Puzzle = input.parse().expect("parse failed");
5+
puzzle.distance_sum_2d(factor)
6+
}
7+
8+
pub fn run(input: &str) -> u64 {
9+
run_with_factor(input, 1000000)
10+
}
11+
12+
#[cfg(test)]
13+
mod test {
14+
use super::*;
15+
use crate::data::EXAMPLE1;
16+
17+
#[test]
18+
fn test1() {
19+
assert_eq!(run_with_factor(EXAMPLE1, 10), 1030);
20+
}
21+
22+
#[test]
23+
fn test2() {
24+
assert_eq!(run_with_factor(EXAMPLE1, 100), 8410);
25+
}
26+
}

0 commit comments

Comments
 (0)