add 2022 day1 (p1 and 2) and 2024 day1 (p1 and 2)

This commit is contained in:
2024-12-01 02:00:22 -05:00
parent 5b7a6edbfb
commit 4f75e45108
17 changed files with 6752 additions and 0 deletions

16
2022/day1p2/Cargo.lock generated Normal file
View File

@@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1p2"
version = "0.1.0"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.167"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"

7
2022/day1p2/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "day1p2"
version = "0.1.0"
edition = "2021"
[dependencies]
libc = "0.2.167"

2236
2022/day1p2/src/input.txt Normal file

File diff suppressed because it is too large Load Diff

45
2022/day1p2/src/main.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::io::{stdin, Read};
use libc::{clock_gettime, timespec, CLOCK_PROCESS_CPUTIME_ID};
fn main() {
let mut data = String::new();
stdin().read_to_string(&mut data).unwrap();
let mut begin = timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut begin);
};
let calories = data.replace('\n', " ");
let lines = calories.split(" ");
let mut all_cals = vec![];
for line in lines {
let mut cal_count = vec![];
let nums = line.split_whitespace();
for num in nums {
let num: i32 = num.trim().parse().unwrap();
cal_count.push(num);
}
let total_sum: i32 = cal_count.iter().sum();
all_cals.push(total_sum);
}
all_cals.sort_unstable();
all_cals.reverse();
let sum = all_cals[0] + all_cals[1] + all_cals[2];
let mut end = timespec {
tv_sec: 0,
tv_nsec: 0,
};
unsafe {
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut end);
};
println!("{sum:?}");
println!(
"time: {:.6}ms",
(end.tv_nsec - begin.tv_nsec) as f32 / 1000.0 / 1000.0
);
}