Day 1
This commit is contained in:
commit
494dc83d00
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
target/
|
||||||
|
tags
|
||||||
|
.direnv
|
5
1/Cargo.lock
generated
Normal file
5
1/Cargo.lock
generated
Normal 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
9
1/Cargo.toml
Normal 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]
|
54
1/src/main.rs
Normal file
54
1/src/main.rs
Normal 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);
|
||||||
|
}
|
Loading…
Reference in a new issue