1
0
Fork 0

Optimized day 9 part 2 by breakinng out early if a knot doesn't move

This commit is contained in:
Ishan Jain 2022-12-09 19:41:21 +05:30
parent 8b91005736
commit 8354241bbd
Signed by: ishan
GPG Key ID: 0506DB2A1CC75C27
1 changed files with 6 additions and 2 deletions

View File

@ -51,7 +51,7 @@ fn solution(input: Vec<Move>) -> usize {
let mut set: HashSet<(i32, i32)> = HashSet::with_capacity(3000);
for mmove in input {
let (steps, (dsxh, dsyh)): (u8, (i32, i32)) = match mmove {
let (steps, (dsxh, dsyh)) = match mmove {
Move::R(v) => (v, (1, 0)),
Move::L(v) => (v, (-1, 0)),
Move::U(v) => (v, (0, -1)),
@ -65,7 +65,11 @@ fn solution(input: Vec<Move>) -> usize {
// One by one, Updated position of each knot
for i in 1..10 {
locs[i] = move_tail(locs[i - 1], locs[i]);
let loci = move_tail(locs[i - 1], locs[i]);
if loci == locs[i] {
break;
}
locs[i] = loci;
}
set.insert(locs[9]);