Skip to content

Instantly share code, notes, and snippets.

@thiagopnts
Last active December 1, 2021 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagopnts/1907157171fb1119419297ec07a4899c to your computer and use it in GitHub Desktop.
Save thiagopnts/1907157171fb1119419297ec07a4899c to your computer and use it in GitHub Desktop.
Advent of Code 2021
// rustc day1-1.rs && ./day1-1 < input.txt
use std::io::{self, BufRead};
fn main() {
println!(
"{}",
io::stdin()
.lock()
.lines()
.take_while(|line| line.is_ok())
.map(|result| result.unwrap().parse::<i32>().unwrap())
.collect::<Vec<_>>()
.as_slice()
.windows(2)
.filter(|win| win[1] > win[0])
.collect::<Vec<_>>()
.len()
);
}
// rustc day1-2.rs && ./day1-2 < input.txt
use std::io::{self, BufRead};
fn main() {
println!(
"{}",
io::stdin()
.lock()
.lines()
.take_while(|line| line.is_ok())
.map(|result| result.unwrap().parse::<i32>().unwrap())
.collect::<Vec<_>>()
.as_slice()
.windows(3)
.map(|win| win[0] + win[1] + win[2])
.collect::<Vec<_>>()
.as_slice()
.windows(2)
.filter(|win| win[1] > win[0])
.collect::<Vec<_>>()
.len()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment