File tree Expand file tree Collapse file tree 1 file changed +9
-13
lines changed
Expand file tree Collapse file tree 1 file changed +9
-13
lines changed Original file line number Diff line number Diff line change 1- use std:: num:: NonZero ;
1+ use std:: { hint :: unreachable_unchecked , num:: NonZero } ;
22
33use aoc_runner_derive:: aoc;
44
55fn search ( target : u64 , v : & [ NonZero < u64 > ] ) -> bool {
66 match v {
7- [ ] => {
8- return target == 0 ;
9- }
7+ [ ] => unsafe { unreachable_unchecked ( ) } ,
108 [ rest @ .., last] => {
119 let last = last. get ( ) ;
10+ if last > target {
11+ return rest. is_empty ( ) ;
12+ }
1213 if target % last == 0 {
1314 if search ( target / last, rest) {
1415 return true ;
1516 }
1617 }
17- if last > target {
18- return false ;
19- }
2018
2119 return search ( target - last, rest) ;
2220 }
@@ -73,19 +71,17 @@ unsafe fn part1_inner(s: &str) -> u64 {
7371
7472fn search_part2 ( target : u64 , v : & [ NonZero < u64 > ] ) -> bool {
7573 match v {
76- [ ] => {
77- return target == 0 ;
78- }
74+ [ ] => unsafe { unreachable_unchecked ( ) } ,
7975 [ rest @ .., last] => {
8076 let last = last. get ( ) ;
77+ if last >= target {
78+ return rest. is_empty ( ) ;
79+ }
8180 if target % last == 0 {
8281 if search_part2 ( target / last, rest) {
8382 return true ;
8483 }
8584 }
86- if last > target {
87- return false ;
88- }
8985
9086 let size = unsafe { NonZero :: new_unchecked ( 10u64 . pow ( last. ilog10 ( ) + 1 ) ) } ;
9187 if ( target - last) % size == 0 {
You can’t perform that action at this time.
0 commit comments