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

View file

@ -381,19 +381,16 @@ mod day09 {
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
fn part1(numbers: &[usize]) -> usize { fn part1(numbers: &[usize]) -> usize {
for (i, &n) in numbers.iter().enumerate().skip(COUNT) { 'outer: for (i, &n) in numbers.iter().enumerate().skip(COUNT) {
let mut found = false;
for x in i - COUNT..i { for x in i - COUNT..i {
for y in x + 1..i { for y in x + 1..i {
if numbers[x] + numbers[y] == n { if numbers[x] + numbers[y] == n {
found = true; continue 'outer;
} }
} }
} }
if !found { return n;
return n;
}
} }
panic!(); 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; const COUNT: usize = 25;