First day of 2021, done in Rust

This commit is contained in:
Teo-CD 2021-12-03 23:59:40 +00:00
parent 8fe41ecd3b
commit 4f147f4f7a
6 changed files with 2060 additions and 1 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
**/.idea
**/target

2000
2021/Day 1/input.txt Normal file

File diff suppressed because it is too large Load diff

5
2021/Day 1/rust_solution/Cargo.lock generated Normal file
View file

@ -0,0 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "rust_solution"
version = "0.1.0"

View file

@ -0,0 +1,9 @@
[package]
name = "rust_solution"
version = "0.1.0"
authors = ["Teo-CD"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -0,0 +1,38 @@
use std::fs;
fn first_puzzle(input: &String) {
// Go through each line and subtract it to the previous one, keeping only the positive results
// and counting them, knowing that the first diff is invalid.
let count = input.lines().scan(0, |current, line| {
let previous = *current;
*current = line.parse::<i32>().unwrap();
Some(*current - previous)}).filter(|diff| diff.is_positive()).count() - 1;
println!("Count of single increases : {}", count);
}
fn second_puzzle(input: &String) {
let count = input.lines().scan([0; 4], |sliding_window: &mut [i32; 4], line|{
let place = sliding_window[3];
sliding_window[(place % 3) as usize] = line.parse::<i32>().unwrap();
sliding_window[3] = place + 1;
if sliding_window[3] < 3 {
Some(-1)
} else {
Some(sliding_window[0..3].iter().sum::<i32>())
}
}).filter(|sum| !sum.is_negative()).scan(0, |current, sum| {
let previous = *current;
*current = sum;
Some(*current - previous)}).filter(|diff| diff.is_positive()).count() - 1;
println!("Count of triple sums increasing : {}", count);
}
fn main() {
let contents = fs::read_to_string("../input.txt").expect("Could not open file.");
first_puzzle(&contents);
second_puzzle(&contents);
0;
}

View file

@ -5,4 +5,9 @@ The directory structure is simple : `year/day`, each day containing the input fi
## Depencies
Only Python3 for now !
2020:
- Python3
2021:
- Rust (Cargo)