This commit is contained in:
Benjamin Bädorf 2021-12-01 13:07:40 +01:00
commit 494dc83d00
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
7 changed files with 2080 additions and 0 deletions

2
.envrc Normal file
View file

@ -0,0 +1,2 @@
watch_file shell.nix
use nix shell.nix

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
target/
tags
.direnv

5
1/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 = "aoc-2020-1"
version = "0.1.0"

9
1/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "aoc-2020-1"
version = "0.1.0"
authors = ["Benjamin Bädorf <hello@benjaminbaedorf.eu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2000
1/input Normal file

File diff suppressed because it is too large Load diff

54
1/src/main.rs Normal file
View file

@ -0,0 +1,54 @@
use std::fs;
fn main() {
// part_1();
part_2();
}
fn part_1() {
let contents = fs::read_to_string("./input").expect("Couldn't read file!");
let numbers: Vec<usize> = contents
.lines()
.map(|s| s.parse::<usize>().unwrap())
.collect();
let initial_condition: (usize, usize) = (0, 1000000);
let increases = numbers.iter().fold(initial_condition, |acc, num| {
if num > &acc.1 {
(acc.0 + 1, *num)
} else {
(acc.0, *num)
}
});
println!("{}", increases.0);
}
fn part_2() {
let contents = fs::read_to_string("./input").expect("Couldn't read file!");
let numbers: Vec<usize> = contents
.lines()
.map(|s| s.parse::<usize>().unwrap())
.collect();
// inc n - 3 n - 2 n - 1
let initial_condition: (usize, (usize, usize, usize)) = (0, (0, 0, 0));
let increases = numbers.iter().fold(initial_condition, |acc, num| {
let new_previous_numbers = (acc.1 .1, acc.1 .2, *num);
if acc.1 .0 == 0 {
return (0, new_previous_numbers);
}
let previous_sum = acc.1 .0 + acc.1 .1 + acc.1 .2;
let current_sum = acc.1 .1 + acc.1 .2 + num;
if current_sum > previous_sum {
(acc.0 + 1, new_previous_numbers)
} else {
(acc.0, new_previous_numbers)
}
});
println!("{}", increases.0);
}

7
shell.nix Normal file
View file

@ -0,0 +1,7 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
buildInputs = [
pkgs.rustup
];
}