-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday7.rs
194 lines (186 loc) · 6.75 KB
/
day7.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#![allow(clippy::enum_variant_names)]
use crate::Input;
use str_block::str_block;
pub fn inputs() -> Vec<Input<u64>> {
vec![
Input::Hashed("30e1c6a2e8425ba49f7194d019dde4f9179e6994c68681ae0c48af82c26c29b4"),
Input::Inline(
"example",
str_block! {"
190: 10 19
3267: 81 40 27
83: 17 5
156: 15 6
7290: 6 8 6 15
161011: 16 10 13
192: 17 8 14
21037: 9 7 18 13
292: 11 6 16 20
"},
Some(3749),
Some(11387),
),
]
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Op {
MulTo(u64),
AddTo(u64),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Op2 {
MulTo(u64),
AddTo(u64),
CatTo(u64),
}
fn int(input: &mut &[u8]) -> Option<u64> {
if let Some(b) = input.first() {
*input = &input[1..];
let mut v = b.wrapping_sub(b'0') as u64;
while let Some(b) = input.first() {
*input = &input[1..];
let digit = b.wrapping_sub(b'0');
if digit > 9 {
break;
}
v = v * 10 + digit as u64;
}
return Some(v);
}
None
}
fn parse_line(mut input: &[u8], out: &mut Vec<u32>) -> Option<u64> {
out.clear();
int(&mut input).inspect(|_| {
input = &input[1..];
while let Some(int) = int(&mut input) {
out.push(int as u32);
}
})
}
pub fn part1(input: &str) -> u64 {
let mut nums = Vec::with_capacity(12);
let mut ops = Vec::with_capacity(12);
let mut total = 0;
'lines: for line in input.as_bytes().trim_ascii_end().split(|&b| b == b'\n') {
if let Some(target) = parse_line(line, &mut nums) {
let mut subtarget = target;
ops.clear();
'sums: while ops.len() + 1 < nums.len() {
let mut full = ops.len() + 2 == nums.len();
let num = nums[nums.len() - ops.len() - 1] as u64;
let pop = if subtarget >= num {
if subtarget % num == 0 {
ops.push(Op::MulTo(subtarget));
subtarget /= num;
} else {
ops.push(Op::AddTo(subtarget));
subtarget -= num;
}
full && subtarget != nums[0] as u64
} else {
full = false;
true
};
if pop {
while let Some(pop) = ops.pop() {
match pop {
Op::MulTo(t) => {
subtarget = t;
let num = nums[nums.len() - ops.len() - 1] as u64;
if subtarget >= num {
ops.push(Op::AddTo(subtarget));
subtarget -= num;
if !(full && subtarget != nums[0] as u64) {
continue 'sums;
}
} else {
full = false;
}
}
Op::AddTo(_) => full = false,
}
}
continue 'lines;
}
}
total += target;
}
}
total
}
pub fn part2(input: &str) -> u64 {
let mut nums = Vec::with_capacity(12);
let mut ops = Vec::with_capacity(12);
let mut total = 0;
'lines: for line in input.as_bytes().trim_ascii_end().split(|&b| b == b'\n') {
if let Some(target) = parse_line(line, &mut nums) {
let mut subtarget = target;
ops.clear();
'sums: while ops.len() + 1 < nums.len() {
let mut full = ops.len() + 2 == nums.len();
let num = nums[nums.len() - ops.len() - 1] as u64;
let ndig = num.ilog10() + 1;
let pow = 10_u64.pow(ndig);
let pop = if subtarget % pow == num {
ops.push(Op2::CatTo(subtarget));
subtarget /= pow;
full && subtarget != nums[0] as u64
} else if subtarget >= num {
if subtarget % num == 0 {
ops.push(Op2::MulTo(subtarget));
subtarget /= num;
} else {
ops.push(Op2::AddTo(subtarget));
subtarget -= num;
}
full && subtarget != nums[0] as u64
} else {
full = false;
true
};
if pop {
while let Some(pop) = ops.pop() {
match pop {
Op2::CatTo(t) => {
subtarget = t;
let num = nums[nums.len() - ops.len() - 1] as u64;
if subtarget >= num {
if subtarget % num == 0 {
ops.push(Op2::MulTo(subtarget));
subtarget /= num;
} else {
ops.push(Op2::AddTo(subtarget));
subtarget -= num;
}
if !(full && subtarget != nums[0] as u64) {
continue 'sums;
}
} else {
full = false;
}
}
Op2::MulTo(t) => {
subtarget = t;
let num = nums[nums.len() - ops.len() - 1] as u64;
if subtarget >= num {
ops.push(Op2::AddTo(subtarget));
subtarget -= num;
if !(full && subtarget != nums[0] as u64) {
continue 'sums;
}
} else {
full = false;
}
}
Op2::AddTo(_) => full = false,
}
}
continue 'lines;
}
}
total += target;
}
}
total
}