Add alternate itertools solution to day 1
parent
3e8c8b958e
commit
3d5e96dd93
|
@ -5,3 +5,21 @@ version = 3
|
|||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.10"
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
use itertools::Itertools;
|
||||
use std::collections::VecDeque;
|
||||
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<i32> = None;
|
||||
let mut num_increasing = 0;
|
||||
|
@ -20,14 +23,12 @@ fn part1(items: &[i32]) -> i32 {
|
|||
}
|
||||
|
||||
fn part2(items: &[i32]) -> i32 {
|
||||
const WINDOW_SIZE: usize = 3;
|
||||
|
||||
let mut num_increasing = 0;
|
||||
let mut window = VecDeque::<i32>::new();
|
||||
// Initialize the window with the first few elements
|
||||
window.extend(&items[..WINDOW_SIZE]);
|
||||
window.extend(&items[..PART2_WINDOW_SIZE]);
|
||||
let mut last_window_sum = window.iter().sum::<i32>();
|
||||
for &item in &items[WINDOW_SIZE - 1..] {
|
||||
for &item in &items[PART2_WINDOW_SIZE - 1..] {
|
||||
window.pop_back();
|
||||
window.push_front(item);
|
||||
|
||||
|
@ -42,6 +43,26 @@ fn part2(items: &[i32]) -> i32 {
|
|||
num_increasing
|
||||
}
|
||||
|
||||
fn part1_itertools(items: &[i32]) -> usize {
|
||||
items
|
||||
.iter()
|
||||
.tuple_windows()
|
||||
.filter(|(last, current)| current > last)
|
||||
.count()
|
||||
}
|
||||
|
||||
fn part2_itertools(items: &[i32]) -> usize {
|
||||
items
|
||||
.iter()
|
||||
.tuple_windows()
|
||||
.filter(|(&third_to_last, &second_to_last, &last, ¤t)| {
|
||||
let prev_window = third_to_last + second_to_last + last;
|
||||
let current_window = second_to_last + last + current;
|
||||
current_window > prev_window
|
||||
})
|
||||
.count()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input_file_name = env::args().nth(1).expect("No input filename specified");
|
||||
let input_file = File::open(input_file_name).expect("Could not open input file");
|
||||
|
@ -56,4 +77,7 @@ fn main() {
|
|||
|
||||
println!("Part 1: {}", part1(&items));
|
||||
println!("Part 2: {}", part2(&items));
|
||||
println!("--- alternate solution ---");
|
||||
println!("Part 1: {}", part1_itertools(&items));
|
||||
println!("Part 2: {}", part2_itertools(&items));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue