This commit is contained in:
Shadowfacts 2023-12-11 11:12:43 -05:00
parent 68d1983e5f
commit 3928623854
5 changed files with 281 additions and 1 deletions

16
Cargo.lock generated
View File

@ -15,9 +15,25 @@ dependencies = [
name = "aoc23"
version = "0.1.0"
dependencies = [
"itertools",
"regex",
]
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"either",
]
[[package]]
name = "memchr"
version = "2.6.4"

View File

@ -7,3 +7,4 @@ edition = "2021"
[dependencies]
regex = "1.10"
itertools = "0.12"

140
input/day11.txt Normal file
View File

@ -0,0 +1,140 @@
.......#.....#.....................#.............#.................#.........................#..............................................
#.....................#.......#.................................................................................#.......#...................
.......................................................................#.........................#..................................#.......
................................................................#..............#..............................................#.............
.........................................#............................................................#....................................#
.................#..................................................#.......................................................................
...........................#...............................................................................#................................
....#.......#...........................................#..........................#.............................#..........................
....................#.............#........#................................................................................................
...................................................#...................................................#...................#................
.........................#........................................#..................................................#......................
......#..........#...........................................#...........#............#.....................................................
......................................#..........................................#.........................#.....................#..........
.#...................#..................................#.......................................#...........................................
............#.................................#.....................#.......................................................................
............................................................................................................................................
..............................#.....................................................................................#..................#....
....................................................................................#........#......#.......................................
.........................#.........................#............#...........................................#...............................
.#......#.....#.......................#.................#........................................................#.....#....................
...........................................................................#.................................................#..............
...............................................#.......................................#...............#..........................#.........
......................#..................#..................................................................................................
..........................................................................................................................#.................
......#...........................#.........................................................#........................#......................
..............................................................................................................................#.............
..#.......................#.................#............................................................................................#..
..................#....................................................#................#...................................................
....................................................#.....#....................#............................................................
...............................#............................................................................................................
..............................................#...............#..........................................................#...........#......
.#.........#........................#..................................................................#....................................
..........................................................................#.......................#........................................#
...................#......................................................................#.................................................
.............................................................................................................................#..............
.....#......................................................................................................................................
........................................................#........................#..........................................................
.............................#...............#.....................#.....#............#..................#..............................#...
............................................................................................#......#...........#............................
.........................#........#...................................................................................#.....................
...........................................................................................................................................#
.....................................................#.....#.................................................................#..............
#.........................................................................................................#.......................#.........
................#...........................................................#.....................#.........................................
..............................#..............#...................................#.......#...........................#......................
........#................#.........#............................#............................................#.............#.........#......
............................................................................................................................................
.......................................................#...............#.............................#...........................#..........
.............#.................................#............................................................................................
............................................................................................................................................
..#.............................................................................#...........................................#...........#...
............................................................................................................................................
......................................#..........................................................#................#.........................
............................#................#......#......................#...........#....................................................
............................................................................................................................................
....................................................................................................#................#..........#...........
.#............#...................#...............................#........................................#.............................#..
.........#...............................................................#..................................................................
.......................#....................#........#...........................................#..........................................
............................................................#.....................................................#.........................
..............................#.......................................................................#...................#.......#.........
............#.......................#...................#............................#......................................................
...................................................#......................................#..............................................#..
.#................#...................................................#........................#............................................
......#....................................................................#................................#...............................
...............................#....................................................................................#.......................
.........................#.....................#......#.............................................#.................................#.....
............................................................................................................................................
...............#.....................#....................................................#.................................................
..................................................................................................................#.........................
....#......#.....................................#..............................#............................#............#.....#...........
....................#.............................................#....................#.........#.......................................#..
.........................................................#..............#.............................#.....................................
............................#...........#...........................................................................#.......................
............................................................................................................................................
#..................................#...............................................................#..........#.............................
.......#......................................................................................#.................................#.......#...
.....................#....................#...............................#.....#......................................#....................
................................#..........................#......#....................#..........................#.........................
..............#...................................#........................................................................#...............#
....#....................................................................................................#..................................
.......................#....................................................................................................................
......................................#................................#..................#.................................................
.....................................................#............................................#...............................#.........
............................................................................................................................................
............................................................................................................................................
....#.................#..................................#.........................#..........................#.............................
.............#........................................................................................#..................#..................
......................................#............................................................................#........................
.........#....................................................#.........#............................................................#......
.............................................................................................#..............................................
.............................#...........#............................................................................#.....................
#..................#................................................#....................#.......................................#.........#
...................................................................................................#......#.................................
...........#.............................................#...........................#......................................................
....................................#.......................................................................................................
..................................................#..........#.........#......................#.....................#.......................
.#......#....................................#..............................................................#...............................
.............................#.............................................#................................................................
.......................#...............................................................................#....................#...............
....................................................#.....................................#...........................................#.....
................#.....................#..................#.................................................................................#
...#.....#..........................................................#................#......................................................
.............................................#...............#................................#......#......................................
...............................#...............................................#............................................................
.............................................................................................................................#.........#....
.....................#................................................#.................................................#...................
...............#.........................#.......................#..................#.......................................................
.....#......................................................................................................#.......................#.......
....................................#.................#.........................#..................#........................................
........................................................................................................................................#...
.........#..............#.....................................#........................................#.....................#..............
...............................................#...................#........................................................................
......................................#.............#..................................#.....#.......................#......................
............#.....#...............................................................................#.........................................
...#...................................................................................................................................#....
.......................#.........................#..........................................................................................
.........................................................#...........#.........................#........#..........#.............#..........
............................#..............#....................#........................................................#..................
.......#.................................................................#................#.................................................
.................#..................................................................................#........................#...........#..
...................................................#.......#................................................................................
.........................................#......................................#......................................#....................
....................................#...........................................................................#...........................
..............................#.............................................................#...........#.......................#......#....
.........#...............................................#..............#...................................................................
........................#....................#.....................................#........................................................
#...........................................................................................................................................
............................#......................................................................#.......#................................
......................................#.....................................................................................................
.....#............#.........................................#..........................#..........................................#.........
...........................................#......................#..............................................#..........................
..............#..............................................................................#............................#.............#...
..#......#...........................................#..............................#..................#.....#..............................
....................#..........#.....#...............................#......................................................................
............................................................................................................................................
.................................................#..........................................................................................
.............#..........#...............................#...............#.............#..........#.........#........#.......................
............................................#................#.................#............................................................
....................................#....................................................................................#.............#....

