Compare commits

...

3 Commits

Author SHA1 Message Date
Matej Janezic 000000305b
solution: day 1 2024-12-01 22:51:39 +01:00
Matej Janezic 000000208a
feat: add basic parser functions 2024-12-01 22:49:59 +01:00
Matej Janezic 00000010b7
chore: add deps 2024-12-01 22:49:26 +01:00
6 changed files with 197 additions and 1 deletions

108
Cargo.lock generated
View File

@ -17,9 +17,30 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "aoc"
version = "48.0.0"
dependencies = [
"hashbrown 0.15.2",
"itertools",
"num-integer",
"regex",
]
[[package]]
name = "autocfg"
@ -109,6 +130,12 @@ dependencies = [
"reqwest",
]
[[package]]
name = "either"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "encoding_rs"
version = "0.8.31"
@ -118,6 +145,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "fastrand"
version = "1.8.0"
@ -133,6 +166,12 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "foldhash"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f81ec6369c545a7d40e4589b5597581fa1c441fe1cce96dd1de43159910a36a2"
[[package]]
name = "foreign-types"
version = "0.3.2"
@ -236,6 +275,17 @@ version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hashbrown"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
dependencies = [
"allocator-api2",
"equivalent",
"foldhash",
]
[[package]]
name = "hermit-abi"
version = "0.2.6"
@ -333,7 +383,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown",
"hashbrown 0.12.3",
]
[[package]]
@ -351,6 +401,15 @@ version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11b0d96e660696543b251e58030cf9787df56da39dab19ad60eae7353040917e"
[[package]]
name = "itertools"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.5"
@ -437,6 +496,24 @@ dependencies = [
"tempfile",
]
[[package]]
name = "num-integer"
version = "0.1.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f"
dependencies = [
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.15.0"
@ -564,6 +641,35 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex"
version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "remove_dir_all"
version = "0.5.3"

View File

@ -21,4 +21,8 @@ authors.workspace = true
repository.workspace = true
[dependencies]
hashbrown = "0.15.2"
itertools = "0.13.0"
num-integer = "0.1.46"
regex = "1.11.1"

6
data/examples/01.txt Normal file
View File

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

57
src/bin/01.rs Normal file
View File

@ -0,0 +1,57 @@
use hashbrown::HashMap;
use itertools::Itertools;
fn parse_input(input: &str) -> impl Iterator<Item = (isize, isize)> + '_ {
input.lines().map(|l| {
l.split(' ')
.filter_map(|x| x.parse::<isize>().ok())
.collect_tuple()
.unwrap()
})
}
pub fn part_one(input: &str) -> Option<isize> {
let mut left = vec![];
let mut right = vec![];
for (a, b) in parse_input(input) {
left.push(a);
right.push(b);
}
left.sort();
right.sort();
Some(
left.into_iter()
.zip(right)
.map(|(x, y)| (x - y).abs())
.sum(),
)
}
pub fn part_two(input: &str) -> Option<isize> {
let mut map = HashMap::new();
for (a, b) in parse_input(input) {
map.entry(a).or_insert((0, 0)).0 += 1;
map.entry(b).or_insert((0, 0)).1 += 1;
}
Some(map.into_iter().map(|(k, v)| k * v.0 * v.1).sum())
}
aoc::solution!(1);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
assert_eq!(part_one(&aoc::template::read_file("examples", 1)), Some(11));
}
#[test]
fn test_part_two() {
assert_eq!(part_two(&aoc::template::read_file("examples", 1)), Some(31));
}
}

View File

@ -1 +1,4 @@
#![feature(pattern)]
pub mod parsers;
pub mod template;

20
src/parsers.rs Normal file
View File

@ -0,0 +1,20 @@
use std::str::{pattern::Pattern, FromStr};
pub fn to_vec<T, P>(s: &str, pat: P) -> Vec<T>
where
T: FromStr,
P: Pattern,
{
s.split(pat).filter_map(|x| x.parse().ok()).collect()
}
pub fn to_vec_map<T, U, P>(s: &str, pat: P, func: impl FnMut(T) -> U) -> Vec<U>
where
T: FromStr,
P: Pattern,
{
s.split(pat)
.filter_map(|x| x.parse().ok())
.map(func)
.collect()
}