From a84c3e49f5193560178d35b7b79cef4f5823f190 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Tue, 15 Dec 2020 22:41:56 +0100 Subject: [PATCH] Rust: small optimization for day9 part1 --- rust/src/main.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/rust/src/main.rs b/rust/src/main.rs index 726019e..76276e2 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -381,19 +381,16 @@ 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; - } + 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;