This commit is contained in:
cool-mist 2024-12-03 14:24:50 +05:30
commit 8ef11b2293
6 changed files with 1101 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
proof
# Added by cargo
/target

7
Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc_24"
version = "0.1.0"

4
Cargo.toml Normal file
View File

@ -0,0 +1,4 @@
[package]
name = "aoc_24"
version = "0.1.0"
edition = "2021"

1000
src/day1/input Normal file

File diff suppressed because it is too large Load Diff

63
src/day1/mod.rs Normal file
View File

@ -0,0 +1,63 @@
use std::collections::HashSet;
pub struct Day1 {
pub input: String,
}
impl Day1 {
pub fn new() -> Day1 {
let input = include_bytes!("input");
let input = String::from_utf8_lossy(input);
Day1 {
input: input.to_string(),
}
}
pub fn part_one(&self) {
let mut left: Vec<i32> = Vec::new();
let mut right: Vec<i32> = Vec::new();
self.input.lines().for_each(|line| {
let mut parts = line.split_whitespace();
left.push(parts.next().unwrap().parse().unwrap());
right.push(parts.next().unwrap().parse().unwrap());
});
left.sort();
right.sort();
let res = left
.iter()
.zip(right.iter())
.map(|(l, r)| Day1::abs(*l, *r))
.sum::<i32>();
println!("{}", res);
}
fn abs(a: i32, b: i32) -> i32 {
if a > b {
a - b
} else {
b - a
}
}
pub fn part_two(&self) {
let mut left: HashSet<i32> = HashSet::new();
let mut right: Vec<i32> = Vec::new();
self.input.lines().for_each(|line| {
let mut parts = line.split_whitespace();
left.insert(parts.next().unwrap().parse().unwrap());
right.push(parts.next().unwrap().parse().unwrap());
});
let mut acc = 0;
for r in right.iter() {
if left.contains(r) {
acc = acc + *r;
}
}
println!("{}", acc);
}
}

21
src/main.rs Normal file
View File

@ -0,0 +1,21 @@
use day1::Day1;
mod day1;
fn main() {
let args = std::env::args().collect::<Vec<String>>();
if args.len() != 2 {
println!("Usage: {} {{day}}.{{problem}}", args[0]);
println!("Example: {} 1.1", args[0]);
std::process::exit(1);
}
let problem = args[1].as_str();
match problem {
"1.1" => Day1::new().part_one(),
"1.2" => Day1::new().part_two(),
_ => {
println!("Bad input: {}", problem);
std::process::exit(1);
}
}
}