day1 part 1
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
/.idea
|
/.idea
|
||||||
|
/input
|
||||||
|
|||||||
18
Cargo.lock
generated
18
Cargo.lock
generated
@@ -5,3 +5,21 @@ version = 4
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "advent-of-rust-2024"
|
name = "advent-of-rust-2024"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"itertools",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "either"
|
||||||
|
version = "1.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|||||||
@@ -4,3 +4,4 @@ version = "0.1.0"
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
itertools = "0.13.0"
|
||||||
|
|||||||
45
src/main.rs
45
src/main.rs
@@ -1,3 +1,46 @@
|
|||||||
|
use itertools::Itertools;
|
||||||
|
use std::fs::read_to_string;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
let input = read_to_string("input/day01.txt").unwrap();
|
||||||
|
let part1 = part1(&input);
|
||||||
|
println!("part1: {}", part1);
|
||||||
|
}
|
||||||
|
|
||||||
|
type Int = i32;
|
||||||
|
|
||||||
|
fn part1(input: &str) -> Int {
|
||||||
|
let (mut l, mut r) = input
|
||||||
|
.trim()
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.split_ascii_whitespace().collect_tuple().unwrap())
|
||||||
|
.map(|(x, y)| (x.parse::<Int>().unwrap(), y.parse::<Int>().unwrap()))
|
||||||
|
.fold((vec![], vec![]), |(mut v1, mut v2), (a, b)| {
|
||||||
|
v1.push(a);
|
||||||
|
v2.push(b);
|
||||||
|
(v1, v2)
|
||||||
|
});
|
||||||
|
|
||||||
|
l.sort();
|
||||||
|
r.sort();
|
||||||
|
|
||||||
|
l.into_iter()
|
||||||
|
.zip(r.into_iter())
|
||||||
|
.map(|(l, r)| (l - r).abs())
|
||||||
|
.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
const TEST_INPUT: &str = r"
|
||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
||||||
|
";
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
assert_eq!(part1(TEST_INPUT), 11);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user