1
0

Optimized day 9 part 2 by not inserting tail position if it hasn't changed

This commit is contained in:
Ishan Jain 2022-12-09 19:57:57 +05:30
parent 8354241bbd
commit c739a665d3
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27

View File

@ -1,8 +1,8 @@
#![feature(byte_slice_trim_ascii)]
#![feature(test)]
extern crate test;
use std::collections::HashSet;
extern crate test;
const INPUTS: [&[u8]; 2] = [
include_bytes!("../inputs/sample.txt"),
@ -48,7 +48,8 @@ fn main() {
fn solution(input: Vec<Move>) -> usize {
let mut locs = [(0, 0); 10];
let mut set: HashSet<(i32, i32)> = HashSet::with_capacity(3000);
let mut set: HashSet<(i32, i32)> = HashSet::with_capacity_and_hasher(3000, Default::default());
set.insert((0, 0));
for mmove in input {
let (steps, (dsxh, dsyh)) = match mmove {
@ -58,6 +59,8 @@ fn solution(input: Vec<Move>) -> usize {
Move::D(v) => (v, (0, 1)),
};
let locs9 = locs[9];
for _ in 0..steps {
// Update Head position
locs[0].0 += dsxh;
@ -72,9 +75,11 @@ fn solution(input: Vec<Move>) -> usize {
locs[i] = loci;
}
if locs9 != locs[9] {
set.insert(locs[9]);
}
}
}
set.len()
}