|
1 | | -use std::{hint::unreachable_unchecked, num::NonZero}; |
| 1 | +use std::num::NonZero; |
2 | 2 |
|
3 | 3 | use aoc_runner_derive::aoc; |
4 | 4 |
|
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() }; |
16 | 7 |
|
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 | + } |
22 | 15 |
|
23 | | - return search(target - last, rest); |
| 16 | + if target % last == 0 { |
| 17 | + if search(target / last, rest) { |
| 18 | + return true; |
24 | 19 | } |
25 | 20 | } |
| 21 | + |
| 22 | + return search(target - last, rest); |
26 | 23 | } |
27 | 24 |
|
28 | 25 | #[aoc(day7, part1)] |
@@ -73,40 +70,37 @@ unsafe fn part1_inner(s: &str) -> u64 { |
73 | 70 | sum |
74 | 71 | } |
75 | 72 |
|
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() }; |
87 | 75 |
|
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 | + } |
93 | 83 |
|
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 | + } |
106 | 89 |
|
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; |
108 | 100 | } |
109 | 101 | } |
| 102 | + |
| 103 | + return search_part2(target - last, rest); |
110 | 104 | } |
111 | 105 |
|
112 | 106 | #[aoc(day7, part2)] |
|
0 commit comments