[TASK] Solved Day 11
This commit is contained in:
140
input/day11.txt
Normal file
140
input/day11.txt
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
............#..................#..........................................................#..........................#......................
|
||||||
|
.................................................................#...............#..............#...............#......................#....
|
||||||
|
...................................#.....................#..................#............................#...................#..............
|
||||||
|
#.....................#.....................#...............................................................................................
|
||||||
|
........................................................................................#............#......................................
|
||||||
|
.....#..............................................#.......................................................................................
|
||||||
|
.......................................#................................#..........#...........#......................................#.....
|
||||||
|
..................#...........#.............................................................................................................
|
||||||
|
.............................................#............#.................................................................................
|
||||||
|
.#............................................................................#..................................#..........................
|
||||||
|
............#......................#....................................................................#...................#.............#.
|
||||||
|
.......#............#....................#........#...................................#......................#..............................
|
||||||
|
...................................................................#................................#.......................................
|
||||||
|
..............................#.................................................................................................#...........
|
||||||
|
..#...........................................................................................#.......................#.....................
|
||||||
|
..............................................................................#.............................................................
|
||||||
|
.......................................#.........#............#...........................#................#..............................#.
|
||||||
|
........................................................#.............#.....................................................................
|
||||||
|
.....#..........................#....................................................................#....................#.................
|
||||||
|
....................#.......................#...................................#...............#...............#......................#....
|
||||||
|
...............#..................................................................................................................#.........
|
||||||
|
..#......................................................................#...................................................#..............
|
||||||
|
.................................................#............#.........................................#.............#.....................
|
||||||
|
....................................#.......................................................................................................
|
||||||
|
...........#..........................................................................#.....................................................
|
||||||
|
.................#..........................................................................................................................
|
||||||
|
.............................................#............................#....................................................#............
|
||||||
|
.........................#.......#..........................#...............................................................................
|
||||||
|
.......#............#....................................................................................................#..........#.......
|
||||||
|
...................................................................#................#...........................#...........................
|
||||||
|
..............#....................................#..........................#..............#..............................................
|
||||||
|
..#....................#....................................................................................#........#......................
|
||||||
|
.........................................................................#......................................................#...........
|
||||||
|
......................................#.........#...........................................................................................
|
||||||
|
..........#.................................................................................................................................
|
||||||
|
.....................#......................#...............#.................................................#.............................
|
||||||
|
...............................#...................................................#....................................#.................#.
|
||||||
|
.............................................................................................#..............................................
|
||||||
|
...................................................................#........................................................................
|
||||||
|
................#..........#.....................#..................................................................................#.......
|
||||||
|
...................................#............................................#..................#........................................
|
||||||
|
.....#.....................................................................#...............................#.....#..........................
|
||||||
|
............#.........................................................................................................#.....................
|
||||||
|
.....................#.............................#.......................................................................#................
|
||||||
|
..............................................................#...............................#..........................................#..
|
||||||
|
.....................................#.......#.................................................................#............................
|
||||||
|
...............#...................................................................#.....#..............................#...................
|
||||||
|
...#.....................#.........................................#......#...........................#.....................................
|
||||||
|
.........#......................................................................................#.............................#.............
|
||||||
|
..........................................................#.................................................................................
|
||||||
|
..................................#.......#...........................................#......................#......................#.......
|
||||||
|
.............................................................................#..............................................................
|
||||||
|
.....#...........................................#..................................................#....................#..................
|
||||||
|
............................................................................................................................................
|
||||||
|
............................................................................................................................................
|
||||||
|
.................#...................#........................#......#..................................#...................................
|
||||||
|
..........................#.........................#.....................................#......#.........................#................
|
||||||
|
..............................................#...........................#.....................................#...........................
|
||||||
|
#.....................#.................#............................................................................................#......
|
||||||
|
...............................#..........................#.....................................................................#...........
|
||||||
|
..........#.........................#.............................#...........#....................................#........................
|
||||||
|
.....................................................#......................................................................................
|
||||||
|
.............................................................................................#..............................................
|
||||||
|
............................................................................................................................................
|
||||||
|
...............#...........#..........#.....#..................#.......................#......................#.......#..............#......
|
||||||
|
........#...........#.............................#.....#...................................................................................
|
||||||
|
............................................................................................................................................
|
||||||
|
.#................................#.............................................#................................................#..........
|
||||||
|
............................................................................................................................................
|
||||||
|
..........................................................................#...............................................................#.
|
||||||
|
..............................#...............#......................................#........#.............#.........#.....................
|
||||||
|
...#................................................#.......................................................................................
|
||||||
|
............................................................................................................................................
|
||||||
|
.......................#...............#...........................................................#..........................#.............
|
||||||
|
.................................................#...........#......................................................#.......................
|
||||||
|
...........#............................................................................#...................................................
|
||||||
|
.....#..................................................#.....................................#.............#..............................#
|
||||||
|
..................#..........#..............................................#..........................#..............................#.....
|
||||||
|
....................................................#.......................................................................................
|
||||||
|
..#............................................................................................................#................#...........
|
||||||
|
.............................................................#........#..........#.....#....................................................
|
||||||
|
...........#..........#............#.......#...............................................................................#................
|
||||||
|
............................................................................................................................................
|
||||||
|
......................................................#..............................................#......................................
|
||||||
|
.........................#.....#...............................................#................................#................#..........
|
||||||
|
...#.......................................................................................................#...........................#....
|
||||||
|
........#...........................#....................................#...............#..................................................
|
||||||
|
..........................................#.......................#.........................................................................
|
||||||
|
............................................................#.............................................................#.................
|
||||||
|
...........................#...................................................................#.....................#......................
|
||||||
|
...............#.................#............#.............................................................................................
|
||||||
|
...#......#..........................................#..................................#...............#...................................
|
||||||
|
.................................................................#......................................................................#...
|
||||||
|
.......................#..................#...............#..................#..............................................................
|
||||||
|
.................................................#...................................#.............#............................#...........
|
||||||
|
................#...........................................................................................................................
|
||||||
|
...................................#.........................................................#........................#.............#.......
|
||||||
|
.............................................................#..........................#......................#............#..............#
|
||||||
|
...#..................#.............................#..........................#............................................................
|
||||||
|
........#......................................#............................................................................................
|
||||||
|
..............#............#...........#............................................................#.......................................
|
||||||
|
....................................................................#.......................#...............................................
|
||||||
|
.#....................................................................................#........................................#.......#....
|
||||||
|
...........#...................#............................................................................................................
|
||||||
|
.........................#..........#.......#.........#..................#..................................#...............................
|
||||||
|
................................................................#....................................................#......................
|
||||||
|
...#.............................................................................................#.....#....................................
|
||||||
|
.................................................#....................#.....................................................................
|
||||||
|
.......................#.........#..................................................#.......#...................#........#..................
|
||||||
|
........................................................................................................................................#...
|
||||||
|
...........#................................................#..............................................#................................
|
||||||
|
.....................................#..........................................................#...........................................
|
||||||
|
....#.......................................#....................................................................................#..........
|
||||||
|
...................#..............................................#.........#..........#.............#......................................
|
||||||
|
............................................................................................................................................
|
||||||
|
............................................................................................................................................
|
||||||
|
.............#.......................................#...............#......................#......................#...............#........
|
||||||
|
...............................................#............................................................................................
|
||||||
|
....#.................................#.......................................................................................#.............
|
||||||
|
............................#.................................................#......#.....................................................#
|
||||||
|
...................#..............#.......#...............#.....................................................#.........#.................
|
||||||
|
..................................................................#.....................................#...................................
|
||||||
|
...................................................#..............................#......#.......#..............................#...........
|
||||||
|
.......................................#.....................#..............#......................................#........................
|
||||||
|
.................#..........................................................................................................................
|
||||||
|
..............................................#.......#.................#.................................#.................................
|
||||||
|
.........#...............#.............................................................#.................................#.............#....
|
||||||
|
..#..............................#..................................#.......................................................................
|
||||||
|
...............................................................#...........#.......................................................#........
|
||||||
|
............................................................................................................................#...............
|
||||||
|
................#...........#............#.........#........................................................................................
|
||||||
|
........#.............#........................................................................................#...........................#
|
||||||
|
..........................................................#...............................#..............#...........................#......
|
||||||
|
...............................#................................................................#...........................................
|
||||||
|
..................#.............................................#.........#..........................................#......................
|
||||||
|
.....#................................................#.........................................................................#...........
|
||||||
|
............................................#.....................................#.........#...............................................
|
||||||
|
.......................................................................................................................................#....
|
||||||
|
#..........#.............#......#.................................#...................................#.....................................
|
||||||
|
.....................................#.........#.........................#.....#............................................................
|
||||||
10
input/day11_example.txt
Normal file
10
input/day11_example.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
...#......
|
||||||
|
.......#..
|
||||||
|
#.........
|
||||||
|
..........
|
||||||
|
......#...
|
||||||
|
.#........
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
.......#..
|
||||||
|
#...#.....
|
||||||
71
src/day11.rs
Normal file
71
src/day11.rs
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
use itertools::Itertools;
|
||||||
|
use crate::day_solver::DaySolver;
|
||||||
|
use crate::util::{Coord, Grid};
|
||||||
|
#[cfg(test)]
|
||||||
|
use crate::util::read_file;
|
||||||
|
|
||||||
|
pub struct Day11 {
|
||||||
|
cosmos: Grid<char>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Day11 {
|
||||||
|
|
||||||
|
pub fn create(input: String) -> Self {
|
||||||
|
return Day11 {
|
||||||
|
cosmos: Grid {
|
||||||
|
width: input.lines().next().unwrap().len(),
|
||||||
|
data: input.lines().join("").chars().collect(),
|
||||||
|
default: Some('.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expand_galaxies_and_calculate_distances(&self, expansion: usize) -> usize {
|
||||||
|
|
||||||
|
// self.cosmos.print();
|
||||||
|
let galaxies: Vec<Coord<usize>> = self.cosmos.find_positions(&(|c| c == &'#')).collect();
|
||||||
|
// println!("{:?}", galaxies);
|
||||||
|
|
||||||
|
let expand_cols: Vec<usize> = (0..self.cosmos.width).filter(|x| galaxies.iter().all(|c| c.x != *x)).collect();
|
||||||
|
let expand_rows: Vec<usize> = (0..self.cosmos.height()).filter(|y| galaxies.iter().all(|c| c.y != *y)).collect();
|
||||||
|
|
||||||
|
let expanded_galaxies: Vec<Coord<usize>> = galaxies.iter().map(|g| Coord {
|
||||||
|
x: g.x + (expand_cols.iter().filter(|x| **x < g.x).count() * (expansion - 1)),
|
||||||
|
y: g.y + (expand_rows.iter().filter(|y| **y < g.y).count() * (expansion - 1)),
|
||||||
|
}).collect();
|
||||||
|
|
||||||
|
let mut sum = 0;
|
||||||
|
for i in 0..expanded_galaxies.len() {
|
||||||
|
for j in (i + 1)..expanded_galaxies.len() {
|
||||||
|
let d = expanded_galaxies[i].manhattan_dist(&expanded_galaxies[j]);
|
||||||
|
// println!("Distance between {} at {:?} and {} at {:?} = {}", i, expanded_galaxies[i], j, expanded_galaxies[j], d);
|
||||||
|
sum += d;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DaySolver for Day11 {
|
||||||
|
|
||||||
|
|
||||||
|
fn solve_part1(&mut self) -> String {
|
||||||
|
return self.expand_galaxies_and_calculate_distances(2).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve_part2(&mut self) -> String {
|
||||||
|
return self.expand_galaxies_and_calculate_distances(1000000).to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part1() {
|
||||||
|
let mut day = Day11::create(read_file("input/day11_example.txt"));
|
||||||
|
assert_eq!("374", day.solve_part1());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part2() {
|
||||||
|
let mut day = Day11::create(read_file("input/day11_example.txt"));
|
||||||
|
assert_eq!(8410, day.expand_galaxies_and_calculate_distances(100));
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ extern crate core;
|
|||||||
|
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use crate::day10::Day10;
|
use crate::day10::Day10;
|
||||||
|
use crate::day11::Day11;
|
||||||
use crate::day1::Day1;
|
use crate::day1::Day1;
|
||||||
use crate::day2::Day2;
|
use crate::day2::Day2;
|
||||||
use crate::day3::Day3;
|
use crate::day3::Day3;
|
||||||
@@ -26,8 +27,9 @@ mod day7;
|
|||||||
mod day8;
|
mod day8;
|
||||||
mod day9;
|
mod day9;
|
||||||
mod day10;
|
mod day10;
|
||||||
|
mod day11;
|
||||||
|
|
||||||
const MAX_DAY: u8 = 10;
|
// const MAX_DAY: u8 = 11;
|
||||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -108,6 +110,7 @@ fn build_day_solver(day: u8, input: String) -> Option<Box<dyn DaySolver>> {
|
|||||||
8 => Some(Box::new(Day8::create(input))),
|
8 => Some(Box::new(Day8::create(input))),
|
||||||
9 => Some(Box::new(Day9::create(input))),
|
9 => Some(Box::new(Day9::create(input))),
|
||||||
10 => Some(Box::new(Day10::create(input))),
|
10 => Some(Box::new(Day10::create(input))),
|
||||||
|
11 => Some(Box::new(Day11::create(input))),
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,7 +190,8 @@ fn run_once(single_day: bool, day: u8, silent: bool, bench_results: &mut Vec<Aoc
|
|||||||
|
|
||||||
fn solve_all(silent: bool) -> AocBenchResult {
|
fn solve_all(silent: bool) -> AocBenchResult {
|
||||||
let mut bench_results = Vec::new();
|
let mut bench_results = Vec::new();
|
||||||
for day in 1..(MAX_DAY + 1) {
|
let max_day = (1..).take_while(|d| build_day_solver(*d, "".to_string()).is_some()).last().unwrap();
|
||||||
|
for day in 1..(max_day + 1) {
|
||||||
bench_results.push(solve(day, silent));
|
bench_results.push(solve(day, silent));
|
||||||
}
|
}
|
||||||
return AocBenchResult {
|
return AocBenchResult {
|
||||||
|
|||||||
12
src/util.rs
12
src/util.rs
@@ -1,7 +1,7 @@
|
|||||||
use std::fs;
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use itertools::Itertools;
|
use std::fs;
|
||||||
|
|
||||||
|
use itertools::Itertools;
|
||||||
use num::Integer;
|
use num::Integer;
|
||||||
|
|
||||||
pub fn read_file(filename: &str) -> String {
|
pub fn read_file(filename: &str) -> String {
|
||||||
@@ -75,6 +75,14 @@ impl<T: Clone + Sized> Grid<T> {
|
|||||||
return x < &self.width && y < &self.height()
|
return x < &self.width && y < &self.height()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn find_positions<'a, P>(&'a self, predicate: &'a P) -> impl Iterator<Item = Coord<usize>> + 'a
|
||||||
|
where P: Fn(&'a T) -> bool + 'a {
|
||||||
|
|
||||||
|
return self.data.iter().enumerate()
|
||||||
|
.filter(|(_,x)| predicate(x).to_owned())
|
||||||
|
.map(|(i, _)| self.coord(i));
|
||||||
|
}
|
||||||
|
|
||||||
pub fn validate(&self) {
|
pub fn validate(&self) {
|
||||||
debug_assert!(&self.data.len() % self.width == 0)
|
debug_assert!(&self.data.len() % self.width == 0)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user