day9: optimized
This commit is contained in:
parent
23967c2689
commit
4a69aeffe5
|
@ -11,31 +11,26 @@ fn process(data: &str) -> i64 {
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut input: Vec<i64> = line
|
let input: Vec<i64> = line.split(' ').flat_map(|x| x.parse::<i64>()).collect();
|
||||||
.split(' ')
|
|
||||||
.filter(|x| !x.is_empty())
|
|
||||||
.map(|x| x.parse::<i64>().unwrap())
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
let mut diffs = vec![input.clone()];
|
let mut diffs = vec![input];
|
||||||
|
|
||||||
|
while let Some(last) = diffs.last() {
|
||||||
|
if !last.iter().any(|&x| x != 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let diff: Vec<i64> = last.windows(2).map(|win| win[1] - win[0]).collect();
|
||||||
|
|
||||||
while input.iter().any(|&x| x != 0) {
|
|
||||||
let diff: Vec<i64> = input.windows(2).map(|win| win[1] - win[0]).collect();
|
|
||||||
input = diff.clone();
|
|
||||||
diffs.push(diff);
|
diffs.push(diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut prev_inserted = 0;
|
let mut prev_inserted = 0;
|
||||||
|
for diff in diffs.drain(..).rev() {
|
||||||
for diff in diffs.iter_mut().rev() {
|
|
||||||
let last = diff[diff.len() - 1];
|
let last = diff[diff.len() - 1];
|
||||||
let num = prev_inserted + last;
|
let num = prev_inserted + last;
|
||||||
diff.push(num);
|
|
||||||
|
|
||||||
prev_inserted = num;
|
prev_inserted = num;
|
||||||
}
|
}
|
||||||
|
answer += prev_inserted;
|
||||||
answer += diffs[0].last().unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
answer
|
answer
|
||||||
|
|
|
@ -12,25 +12,26 @@ fn process(data: &str) -> i64 {
|
||||||
if line.is_empty() {
|
if line.is_empty() {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut input: Vec<i64> = line.split(' ').map(|x| x.parse::<i64>().unwrap()).collect();
|
let input: Vec<i64> = line.split(' ').flat_map(|x| x.parse::<i64>()).collect();
|
||||||
|
|
||||||
let mut diffs = vec![input.clone()];
|
let mut diffs = vec![input];
|
||||||
|
|
||||||
while input.iter().any(|&x| x != 0) {
|
while let Some(last) = diffs.last() {
|
||||||
let diff: Vec<i64> = input.windows(2).map(|win| win[1] - win[0]).collect();
|
if !last.iter().any(|&x| x != 0) {
|
||||||
input = diff.clone();
|
break;
|
||||||
|
}
|
||||||
|
let diff: Vec<i64> = last.windows(2).map(|win| win[1] - win[0]).collect();
|
||||||
diffs.push(diff);
|
diffs.push(diff);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut prev_inserted = 0;
|
let mut prev_inserted = 0;
|
||||||
for diff in diffs.iter_mut().rev() {
|
for diff in diffs.drain(..).rev() {
|
||||||
let last = diff[0];
|
let last = diff[0];
|
||||||
|
|
||||||
let num = last - prev_inserted;
|
let num = last - prev_inserted;
|
||||||
diff.insert(0, num);
|
|
||||||
prev_inserted = num;
|
prev_inserted = num;
|
||||||
}
|
}
|
||||||
answer += diffs[0].first().unwrap();
|
|
||||||
|
answer += prev_inserted;
|
||||||
}
|
}
|
||||||
|
|
||||||
answer
|
answer
|
||||||
|
|
Loading…
Reference in New Issue
Block a user