This commit is contained in:
MaxJa4
2023-12-21 10:55:54 +01:00
parent 7a203d3572
commit 260450a702
3 changed files with 274 additions and 0 deletions

124
src/bin/part11.rs Normal file
View File

@@ -0,0 +1,124 @@
use std::time;
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct Galaxy {
id: u16,
x: i16,
y: i16,
}
fn main() {
let t1 = time::Instant::now();
let mut galaxy_map = load_input();
let d1 = t1.elapsed();
println!("Loading: {:?}", d1);
let t2 = time::Instant::now();
expand_map(&mut galaxy_map);
//print_map(&galaxy_map);
let d2 = t2.elapsed();
println!("Expanding: {:?}", d2);
let t3 = time::Instant::now();
let parsed_map = parse_map(&galaxy_map);
//println!("{:?}", parsed_map);
let d3 = t3.elapsed();
println!("Parsing: {:?}", d3);
let t4 = time::Instant::now();
let distances = calc_distances(&parsed_map);
let d4 = t4.elapsed();
println!("Calculating: {:?}", d4);
let d5 = t1.elapsed();
let sum_distances = distances.iter().sum::<u32>();
println!("Summing: {:?}", d5);
println!("Total: {:?}", d1 + d2 + d3 + d4 + d5);
println!("Sum of distances: {}", sum_distances);
}
fn calc_distances(galaxies: &Vec<Galaxy>) -> Vec<u32> {
let mut distances = Vec::new();
for (i, g1) in galaxies.iter().enumerate() {
for g2 in &galaxies[i + 1..] {
let distance = ((g2.x - g1.x).abs() + (g2.y - g1.y).abs()) as u32;
distances.push(distance);
}
}
distances
}
fn parse_map(galaxy_map: &Vec<Vec<bool>>) -> Vec<Galaxy> {
let mut galaxies = Vec::new();
for (y, row) in galaxy_map.iter().enumerate() {
for (x, c) in row.iter().enumerate() {
if *c {
galaxies.push(Galaxy {
id: galaxies.len() as u16 + 1,
x: x as i16,
y: y as i16,
});
}
}
}
galaxies
}
#[allow(dead_code)]
fn print_map(galaxy_map: &Vec<Vec<bool>>) {
for row in galaxy_map {
for c in row {
print!("{}", if *c { '#' } else { '.' });
}
println!();
}
}
fn expand_map(galaxy_map: &mut Vec<Vec<bool>>) {
let mut new_map = Vec::new();
for row in &mut *galaxy_map {
new_map.push(row.clone());
if row.iter().all(|c| !*c) {
new_map.push(row.repeat(1_000_000));
}
}
let mut shift_offset = 0;
for (i, _) in galaxy_map[0].iter().enumerate() {
if galaxy_map.iter().all(|row| !row[i]) {
for row in new_map.iter_mut() {
row.insert(i + shift_offset, false);
}
shift_offset += 1;
}
}
*galaxy_map = new_map;
}
fn load_input() -> Vec<Vec<bool>> {
let data = std::fs::read_to_string("src/bin/sources/part11.txt")
.expect("Could not read file")
.lines()
.map(|line| {
line.chars()
.map(|c| {
match c {
'#' => true,
'.' => false,
_ => panic!("Invalid input")
}
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
data
}

View File

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

140
src/bin/sources/part11.txt Normal file
View File

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