Rust: small optimization for day9 part1

This commit is contained in:
Sijmen 2020-12-15 22:41:56 +01:00
parent e13d0225bc
commit a84c3e49f5
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
1 changed files with 5 additions and 7 deletions

View File

@ -381,20 +381,17 @@ mod day09 {
use std::time::{Duration, Instant};
fn part1(numbers: &[usize]) -> usize {
for (i, &n) in numbers.iter().enumerate().skip(COUNT) {
let mut found = false;
'outer: for (i, &n) in numbers.iter().enumerate().skip(COUNT) {
for x in i - COUNT..i {
for y in x + 1..i {
if numbers[x] + numbers[y] == n {
found = true;
continue 'outer;
}
}
}
if !found {
return n;
}
}
panic!();
}
@ -416,7 +413,8 @@ mod day09 {
}
}
numbers[lower..upper].iter().min().unwrap() + numbers[lower..upper].iter().max().unwrap()
let slice = &numbers[lower..upper];
slice.iter().min().unwrap() + slice.iter().max().unwrap()
}
const COUNT: usize = 25;