Benjamin Bädorf 2 months ago
commit
494dc83d00
Signed by: b12f GPG Key ID: 4406E80E13CD656C
  1. 2
      .envrc
  2. 3
      .gitignore
  3. 5
      1/Cargo.lock
  4. 9
      1/Cargo.toml
  5. 2000
      1/input
  6. 54
      1/src/main.rs
  7. 7
      shell.nix

2
.envrc

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

3
.gitignore

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

5
1/Cargo.lock

@ -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

@ -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

File diff suppressed because it is too large

54
1/src/main.rs

@ -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

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