This commit is contained in:
Stefan Schwarz 2021-12-04 01:34:01 +01:00
commit 1e95ecc760
8 changed files with 3206 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

86
Cargo.lock generated Normal file
View file

@ -0,0 +1,86 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "anyhow"
version = "1.0.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203"
[[package]]
name = "aoc2021"
version = "0.1.0"
dependencies = [
"anyhow",
"itertools",
"thiserror",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "itertools"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
dependencies = [
"either",
]
[[package]]
name = "proc-macro2"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"

20
Cargo.toml Normal file
View file

@ -0,0 +1,20 @@
[package]
name = "aoc2021"
version = "0.1.0"
authors = ["Stefan Schwarz <stefan@f2o.io>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0"
itertools = "0.10"
thiserror = "1.0"
[[bin]]
name = "1"
path = "src/01.rs"
[[bin]]
name = "2"
path = "src/02.rs"

39
src/01.rs Normal file
View file

@ -0,0 +1,39 @@
static INPUT: &str = include_str!("01.txt");
fn main() {
let mut counter = 0;
INPUT.lines().fold(None, |last, line| {
let cur: u32 = line.parse().expect("line must be numeric");
if let Some(last) = last {
if cur > last {
counter += 1;
}
}
Some(cur)
});
println!("increased {} times", counter);
let mut counter = 0;
let splitted: Vec<u32> = INPUT
.lines()
.map(|line| line.parse().expect("line must be a number"))
.collect();
splitted
.windows(3)
.map(|win| {
if let [a, b, c] = win {
a + b + c
} else {
unreachable!("line must be a number")
}
})
.fold(None, |last, cur| {
if let Some(last) = last {
if cur > last {
counter += 1;
}
}
Some(cur)
});
println!("increased {} times", counter);
}

2000
src/01.txt Normal file

File diff suppressed because it is too large Load diff

57
src/02.rs Normal file
View file

@ -0,0 +1,57 @@
static INPUT: &str = include_str!("02.txt");
fn main() {
// part 1
let mut pos: i32 = 0;
let mut depth: i32 = 0;
INPUT
.lines()
.map(|line| {
let mut line = line.splitn(2, " ");
let direction = line.next().expect("no direction set");
let delta: i32 = line
.next()
.expect("no delta set")
.parse()
.expect("delta must be a number");
match direction {
"forward" => pos += delta,
"down" => depth += delta,
"up" => depth -= delta,
_ => unimplemented!("unknown direction instruction"),
}
})
.count();
println!("---");
println!("pos: {}, depth: {}", pos, depth);
println!("result: {}", pos * depth);
// part 2
let mut pos: i32 = 0;
let mut depth: i32 = 0;
let mut aim: i32 = 0;
INPUT
.lines()
.map(|line| {
let mut line = line.splitn(2, " ");
let direction = line.next().expect("no direction set");
let delta: i32 = line
.next()
.expect("no delta set")
.parse()
.expect("delta must be a number");
match direction {
"forward" => {
pos += delta;
depth += aim * delta;
}
"down" => aim += delta,
"up" => aim -= delta,
_ => unimplemented!("unknown direction instruction"),
}
})
.count();
println!("---");
println!("pos: {}, depth: {}", pos, depth);
println!("result: {}", pos * depth)
}

1000
src/02.txt Normal file

File diff suppressed because it is too large Load diff

3
src/main.rs Normal file
View file

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}