mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
2024: Day 1, part 1
This commit is contained in:
parent
663fc5bc6e
commit
ca118d2856
5 changed files with 1071 additions and 0 deletions
7
2024/Cargo.lock
generated
Normal file
7
2024/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "advent-of-code"
|
||||||
|
version = "0.1.0"
|
6
2024/Cargo.toml
Normal file
6
2024/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "advent-of-code"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
1000
2024/input/day1.txt
Executable file
1000
2024/input/day1.txt
Executable file
File diff suppressed because it is too large
Load diff
30
2024/src/day1.rs
Normal file
30
2024/src/day1.rs
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
pub fn main(input: &Path) {
|
||||||
|
let file = BufReader::new(File::open(input).expect("unable to open input"));
|
||||||
|
|
||||||
|
let mut group1 = Vec::new();
|
||||||
|
let mut group2 = Vec::new();
|
||||||
|
|
||||||
|
for line in file.lines() {
|
||||||
|
let line = line.expect("error reading input line");
|
||||||
|
let mut split = line.split_ascii_whitespace();
|
||||||
|
let mut next = || split.next().unwrap().parse::<i32>().unwrap();
|
||||||
|
group1.push(next());
|
||||||
|
group2.push(next());
|
||||||
|
}
|
||||||
|
|
||||||
|
group1.sort();
|
||||||
|
group2.sort();
|
||||||
|
|
||||||
|
let part1: i32 = group1
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.zip(group2.iter().copied())
|
||||||
|
.map(|(a, b)| (a - b).abs())
|
||||||
|
.sum();
|
||||||
|
|
||||||
|
println!("Part 1: {part1}");
|
||||||
|
}
|
28
2024/src/main.rs
Normal file
28
2024/src/main.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
use std::env;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
mod day1;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let Some(input) = env::args().skip(1).next().map(PathBuf::from) else {
|
||||||
|
eprintln!("Usage advent-of-code dayN.txt");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
let day = input
|
||||||
|
.file_stem()
|
||||||
|
.and_then(|stem| stem.to_str())
|
||||||
|
.and_then(|stem| stem.strip_prefix("day").unwrap_or(stem).parse::<u8>().ok());
|
||||||
|
let Some(day) = day else {
|
||||||
|
eprintln!("Unable to determine day from {}", input.display());
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
match day {
|
||||||
|
1 => day1::main(&input),
|
||||||
|
_ => {
|
||||||
|
eprintln!("Unknown day {day}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue