This commit is contained in:
Benjamin Bädorf 2021-12-02 11:33:23 +01:00
parent 494dc83d00
commit 6f5f1be338
No known key found for this signature in database
GPG key ID: 4406E80E13CD656C
8 changed files with 3075 additions and 0 deletions

10
1-alt/aoc01.scala Normal file
View file

@ -0,0 +1,10 @@
def main(args: Array[String]): Unit = {
val data: Seq[Int] = io.Source.fromFile("input.txt").getLines.filter(_.nonEmpty).map(_.toInt).toSeq
def solution(xs: Seq[Int]): Int = xs.sliding(2, 1).count{case Seq(a, b) => b > a}
val part1 = solution(data)
val part2 = solution(data.sliding(3, 1).map(_.sum).toSeq)
println(s"Part 1: $part1")
println(s"Part 2: $part2")
}

2000
1-alt/input.txt Normal file

File diff suppressed because it is too large Load diff

6
1-alt/run.sh Executable file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
nix-shell \
-p scala curl \
--run \
'scala -e "$(curl https://gist.githubusercontent.com/machisuji/7bca405fac2612c5a9575c056204f773/raw/b7556120bfadd9faf2b2a91ce1907df9fca2cf66/aoc01.scala); main(Array[String]());"'

5
2/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
2/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]

1000
2/input Normal file

File diff suppressed because it is too large Load diff

44
2/src/main.rs Normal file
View file

@ -0,0 +1,44 @@
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 position = contents
.lines()
.map(|s| {
let c: Vec<&str> = s.split(" ").collect();
let num = c[1].parse::<usize>().unwrap();
return (c[0].to_string(), num);
})
.fold((0, 0), |acc, command| match command.0.as_str() {
"forward" => (acc.0 + command.1, acc.1),
"up" => (acc.0, acc.1 - command.1),
"down" => (acc.0, acc.1 + command.1),
_ => acc,
});
println!("{}", position.0 * position.1);
}
fn part_2() {
let contents = fs::read_to_string("./input").expect("Couldn't read file!");
let position = contents
.lines()
.map(|s| {
let c: Vec<&str> = s.split(" ").collect();
let num = c[1].parse::<usize>().unwrap();
return (c[0].to_string(), num);
})
.fold((0, 0, 0), |acc, command| match command.0.as_str() {
"forward" => (acc.0 + command.1, acc.1, acc.2 + acc.1 * command.1),
"up" => (acc.0, acc.1 - command.1, acc.2),
"down" => (acc.0, acc.1 + command.1, acc.2),
_ => acc,
});
println!("{}", position.0 * position.2);
}

View file

@ -3,5 +3,6 @@
pkgs.mkShell {
buildInputs = [
pkgs.rustup
pkgs.scala
];
}