day 11 part 1

This commit is contained in:
Kristofers Solo 2023-12-11 22:23:42 +02:00
parent 605037250c
commit df25a506ba
8 changed files with 405 additions and 0 deletions

7
2023/Cargo.lock generated
View File

@ -198,6 +198,13 @@ dependencies = [
"itertools",
]
[[package]]
name = "day-11"
version = "0.1.0"
dependencies = [
"color-eyre",
]
[[package]]
name = "either"
version = "1.9.0"

9
2023/day-11/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[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]
color-eyre.workspace = true

140
2023/day-11/input.txt Normal file
View File

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

View File

@ -0,0 +1,9 @@
use color_eyre::Result;
use day_11::part1::process;
fn main() -> Result<()> {
let file = include_str!("../../input.txt");
let result = process(file)?;
println!("{}", result);
Ok(())
}

View File

@ -0,0 +1,9 @@
use color_eyre::Result;
use day_11::part2::process;
fn main() -> Result<()> {
let file = include_str!("../../input.txt");
let result = process(file)?;
println!("{}", result);
Ok(())
}

2
2023/day-11/src/lib.rs Normal file
View File

@ -0,0 +1,2 @@
pub mod part1;
pub mod part2;

210
2023/day-11/src/part1.rs Normal file
View File

@ -0,0 +1,210 @@
use color_eyre::Result;
#[derive(Debug, Clone, Copy, PartialEq)]
enum Universe {
Galaxy,
Empty,
}
impl Universe {
fn is_empty(&self) -> bool {
match self {
Universe::Galaxy => false,
Universe::Empty => true,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Point {
x: usize,
y: usize,
}
impl Point {
fn new(x: usize, y: usize) -> Self {
Self { x, y }
}
fn distance(&self, other: &Self) -> usize {
(self.x as i32 - other.x as i32).abs() as usize
+ (self.y as i32 - other.y as i32).abs() as usize
}
}
fn expand_universe(input: Vec<Vec<Universe>>) -> Result<Vec<Vec<Universe>>> {
let mut output = input.clone();
let mut add_cols = Vec::new();
let mut add_rows = Vec::new();
// Check if any row contains only dots
for (idx, line) in input.iter().enumerate() {
if line.iter().all(|&ch| ch.is_empty()) {
add_rows.push(idx + 1);
}
}
// Check if any column contains only dots
let num_cols = input.first().unwrap().len();
for col in 0..num_cols {
if input.iter().all(|line| line.get(col).unwrap().is_empty()) {
add_cols.push(col);
}
}
// Add rows if necessary
for row in add_rows {
let new_row = vec![Universe::Empty; num_cols];
output.insert(row, new_row);
}
// Add columns if necessary
for (idx, &col) in add_cols.iter().enumerate() {
for line in output.iter_mut() {
line.insert(col + idx + 1, Universe::Empty);
}
}
Ok(output)
}
pub fn process(input: &str) -> Result<usize> {
let universe: Vec<Vec<Universe>> = input
.lines()
.map(|line| {
line.chars()
.map(|ch| match ch {
'.' => Universe::Empty,
'#' => Universe::Galaxy,
_ => unimplemented!(),
})
.collect()
})
.collect();
// Create a vector of points
let points = expand_universe(universe)?.iter().enumerate().fold(
Vec::new(),
|acc: Vec<Point>, (y, row)| {
row.iter().enumerate().fold(acc, |mut acc, (x, col)| {
if !col.is_empty() {
acc.push(Point::new(x + 1, y + 1));
}
acc
})
},
);
// Sum the shortest path between each pair of points
let sum = points.iter().enumerate().fold(0, |acc, (idx, point)| {
points
.iter()
.skip(idx + 1)
.fold(acc, |acc, other| acc + point.distance(other))
});
Ok(sum)
/* TRIED
- 10492576
*/
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> Result<()> {
let input = "...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....";
assert_eq!(374, process(input)?);
Ok(())
}
#[test]
fn test_expand_no_expansion_needed() -> Result<()> {
let input = vec![
vec![Universe::Galaxy, Universe::Empty],
vec![Universe::Empty, Universe::Galaxy],
];
assert_eq!(expand_universe(input.clone())?, input);
Ok(())
}
#[test]
fn test_expand_last_rows() -> Result<()> {
let input = vec![
vec![Universe::Galaxy, Universe::Galaxy],
vec![Universe::Empty, Universe::Empty],
];
assert_eq!(
expand_universe(input)?,
vec![
vec![Universe::Galaxy, Universe::Galaxy],
vec![Universe::Empty, Universe::Empty],
vec![Universe::Empty, Universe::Empty],
]
);
Ok(())
}
#[test]
fn test_expand_first_rows() -> Result<()> {
let input = vec![
vec![Universe::Empty, Universe::Empty],
vec![Universe::Galaxy, Universe::Galaxy],
];
assert_eq!(
expand_universe(input)?,
vec![
vec![Universe::Empty, Universe::Empty],
vec![Universe::Empty, Universe::Empty],
vec![Universe::Galaxy, Universe::Galaxy],
]
);
Ok(())
}
#[test]
fn test_expand_first_columns() -> Result<()> {
let input = vec![
vec![Universe::Empty, Universe::Galaxy],
vec![Universe::Empty, Universe::Galaxy],
];
assert_eq!(
expand_universe(input)?,
vec![
vec![Universe::Empty, Universe::Empty, Universe::Galaxy],
vec![Universe::Empty, Universe::Empty, Universe::Galaxy],
]
);
Ok(())
}
#[test]
fn test_expand_last_columns() -> Result<()> {
let input = vec![
vec![Universe::Galaxy, Universe::Empty],
vec![Universe::Galaxy, Universe::Empty],
];
assert_eq!(
expand_universe(input)?,
vec![
vec![Universe::Galaxy, Universe::Empty, Universe::Empty],
vec![Universe::Galaxy, Universe::Empty, Universe::Empty],
]
);
Ok(())
}
}

19
2023/day-11/src/part2.rs Normal file
View File

@ -0,0 +1,19 @@
use color_eyre::Result;
pub fn process(input: &str) -> Result<usize> {
todo!("day xx - part 2");
Ok(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_process() -> Result<()> {
let input = "";
todo!("haven't built test yet");
assert_eq!(0, process(input)?);
Ok(())
}
}