From 0f2875c9ae0fe897a1394755fa9b1d5dc575aecd Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Wed, 1 Dec 2021 00:58:05 -0500 Subject: [PATCH] Minor cleanup to day 1 part 1 --- day1/src/main.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/day1/src/main.rs b/day1/src/main.rs index 99eb70f..31574e1 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -4,8 +4,6 @@ use std::env; use std::fs::File; use std::io::{BufRead, BufReader}; -const PART2_WINDOW_SIZE: usize = 3; - fn part1(items: &[i32]) -> i32 { let mut prev: Option = None; let mut num_increasing = 0; @@ -23,12 +21,16 @@ fn part1(items: &[i32]) -> i32 { } fn part2(items: &[i32]) -> i32 { + const WINDOW_SIZE: usize = 3; + let mut num_increasing = 0; let mut window = VecDeque::::new(); // Initialize the window with the first few elements - window.extend(&items[..PART2_WINDOW_SIZE]); + window.extend(&items[..WINDOW_SIZE]); let mut last_window_sum = window.iter().sum::(); - for &item in &items[PART2_WINDOW_SIZE - 1..] { + for &item in &items[WINDOW_SIZE - 1..] { + // Ensure there are always only WINDOW_SIZE elements in the vec + // by popping first we won't realloc but this is AoC so that doesn't really matter window.pop_back(); window.push_front(item);