Day 15, switch to vec for more performance
This commit is contained in:
parent
ce55569030
commit
739b17dd32
1 changed files with 4 additions and 5 deletions
|
@ -1,9 +1,8 @@
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::HashMap;
|
||||||
use std::{collections::VecDeque, io::Write};
|
|
||||||
type Data = Vec<usize>;
|
type Data = Vec<usize>;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug,Clone)]
|
||||||
enum Seen {
|
enum Seen {
|
||||||
Never,
|
Never,
|
||||||
Once(usize),
|
Once(usize),
|
||||||
|
@ -45,11 +44,11 @@ pub fn input_generator(input: &str) -> Data {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_number(start: &[usize], num_turns: usize) -> usize {
|
fn get_number(start: &[usize], num_turns: usize) -> usize {
|
||||||
let mut cache: BTreeMap<usize, Seen> = BTreeMap::new();
|
let mut cache = vec![Seen::Never;num_turns];
|
||||||
let mut nums = start.to_vec();
|
let mut nums = start.to_vec();
|
||||||
let mut num=std::usize::MAX;
|
let mut num=std::usize::MAX;
|
||||||
for i in 1..num_turns {
|
for i in 1..num_turns {
|
||||||
num = cache.entry(nums[i - 1]).or_default().add(i);
|
num = cache[nums[i - 1]].add(i);
|
||||||
if i >= nums.len() {
|
if i >= nums.len() {
|
||||||
nums.push(num);
|
nums.push(num);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue