day1 day2

This commit is contained in:
Frederik Palmø 2023-05-23 23:18:22 +02:00
commit 4f7afcf17d
8 changed files with 5027 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

183
Cargo.lock generated Normal file
View file

@ -0,0 +1,183 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67fc08ce920c31afb70f013dcce1bfc3a3195de6a228474e45e1f145b36f8d04"
dependencies = [
"memchr",
]
[[package]]
name = "aoc-2022"
version = "0.1.0"
dependencies = [
"eyes",
"itertools",
"rayon",
"regex",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "crossbeam-channel"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "46bd5f3f85273295a9d14aedfb86f6aadbff6d8f5295c4a9edb08e819dcf5695"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c063cd8cc95f5c377ed0d4b49a4b21f632396ff690e8470c29b3359b346984b"
dependencies = [
"cfg-if",
]
[[package]]
name = "either"
version = "1.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]]
name = "eyes"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20c46942b268eef9b50b6766c146e8a4903e5e51c4236d321af6c8069ef9a60b"
[[package]]
name = "hermit-abi"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
dependencies = [
"libc",
]
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "libc"
version = "0.2.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "memoffset"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d61c719bcfbcf5d62b3a09efa6088de8c54bc0bfcd3ea7ae39fcc186108b8de1"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "rayon"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d2df5196e37bcc87abebc0053e20787d73847bb33134a69841207dd0a47f03b"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b8f95bd6966f5c87776639160a66bd8ab9895d9d4ab01ddba9fc60661aebe8d"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "regex"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1a59b5d8e97dee33696bf13c5ba8ab85341c002922fba050069326b9c498974"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"

10
Cargo.toml Normal file
View file

@ -0,0 +1,10 @@
[package]
name = "aoc-2022"
version = "0.1.0"
edition = "2021"
[dependencies]
regex = "1"
itertools = "0.10"
rayon = "1"
eyes = "1"

2253
input/day1.txt Normal file

File diff suppressed because it is too large Load diff

2500
input/day2.txt Normal file

File diff suppressed because it is too large Load diff

42
src/bin/day1.rs Normal file
View file

@ -0,0 +1,42 @@
use itertools::Itertools;
fn main() {
let input = std::fs::read_to_string("input/day1.txt").unwrap();
println!("part 1: {}", part1(&input));
println!("part 1: {}", part2(&input));
}
fn part1(input: &str) -> i64 {
input
.split("\n\n") // double newline means new elf inventory
.map(|items| items.lines().map(|item| item.parse::<i64>().unwrap()).sum())
.max()
.unwrap()
}
fn part2(input: &str) -> i64 {
input
.split("\n\n") // double newline means new elf inventory
.map(|items| {
items
.lines()
.map(|item| item.parse::<i64>().unwrap())
.sum::<i64>()
})
.sorted_unstable()
.rev()
.take(3)
.sum::<i64>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn examples() {
let example = "1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000";
assert_eq!(part1(example), 24000);
assert_eq!(part2(example), 45000);
}
}

37
src/bin/day2.rs Normal file
View file

@ -0,0 +1,37 @@
use eyes::{parse, try_parse};
fn main() {
let input = std::fs::read_to_string("input/day2.txt").unwrap();
println!("part 1: {}", part1(&input));
println!("part 1: {}", part2(&input));
}
fn part1(input: &str) -> i64 {
input
.lines()
.map(|line| parse!(line, "{} {}", char, char))
.map(|(them, you)| ((them as u8 - b'A') as i64, (you as u8 - b'X') as i64))
.map(|(them, you)| ((you + 4 - them) % 3 * 3) + (you + 1)) // (outcome score) + (shape score)
.sum::<i64>()
}
fn part2(input: &str) -> i64 {
input
.lines()
.map(|line| parse!(line, "{} {}", char, char))
.map(|(them, you)| ((them as u8 - b'A') as i64, (you as u8 - b'X') as i64))
.map(|(them, you)| (you * 3) + ((you + 2 + them) % 3 + 1)) // (outcome score) + (shape score)
.sum::<i64>()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn examples() {
let example = "A Y\nB X\nC Z";
assert_eq!(part1(example), 15);
assert_eq!(part2(example), 12);
}
}

1
src/lib.rs Normal file
View file

@ -0,0 +1 @@