Skip to content

Commit 69c64cd

Browse files
committed
Use split_last
1 parent 89375c2 commit 69c64cd

File tree

1 file changed

+41
-47
lines changed

1 file changed

+41
-47
lines changed

src/day7.rs

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
use std::{hint::unreachable_unchecked, num::NonZero};
1+
use std::num::NonZero;
22

33
use aoc_runner_derive::aoc;
44

5-
fn search(target: u64, v: &[NonZero<u64>]) -> bool {
6-
match v {
7-
[] => unsafe { unreachable_unchecked() },
8-
[rest @ .., last] => {
9-
let last = last.get();
10-
if rest.is_empty() {
11-
return target == last;
12-
}
13-
if last > target {
14-
return false;
15-
}
5+
pub fn search(target: u64, v: &[NonZero<u64>]) -> bool {
6+
let (last, rest) = unsafe { v.split_last().unwrap_unchecked() };
167

17-
if target % last == 0 {
18-
if search(target / last, rest) {
19-
return true;
20-
}
21-
}
8+
let last = last.get();
9+
if rest.is_empty() {
10+
return target == last;
11+
}
12+
if last > target {
13+
return false;
14+
}
2215

23-
return search(target - last, rest);
16+
if target % last == 0 {
17+
if search(target / last, rest) {
18+
return true;
2419
}
2520
}
21+
22+
return search(target - last, rest);
2623
}
2724

2825
#[aoc(day7, part1)]
@@ -73,40 +70,37 @@ unsafe fn part1_inner(s: &str) -> u64 {
7370
sum
7471
}
7572

76-
fn search_part2(target: u64, v: &[NonZero<u64>]) -> bool {
77-
match v {
78-
[] => unsafe { unreachable_unchecked() },
79-
[rest @ .., last] => {
80-
let last = last.get();
81-
if rest.is_empty() {
82-
return target == last;
83-
}
84-
if last > target {
85-
return false;
86-
}
73+
pub fn search_part2(target: u64, v: &[NonZero<u64>]) -> bool {
74+
let (last, rest) = unsafe { v.split_last().unwrap_unchecked() };
8775

88-
if target % last == 0 {
89-
if search_part2(target / last, rest) {
90-
return true;
91-
}
92-
}
76+
let last = last.get();
77+
if rest.is_empty() {
78+
return target == last;
79+
}
80+
if last > target {
81+
return false;
82+
}
9383

94-
let size = if last >= 100 {
95-
1000
96-
} else if last >= 10 {
97-
100
98-
} else {
99-
10
100-
};
101-
if (target - last) % size == 0 {
102-
if search_part2((target - last) / size, rest) {
103-
return true;
104-
}
105-
}
84+
if target % last == 0 {
85+
if search_part2(target / last, rest) {
86+
return true;
87+
}
88+
}
10689

107-
return search_part2(target - last, rest);
90+
let size = if last >= 100 {
91+
1000
92+
} else if last >= 10 {
93+
100
94+
} else {
95+
10
96+
};
97+
if (target - last) % size == 0 {
98+
if search_part2((target - last) / size, rest) {
99+
return true;
108100
}
109101
}
102+
103+
return search_part2(target - last, rest);
110104
}
111105

112106
#[aoc(day7, part2)]

0 commit comments

Comments
 (0)