Day 11 - Part 1

Signed-off-by: Anthony Oteri <anthony.oteri@gmail.com>
This commit is contained in:
Anthony Oteri
2023-12-11 18:52:00 -05:00
parent 6fd19bdcc7
commit 8580a33dcd
10 changed files with 379 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
[package]
name = "day-11"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
itertools = { workspace = true }
nom = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
miette = { workspace = true }
thiserror = { workspace = true }
dhat = { workspace = true }
[dev-dependencies]
divan = { workspace = true }
env_logger = { workspace = true }
test-log = { workspace = true }
[[bench]]
name = "day-00"
path = "benches/benchmark.rs"
harness = false
[features]
dhat-heap = []
+15
View File
@@ -0,0 +1,15 @@
use day_11::*;
fn main() {
divan::main();
}
#[divan::bench]
fn part1() {
part1::process(divan::black_box("../input.txt")).unwrap();
}
#[divan::bench]
fn part2() {
part2::process(divan::black_box("../input.txt")).unwrap();
}
+140
View File
@@ -0,0 +1,140 @@
.#...........#.........................................................................#...............#...........#........................
.......................#....................................................................................................................
...............................#...........#.................#.............................#.............................................#..
......#............................................#........................................................................................
......................................................................#.....#.....................#.........................................
.................#.......#.......................................#................#.....#.....................#.............................
.........................................#.................#................................................................................
................................#.............................................................#..........#..................................
#.......................................................................#..............................................#.................#..
..........#....................................#......................................#............................................#........
................#....................................#........#.............................................................................
......#........................................................................#............................................................
......................#..............#..............................................................#..........#.....#......................
..................................................#.......................#..................................................#..........#...
............................#...............................................................................................................
........#...................................................................................................................................
..................#.......................#................................................................#................................
.............................................................#........#.....................................................................
..............................#.......#......................................#.............#........................#.......................
.#............................................................................................................#...........................#.
...................................................................................................#....................#...................
....................#......................#.............#.....#..................................................................#.........
.................................................................................................................#..........................
............................................................................................................................................
....................................................................#.....................#.................................................
.................................#..............................................#..............#............#.............#.................
...........................#............................................#...........................................#.......................
......................#.........................#...........................................................................................
.......................................#....................................#...................................#...........................
.....#..........#.........................................#...............................................................................#.
#..........#......................#...................................................#.......#.....#.......................................
.........................#..................................................................................................................
....................#..............................#.............#.........................................#.......#........#...............
...........................................#........................................................................................#.......
...........................................................#....................#...........................................................
..........#....................................................................................................#............................
...............#................................#..........................#..............................................................#.
..#.................................#.................................................................#.......................#.............
.....................#......#...............#........................#................................................#.....................
..............................................................................................#....................................#........
................................#.......#...................................................................................................
..........#................................................#...................#.....#..............#..........#..........#.................
.........................#..................................................................................................................
..................#...............................#...........................................................................#.............
.........................................................................#............................................#.....................
........#.....#..............................................................................#.........#..................................#.
.#...............................#......#............#.............................................................................#........
....................................................................................#.............#...............#........#................
...........................#................................................................................................................
......................#..................................#..............#................................#.............#................#...
...............................................................#..............#.............................................................
..................................#........................................................#........#.......................................
......#.................................#.......#....................#...............................................................#......
.................#............#..............................................................................#.............................#
............................................................................................................................................
..#.....................#.............................................................................#.....................................
.......................................................................................#..............................#.....................
...............................................................................#.............#..............................................
....................#.....................#...............................................................#.................................
..........#........................#............#...................#.............................................#...............#.........
............................#.............................#..........................#......................................................
...........................................................................................#................................................
.................................................................#.................................#.........................#........#.....
.................#........................................................#..................................#..............................
......................................#......................#..............................................................................
.............#...........#...............................................................................#..........#.......................
.................................#...........#...........................................................................#..............#...
.......#............................................#.....#....................#..............#.....#.......................................
...................#...........................................#................................................#...........................
........................................................................................#...................................................
...#....................#.....#............#.........................................................................................#......
...............#.............................................................#..................#........#................#.................
.........#.....................................#............................................................................................
................................................................................................................................#...........
.....................#.............#.........................................................................#..............................
.....#...............................................................................................................#..............#......#
...........#.......................................#.....#.................#.................#..............................................
............................................................................................................................................
.#.......................................#..........................................#................#......................................
......................#.............#.................#.......................#...........#.............................#...................
...............#.............#................................#......#.......................................................#..............
............................................................................................................................................
...#............................................................................................#....................#......................
..........#.....................#.............................................................................#.............................
...........................#...............................................................................................#...............#
.....................#......................................#.......#......#........#..................#...........................#........
.....#...............................#............#.........................................................................................
............................................................................................................................................
.........................................................#........................................................#.........................
#..............................#..........#..............................................................................#................#.
....................#..............................................#.......................#.........#......................................
............................................................................................................................................
...........#.......................................#.............................................................................#..........
...#......................#...............................................#......................#..........................................
..................#.........................................................................................................................
...........................................................#........#...........#.....#......#........................#.................#...
........#...................................................................................................................................
....................................................................................................#......#................................
................................#...........#....................#.....#...................................................#........#.......
........................#............#......................................#.............#.................................................
...................#............................................................................................#...........................
#................................................................................................................................#..........
..............#.............................................#...............................................................................
..............................#...............................................#.................#......................................#....
....#....................#......................................#...................#...............................#.......#...............
........................................#..........#........................................#................#..............................
............................................................................................................................................
............................................................................................................................................
........................................................................................................................................#...
...........................................................#..................#.................#.................#.........................
.......#........................................................#................................................................#..........
..........................#......#......#..................................................................................#................
...............................................#.....#.................#....................................................................
......................................................................................................#.....................................
................#....................#.....................................................................#........#.......................
..............................................................................................................................#.............
...........................................#.............................................................................................#..
....#........................#..............................#.............#................#................................................
......................#..........................................................................................#......#...................
....................................................#................................#.........#............................................
..........................#.......................................#.........................................................................
.................#........................#.....#........................................................#..................................
.#.........................................................#............#....................................................#..............
..........#..................#.....#..................................................................................................#.....
.................................................................................#..........................................................
.....................................................#....................................#.........#...........#.....#.....................
.............................................#.....................#.......................................#.......................#........
................................#...........................................................................................................
......#........................................................#..............................#.............................................
............#......................................#.....#............................#........................................#............
............................................................................#...............................................................
.......................................#...............................#...................#..............................#...........#.....
.......................#..............................#..........................#..............................#...........................
.................................................................#...................................#......................................
......................................................................................................................#...................#.
.................#..........................................................................................................................
.........#......................#.....#...........#.......................................................#.................................
...........................................#.................#................#.....................................................#.......
..#...............................................................#.........................................................................
.....................#......#......#.............................................................#............#...........#.................
+21
View File
@@ -0,0 +1,21 @@
use day_11::part1::process;
use miette::Context;
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
#[tracing::instrument]
fn main() -> miette::Result<()> {
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();
#[cfg(not(feature = "dhat-heap"))]
tracing_subscriber::fmt::init();
let file = include_str!("../../input.txt");
let result = process(file).context("process part 1")?;
println!("{}", result);
Ok(())
}
+21
View File
@@ -0,0 +1,21 @@
use day_11::part2::process;
use miette::Context;
#[cfg(feature = "dhat-heap")]
#[global_allocator]
static ALLOC: dhat::Alloc = dhat::Alloc;
#[tracing::instrument]
fn main() -> miette::Result<()> {
#[cfg(feature = "dhat-heap")]
let _profiler = dhat::Profiler::new_heap();
#[cfg(not(feature = "dhat-heap"))]
tracing_subscriber::fmt::init();
let file = include_str!("../../input.txt");
let result = process(file).context("process part 1")?;
println!("{}", result);
Ok(())
}
+9
View File
@@ -0,0 +1,9 @@
use miette::Diagnostic;
use thiserror::Error;
#[derive(Error, Diagnostic, Debug)]
pub enum AocError {
#[error(transparent)]
#[diagnostic(code(aoc::io_error))]
IoError(#[from] std::io::Error),
}
+4
View File
@@ -0,0 +1,4 @@
pub mod error;
pub mod part1;
pub mod part2;
+113
View File
@@ -0,0 +1,113 @@
use std::collections::{BTreeMap, BTreeSet};
use itertools::Itertools;
use crate::error::AocError;
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
struct Point {
x: usize,
y: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Tile {
Galax,
Empty,
}
fn parse(input: &str) -> BTreeMap<Point, Tile> {
let input = input
.lines()
.map(|line| {
line.chars()
.map(|c| match c {
'#' => Tile::Galax,
'.' => Tile::Empty,
_ => panic!("Unknown tile type"),
})
.collect_vec()
})
.collect::<Vec<Vec<Tile>>>();
let mut expanded_rows = Vec::new();
for line in input.iter() {
if line.iter().all(|t| matches!(t, Tile::Empty)) {
expanded_rows.push(line.clone());
}
expanded_rows.push(line.clone());
}
let mut translated = Vec::new();
for col_index in 0..expanded_rows[0].len() {
let col = expanded_rows
.iter()
.map(|r| r[col_index].clone())
.collect::<Vec<Tile>>();
if col.iter().all(|t| matches!(t, Tile::Empty)) {
translated.push(col.clone());
}
translated.push(col.clone());
}
translated
.into_iter()
.enumerate()
.flat_map(|(y, line)| {
line.into_iter().enumerate().map(move |(x, tile)| {
// Working off translated map, so re-translate back
let pos = Point { x, y };
(pos, tile.clone())
})
})
.collect()
}
#[tracing::instrument]
pub fn process(input: &str) -> miette::Result<u64, AocError> {
let map = parse(input);
let locations = map
.iter()
.filter_map(|(p, t)| matches!(t, Tile::Galax).then_some(p.clone()))
.collect::<BTreeSet<Point>>();
let pairs: BTreeSet<BTreeSet<Point>> = locations
.iter()
.cartesian_product(locations.iter())
.filter(|(p1, p2)| p1 != p2)
.map(|(p1, p2)| BTreeSet::from([p1.clone(), p2.clone()]))
.collect();
let pairs: Vec<(Point, Point)> = pairs
.into_iter()
.map(|pair| {
let mut pair = pair.into_iter();
let p1 = pair.next().unwrap();
let p2 = pair.next().unwrap();
(p1, p2)
})
.collect();
let distances: Vec<usize> = pairs
.iter()
.map(|(p1, p2)| {
let x = (p1.x as i64 - p2.x as i64).abs();
let y = (p1.y as i64 - p2.y as i64).abs();
(x + y) as usize
})
.collect();
Ok(distances.iter().map(|&d| d as u64).sum())
}
#[cfg(test)]
mod tests {
use super::*;
#[test_log::test]
fn test_process() -> miette::Result<()> {
let input = include_str!("../test-input.txt");
assert_eq!(374, process(input)?);
Ok(())
}
}
+18
View File
@@ -0,0 +1,18 @@
use crate::error::AocError;
#[tracing::instrument]
pub fn process(input: &str) -> miette::Result<u64, AocError> {
Ok(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test_log::test]
fn test_process() -> miette::Result<()> {
let input = include_str!("../test-input.txt");
assert_eq!(0, process(input)?);
Ok(())
}
}
+10
View File
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....