Last active
December 1, 2021 20:03
-
-
Save thiagopnts/1907157171fb1119419297ec07a4899c to your computer and use it in GitHub Desktop.
Advent of Code 2021
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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