Benjamin Bädorf 2 months ago
parent
commit
6f5f1be338
Signed by: b12f GPG Key ID: 4406E80E13CD656C
  1. 10
      1-alt/aoc01.scala
  2. 2000
      1-alt/input.txt
  3. 6
      1-alt/run.sh
  4. 5
      2/Cargo.lock
  5. 9
      2/Cargo.toml
  6. 1000
      2/input
  7. 44
      2/src/main.rs
  8. 1
      shell.nix

10
1-alt/aoc01.scala

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

File diff suppressed because it is too large

6
1-alt/run.sh

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

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

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

File diff suppressed because it is too large

44
2/src/main.rs

@ -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);
}

1
shell.nix

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

Loading…
Cancel
Save