various stuff on older solutions
This commit is contained in:
5 files changed
+171
-40
No files matched your search
+33
-29
@@ -1,8 +1,9 @@
|
||||
use aoc_runner_derive::aoc;
|
||||
use core::panic;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use itertools::Itertools;
|
||||
use rayon::prelude::*;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::str::Lines;
|
||||
|
||||
// PARSE
|
||||
@@ -89,31 +90,31 @@ impl GardenMap {
|
||||
}
|
||||
panic!("didn't find the start square!");
|
||||
}
|
||||
fn reachable_after(&self, from: &Position, n: usize) -> u64 {
|
||||
let bar = ProgressBar::new(n as u64).with_style(
|
||||
ProgressStyle::with_template(
|
||||
"[{elapsed_precise}/{eta_precise}] {bar:40.cyan/blue} {pos:>7}/{len:7} {per_sec}",
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
let mut visited_after: Vec<HashSet<Position>> = Vec::new();
|
||||
visited_after.push(HashSet::from([*from]));
|
||||
for i in 1..n + 1 {
|
||||
visited_after.push(
|
||||
visited_after[i - 1]
|
||||
.iter()
|
||||
.flat_map(|last| self.adjacent_to(last))
|
||||
.collect(),
|
||||
);
|
||||
bar.inc(1);
|
||||
// if primes::is_prime(i as u64) {
|
||||
// println!("count after {} steps: {}", i, visited_after[i].len());
|
||||
// }
|
||||
}
|
||||
fn distances(&self, from: &Position, n: usize) -> HashMap<Position, usize> {
|
||||
let mut map = HashMap::new();
|
||||
let mut q = VecDeque::new();
|
||||
|
||||
visited_after[n].len() as u64
|
||||
q.push_back((*from, 0));
|
||||
|
||||
while let Some((next, dist)) = q.pop_front() {
|
||||
if dist > n {
|
||||
continue;
|
||||
}
|
||||
if !map.contains_key(&next) {
|
||||
map.insert(next, dist);
|
||||
for adj in self.adjacent_to(&next) {
|
||||
q.push_back((adj, dist + 1))
|
||||
}
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
fn reachable_count_after(&self, from: &Position, n: usize) -> u64 {
|
||||
fn reachable_after(&mut self, from: &Position, n: usize) -> u64 {
|
||||
let distances = self.distances(from, n);
|
||||
|
||||
distances.values().filter(|d| **d <= n && **d % 2 == n % 2).count() as u64
|
||||
}
|
||||
fn reachable_count_after(&mut self, from: &Position, n: usize) -> u64 {
|
||||
let dim = self.width() as f64;
|
||||
let target_mod = (n % self.width()) as f64;
|
||||
let x_values: Vec<f64> = vec![target_mod, target_mod + dim, target_mod + 2. * dim];
|
||||
@@ -135,9 +136,9 @@ impl GardenMap {
|
||||
fn draw_with_bounds(&self, from: &Position, to: &Position) {
|
||||
for row in from.1..to.1 + 1 {
|
||||
for col in from.0..to.0 + 1 {
|
||||
// print!("{}", self.at(&(col, row)).c);
|
||||
print!("{}", self.at(&(col, row)).c);
|
||||
}
|
||||
// println!();
|
||||
println!();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,13 +155,14 @@ impl GardenMap {
|
||||
// PROBLEM 1 solution
|
||||
|
||||
fn problem1_impl(input: &str, n: usize) -> u64 {
|
||||
let map = GardenMap::from(input.lines());
|
||||
let mut map = GardenMap::from(input.lines());
|
||||
// println!("map: {:?} start: {:?}", map.map, &map.start);
|
||||
// map.draw_with_bounds(
|
||||
// &(-(map.width() as isize), -(map.height() as isize)),
|
||||
// &(map.width() as isize * 2 + 1, map.height() as isize * 2 + 1),
|
||||
// );
|
||||
map.reachable_count_after(&map.start, n)
|
||||
let start = map.start;
|
||||
map.reachable_after(&start, n)
|
||||
}
|
||||
|
||||
#[aoc(day21, part1)]
|
||||
@@ -171,7 +173,9 @@ fn part1(input: &str) -> u64 {
|
||||
// PROBLEM 2 solution
|
||||
#[aoc(day21, part2)]
|
||||
fn part2(input: &str) -> u64 {
|
||||
problem1_impl(input, 26501365)
|
||||
let mut map = GardenMap::from(input.lines());
|
||||
let start = map.start;
|
||||
map.reachable_count_after(&start, 26501365)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in new issue
Block a user