Rust: day 15 first working

This commit is contained in:
Sijmen 2020-12-15 18:08:40 +01:00
parent 1aaba8eba8
commit e03fc564c1
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48

View file

@ -377,6 +377,94 @@ mod day08 {
} }
} }
mod day09 {
use std::time::Duration;
pub fn run(_: bool) -> Duration {
Duration::new(0, 0)
}
}
mod day10 {
use std::time::Duration;
pub fn run(_: bool) -> Duration {
Duration::new(0, 0)
}
}
mod day11 {
use std::time::Duration;
pub fn run(_: bool) -> Duration {
Duration::new(0, 0)
}
}
mod day12 {
use std::time::Duration;
pub fn run(_: bool) -> Duration {
Duration::new(0, 0)
}
}
mod day13 {
use std::time::Duration;
pub fn run(_: bool) -> Duration {
Duration::new(0, 0)
}
}
mod day14 {
use std::time::Duration;
pub fn run(_: bool) -> Duration {
Duration::new(0, 0)
}
}
mod day15 {
use std::time::{Duration, Instant};
const END: usize = 30_000_000;
pub fn run(print: bool) -> Duration {
let input: Vec<usize> = "0,1,5,10,3,12,19"
.split(",")
.map(|i| i.parse().unwrap())
.collect();
let instant = Instant::now();
let mut history = vec![0; END];
let mut last = input[0];
for turn in 0..input.len() {
history[last] = turn;
last = input[turn];
}
for turn in input.len()..2020 {
let stored = history[last];
history[last] = turn;
last = if stored == 0 { 0 } else { turn - stored };
}
if print {
println!("Part 1: {}", last);
}
for turn in 2020..END {
let stored = history[last];
history[last] = turn;
last = if stored == 0 { 0 } else { turn - stored };
}
if print {
println!("Part 2: {}", last);
}
instant.elapsed()
}
}
fn main() { fn main() {
let days = [ let days = [
day01::run, day01::run,
@ -387,6 +475,13 @@ fn main() {
day06::run, day06::run,
day07::run, day07::run,
day08::run, day08::run,
day09::run,
day10::run,
day11::run,
day12::run,
day13::run,
day14::run,
day15::run,
]; ];
if let Some(day) = std::env::args().nth(1) { if let Some(day) = std::env::args().nth(1) {
@ -395,10 +490,18 @@ fn main() {
days[day - 1](true); days[day - 1](true);
} }
} else { } else {
let repetitions = 2000;
let mut total = Duration::new(0, 0); let mut total = Duration::new(0, 0);
for day in 0..days.len() { for day in 0..days.len() {
let first_run = days[day](false);
if first_run.as_nanos() == 0 {
println!("Day {:>2}: skipped", day);
continue;
}
let repetitions =
5000.min((Duration::new(5, 0).as_nanos() / first_run.as_nanos()) as u32);
let mut elapsed = Duration::new(0, 0); let mut elapsed = Duration::new(0, 0);
for _ in 0..repetitions { for _ in 0..repetitions {
elapsed += days[day](false) / repetitions; elapsed += days[day](false) / repetitions;