Skip to content

Commit afcdd61

Browse files
committed
Revert "Use split_last"
This reverts commit 69c64cd.
1 parent 69c64cd commit afcdd61

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

src/day7.rs

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

33
use aoc_runner_derive::aoc;
44

5-
pub fn search(target: u64, v: &[NonZero<u64>]) -> bool {
6-
let (last, rest) = unsafe { v.split_last().unwrap_unchecked() };
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+
}
716

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

16-
if target % last == 0 {
17-
if search(target / last, rest) {
18-
return true;
23+
return search(target - last, rest);
1924
}
2025
}
21-
22-
return search(target - last, rest);
2326
}
2427

2528
#[aoc(day7, part1)]
@@ -70,37 +73,40 @@ unsafe fn part1_inner(s: &str) -> u64 {
7073
sum
7174
}
7275

73-
pub fn search_part2(target: u64, v: &[NonZero<u64>]) -> bool {
74-
let (last, rest) = unsafe { v.split_last().unwrap_unchecked() };
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+
}
7587

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

84-
if target % last == 0 {
85-
if search_part2(target / last, rest) {
86-
return true;
87-
}
88-
}
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+
}
89106

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;
107+
return search_part2(target - last, rest);
100108
}
101109
}
102-
103-
return search_part2(target - last, rest);
104110
}
105111

106112
#[aoc(day7, part2)]

0 commit comments

Comments
 (0)