Skip to content

Commit a3abfee

Browse files
committed
Safety? whats that
1 parent 31aeae2 commit a3abfee

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

src/day7.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
1-
use std::num::NonZero;
1+
use std::{hint::unreachable_unchecked, num::NonZero};
22

33
use aoc_runner_derive::aoc;
44

55
fn 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

7472
fn 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 {

0 commit comments

Comments
 (0)