122
src/day11.rs Normal file
View File

@ -0,0 +1,122 @@
use itertools::Itertools;
use std::fmt::Debug;
pub fn run() {
let input = include_str!("../input/day11.txt");
// let input = r#"...#......
// .......#..
// #.........
// ..........
// ......#...
// .#........
// .........#
// ..........
// .......#..
// #...#....."#;
let image: Image = input.into();
let expansions = image.expand();
dbg!(calculate_distances(&image, &expansions, 2));
dbg!(calculate_distances(&image, &expansions, 1_000_000));
}
fn calculate_distances(image: &Image, expansions: &Expansions, factor: usize) -> usize {
image
.iter_galaxies()
.combinations(2)
.map(|v| expansions.distance(factor, v[0], v[1]))
.sum::<usize>()
}
#[derive(Clone)]
struct Image {
chars: Vec<Vec<char>>,
width: usize,
height: usize,
}
impl From<&str> for Image {
fn from(value: &str) -> Self {
let chars = value
.trim()
.lines()
.map(|l| l.trim().chars().collect::<Vec<_>>())
.collect::<Vec<_>>();
let width = chars[0].len();
let height = chars.len();
Self {
chars,
width,
height,
}
}
}
impl Debug for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = self
.chars
.iter()
.map(|row| row.iter().collect::<String>())
.collect::<Vec<_>>()
.join("\n");
write!(f, "Image(\n{})", s)
}
}
impl Image {
fn at(&self, col: usize, row: usize) -> char {
if col < self.chars[0].len() && row < self.chars.len() {
self.chars[row][col]
} else {
'.'
}
}
fn expand(&self) -> Expansions {
let rows = (0..self.height)
.rev()
.filter(|&row| (0..self.width).all(|col| self.at(col, row) == '.'))
.collect::<Vec<_>>();
let cols = (0..self.width)
.rev()
.filter(|&col| (0..self.height).all(|row| self.at(col, row) == '.'))
.collect::<Vec<_>>();
Expansions { rows, cols }
}
fn iter_galaxies<'a>(&'a self) -> impl Iterator<Item = (usize, usize)> + 'a {
(0..self.height)
.into_iter()
.flat_map(|row| (0..self.width).into_iter().map(move |col| (col, row)))
.filter(|(col, row)| self.at(*col, *row) == '#')
}
}
#[derive(Debug, Clone)]
struct Expansions {
rows: Vec<usize>,
cols: Vec<usize>,
}
impl Expansions {
fn distance(&self, factor: usize, (x1, y1): (usize, usize), (x2, y2): (usize, usize)) -> usize {
let min_x = x1.min(x2);
let max_x = x1.max(x2);
let min_y = y1.min(y2);
let max_y = y1.max(y2);
let expanded_rows = self
.rows
.iter()
.filter(|row| (min_y..=max_y).contains(row))
.count();
let expanded_cols = self
.cols
.iter()
.filter(|col| (min_x..=max_x).contains(col))
.count();
(max_x - min_x) + (max_y - min_y) + (expanded_rows + expanded_cols) * (factor - 1)
}
}

View File

@ -11,7 +11,8 @@ mod day07;
mod day08;
mod day09;
mod day10;
mod day11;
fn main() {
day10::run();
day11::run();
}