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