mirror of
https://codeberg.org/JasterV/aoc_2021.git
synced 2026-04-26 18:40:05 +00:00
first commit
This commit is contained in:
commit
0a924a34a3
6 changed files with 2082 additions and 0 deletions
5
README.md
Normal file
5
README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Advent of code 2021
|
||||
|
||||
This repo contains my solutions for advent of code 2021 :)
|
||||
|
||||
I will solve all of the problems with Rust, trying to take a functional approach for the problems
|
||||
1
day1/.gitignore
vendored
Normal file
1
day1/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
7
day1/Cargo.lock
generated
Normal file
7
day1/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
8
day1/Cargo.toml
Normal file
8
day1/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
2000
day1/input.txt
Normal file
2000
day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
61
day1/src/main.rs
Normal file
61
day1/src/main.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::io::{BufReader, Lines, Result};
|
||||
use std::iter::Iterator;
|
||||
use std::path::Path;
|
||||
|
||||
const INPUT_PATH: &'static str = "./input.txt";
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let depths = read_depths(INPUT_PATH)?;
|
||||
// First puzzle
|
||||
let increases = count_increases(&depths);
|
||||
println!("First puzzle {0}", increases);
|
||||
// Second puzzle
|
||||
let sliding_window_sums = calculate_sliding_window_sums(&depths, 3);
|
||||
let increases = count_increases(&sliding_window_sums);
|
||||
println!("Second puzzle {0}", increases);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn calculate_sliding_window_sums(depths: &[i32], size: usize) -> Vec<i32> {
|
||||
let mut sums = vec![];
|
||||
for i in 0..=depths.len() - size {
|
||||
let window = &depths[i..i + size];
|
||||
let sum = window.into_iter().fold(0, |a, &b| a + b);
|
||||
sums.push(sum)
|
||||
}
|
||||
sums
|
||||
}
|
||||
|
||||
fn count_increases(nums: &[i32]) -> i32 {
|
||||
// Map an iterator of nums to [last_num, counter]
|
||||
let [_, counter] = nums.iter().fold([0, -1], |[prev, counter], &curr| {
|
||||
[curr, counter + if curr > prev { 1 } else { 0 }]
|
||||
});
|
||||
counter
|
||||
}
|
||||
|
||||
fn read_depths(file_path: &str) -> Result<Vec<i32>> {
|
||||
read_lines(file_path).map(lines_to_depths)
|
||||
}
|
||||
|
||||
fn lines_to_depths(lines: Lines<BufReader<File>>) -> Vec<i32> {
|
||||
lines
|
||||
.filter_map(|line| line.ok().map(line_to_depth))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn line_to_depth(line: String) -> i32 {
|
||||
line.parse::<i32>().unwrap()
|
||||
}
|
||||
|
||||
// The output is wrapped in a Result to allow matching on errors
|
||||
// Returns an Iterator to the Reader of the lines of the file.
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
Loading…
Reference in a new issue