Skip to content

Commit f275af0

Browse files
committed
WIP day-21 add tests
1 parent dc27995 commit f275af0

File tree

1 file changed

+167
-64
lines changed
  • AdventOfCode/2024/day-21/src

1 file changed

+167
-64
lines changed

AdventOfCode/2024/day-21/src/lib.rs

Lines changed: 167 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -323,75 +323,101 @@ impl Robot {
323323
let mut curr = from_coord.clone();
324324

325325
while diff.x != 0 || diff.y != 0 {
326-
//match self.pad_position {
327-
// PadPosition::NumPad(c) => {
328-
// // if going down, go down first, then left
329-
// // if going up, go right first, then left
330-
// }
331-
// PadPosition::DirectionPad(c) => {
332-
//
333-
// }
334-
//}
335-
336-
while diff.x != 0 {
337-
if diff.x > 0 { // go left
338-
if curr.y == avoid.y && (curr.x - 1) == avoid.x {
339-
// TODO for dpad - better to go vv< than <v< for path from A to <
340-
println!("[Robot::path()] avoid going left, avoid={}, curr={}, path={}", avoid, curr, path);
341-
break;
342-
} else {
343-
path.push('<');
344-
diff.x -= 1;
345-
curr.x -= 1;
326+
match self.pad_position {
327+
PadPosition::NumPad(c) => {
328+
// if going down, go down first, then left
329+
// if going up, go right first, then left
330+
331+
while diff.x != 0 {
332+
if diff.x > 0 { // go left
333+
if curr.y == avoid.y && (curr.x - 1) == avoid.x {
334+
// TODO for dpad - better to go vv< than <v< for path from A to <
335+
println!("[Robot::path()] avoid going left, avoid={}, curr={}, path={}", avoid, curr, path);
336+
break;
337+
} else {
338+
path.push('<');
339+
diff.x -= 1;
340+
curr.x -= 1;
341+
}
342+
//if curr.y == avoid.y && curr.x == avoid.y {
343+
// panic!("from={}, to={}, diff={}, avoid={}", from_coord, to_coord, diff, avoid);
344+
//}
345+
} else {
346+
path.push('>');
347+
diff.x += 1;
348+
curr.x += 1;
349+
}
350+
}
351+
352+
while diff.y != 0 {
353+
if diff.y < 0 {
354+
if curr.x == avoid.x && (curr.y + 1) == avoid.y {
355+
println!("[Robot::path()] avoid={}, curr={} avoid going down", avoid, curr);
356+
break;
357+
} else {
358+
path.push('v');
359+
diff.y += 1;
360+
curr.y += 1;
361+
}
362+
} else {
363+
if curr.x == avoid.x && (curr.y - 1) == avoid.y {
364+
break;
365+
} else {
366+
path.push('^'); // (0, 0) is origin
367+
diff.y -=1;
368+
curr.y -=1;
369+
}
370+
}
346371
}
347-
//if curr.y == avoid.y && curr.x == avoid.y {
348-
// panic!("from={}, to={}, diff={}, avoid={}", from_coord, to_coord, diff, avoid);
349-
//}
350-
} else {
351-
path.push('>');
352-
diff.x += 1;
353-
curr.x += 1;
354372
}
355-
}
356373

357-
while diff.y != 0 {
358-
if diff.y < 0 {
359-
if curr.x == avoid.x && (curr.y + 1) == avoid.y {
360-
println!("[Robot::path()] avoid={}, curr={} avoid going down", avoid, curr);
361-
break;
362-
} else {
363-
path.push('v');
364-
diff.y += 1;
365-
curr.y += 1;
374+
PadPosition::DirectionPad(c) => {
375+
while diff.y != 0 {
376+
if diff.y < 0 {
377+
if curr.x == avoid.x && (curr.y + 1) == avoid.y {
378+
println!("[Robot::path()] avoid={}, curr={} avoid going down", avoid, curr);
379+
break;
380+
} else {
381+
path.push('v');
382+
diff.y += 1;
383+
curr.y += 1;
384+
}
385+
} else {
386+
if curr.x == avoid.x && (curr.y - 1) == avoid.y {
387+
break;
388+
} else {
389+
path.push('^'); // (0, 0) is origin
390+
diff.y -=1;
391+
curr.y -=1;
392+
}
393+
}
366394
}
367-
} else {
368-
if curr.x == avoid.x && (curr.y - 1) == avoid.y {
369-
break;
370-
} else {
371-
path.push('^'); // (0, 0) is origin
372-
diff.y -=1;
373-
curr.y -=1;
395+
396+
while diff.x != 0 {
397+
if diff.x > 0 { // go left
398+
if curr.y == avoid.y && (curr.x - 1) == avoid.x {
399+
// TODO for dpad - better to go vv< than <v< for path from A to <
400+
println!("[Robot::path()] avoid going left, avoid={}, curr={}, path={}", avoid, curr, path);
401+
break;
402+
} else {
403+
path.push('<');
404+
diff.x -= 1;
405+
curr.x -= 1;
406+
}
407+
//if curr.y == avoid.y && curr.x == avoid.y {
408+
// panic!("from={}, to={}, diff={}, avoid={}", from_coord, to_coord, diff, avoid);
409+
//}
410+
} else {
411+
path.push('>');
412+
diff.x += 1;
413+
curr.x += 1;
414+
}
374415
}
375-
}
376416

377-
//if diff.y > 0 {
378-
// if curr.x == avoid.x && (curr.y - 1) == avoid.y {
379-
// break;
380-
// } else {
381-
// path.push('^'); // (0, 0) is origin
382-
// diff.y -=1;
383-
// curr.y -=1;
384-
// }
385-
//} else {
386-
// if curr.x == avoid.x && (curr.y + 1) == avoid.y {
387-
// break;
388-
// } else {
389-
// path.push('v');
390-
// diff.y += 1;
391-
// curr.y += 1;
392-
// }
393-
//}
417+
418+
}
394419
}
420+
395421
}
396422

397423
assert_eq!(to_coord, curr);
@@ -488,6 +514,7 @@ mod tests {
488514
}
489515

490516
#[test]
517+
#[allow(non_snake_case)]
491518
fn test_distance_r3_from_A_to_0() -> Result<(), String> {
492519
let conundrum: Conundrum = "".parse()?;
493520

@@ -510,7 +537,8 @@ mod tests {
510537
}
511538

512539
#[test]
513-
fn test_part1_by_steps() -> Result<(), String> {
540+
#[allow(non_snake_case)]
541+
fn test_029A_by_steps() -> Result<(), String> {
514542
/*
515543
* 029A: <vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A
516544
*/
@@ -529,7 +557,8 @@ mod tests {
529557
}
530558

531559
#[test]
532-
fn test_part1_code() -> Result<(), String> {
560+
#[allow(non_snake_case)]
561+
fn test_029A_code() -> Result<(), String> {
533562
let conundrum: Conundrum = "029A".parse()?;
534563

535564
let actual = conundrum.solve_part1();
@@ -539,6 +568,80 @@ mod tests {
539568
Ok(())
540569
}
541570

571+
#[test]
572+
#[allow(non_snake_case)]
573+
fn test_980A_code() -> Result<(), String> {
574+
/*
575+
* 980A: <v<A>>^AAAvA^A<vA<AA>>^AvAA<^A>A<v<A>A>^AAAvA<^A>A<vA>^A<A>A
576+
*/
577+
let conundrum: Conundrum = "980A".parse()?;
578+
579+
let actual = conundrum.solve_part1();
580+
let expected = 60 * 980;
581+
assert_eq!(expected, actual);
582+
583+
Ok(())
584+
}
585+
586+
#[test]
587+
#[allow(non_snake_case)]
588+
fn test_179A_code() -> Result<(), String> {
589+
/*
590+
* Expected:
591+
* 179A: <v<A>>^A<vA<A>>^AAvAA<^A>A<v<A>>^AAvA^A<vA>^AA<A>A<v<A>A>^AAAvA<^A>A
592+
*
593+
* Actual:
594+
* [Robot::path()] avoid going left, avoid=(0, 3), curr=(1, 3), path=<
595+
* [distance_r3] A to 1, r3_path=v<A<AA>>^AvA^<A>Av<A<A>>^AvAA^<A>A, r3_path.len()=34, r2_full_path=v<<A>^Av<A>>^A, r1_path=<^<A
596+
* [distance_r3] 1 to 7, r3_path=v<<A>>^AAvA^A, r3_path.len()=13, r2_full_path=<AA>A, r1_path=^^A
597+
* [distance_r3] 7 to 9, r3_path=v<A^>AA<A>A, r3_path.len()=11, r2_full_path=vAA^A, r1_path=>>A
598+
* [distance_r3] 9 to A, r3_path=v<A<A>>^AAA<Av>A^A, r3_path.len()=18, r2_full_path=v<AAA^>A, r1_path=vvvA
599+
* code=A179A, sum=76, numeric=179, sum*numeric=13604
600+
*
601+
* A1 v<A<AA>>^AvA^<A>Av<A<A>>^AvAA^<A>A
602+
* 17 v<<A>>^AAvA^A
603+
* 79 v<A^>AA<A>A
604+
* 9A v<A<A>>^AAA<Av>A^A
605+
*/
606+
let conundrum: Conundrum = "179A".parse()?;
607+
608+
let actual = conundrum.solve_part1();
609+
let expected = 68 * 179;
610+
assert_eq!(expected, actual);
611+
612+
Ok(())
613+
}
614+
615+
//#[test]
616+
//#[allow(non_snake_case)]
617+
//fn test_456A_code() -> Result<(), String> {
618+
// /*
619+
// * 456A: <v<A>>^AA<vA<A>>^AAvAA<^A>A<vA>^A<A>A<vA>^A<A>A<v<A>A>^AAvA<^A>A
620+
// */
621+
// let conundrum: Conundrum = "456A".parse()?;
622+
//
623+
// let actual = conundrum.solve_part1();
624+
// //let expected =
625+
// assert_eq!(expected, actual);
626+
//
627+
// Ok(())
628+
//}
629+
630+
//#[test]
631+
//#[allow(non_snake_case)]
632+
//fn test_379A_code() -> Result<(), String> {
633+
// /*
634+
// * 379A: <v<A>>^AvA^A<vA<AA>>^AAvA<^A>AAvA^A<vA>^AA<A>A<v<A>A>^AAAvA<^A>A
635+
// */
636+
// let conundrum: Conundrum = "456A".parse()?;
637+
//
638+
// let actual = conundrum.solve_part1();
639+
// //let expected =
640+
// assert_eq!(expected, actual);
641+
//
642+
// Ok(())
643+
//}
644+
542645
#[test]
543646
fn test_part1() -> Result<(), String> {
544647
// TODO: path cannot cross the empty space on the keypad

0 commit comments

Comments
 (0)