forked from danmax/aoc
add day 1 and 2
This commit is contained in:
1
2023/day1p2/.gitignore
vendored
Normal file
1
2023/day1p2/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
16
2023/day1p2/Cargo.lock
generated
Normal file
16
2023/day1p2/Cargo.lock
generated
Normal 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.164"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "433bfe06b8c75da9b2e3fbea6e5329ff87748f0b144ef75306e674c3f6f7c13f"
|
7
2023/day1p2/Cargo.toml
Normal file
7
2023/day1p2/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "day1p2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2.164"
|
1000
2023/day1p2/src/input.txt
Normal file
1000
2023/day1p2/src/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
70
2023/day1p2/src/main.rs
Normal file
70
2023/day1p2/src/main.rs
Normal file
@@ -0,0 +1,70 @@
|
||||
use std::io::{self, Read};
|
||||
use libc::{clock_gettime, timespec, CLOCK_PROCESS_CPUTIME_ID};
|
||||
|
||||
fn main() {
|
||||
let mut sum = 0;
|
||||
let mut data = String::new();
|
||||
io::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);
|
||||
};
|
||||
for line in data.lines() {
|
||||
let mut vec = Vec::new();
|
||||
let replacements = [
|
||||
("1", 1),
|
||||
("2", 2),
|
||||
("3", 3),
|
||||
("4", 4),
|
||||
("5", 5),
|
||||
("6", 6),
|
||||
("7", 7),
|
||||
("8", 8),
|
||||
("9", 9),
|
||||
("one", 1),
|
||||
("two", 2),
|
||||
("three", 3),
|
||||
("four", 4),
|
||||
("five", 5),
|
||||
("six", 6),
|
||||
("seven", 7),
|
||||
("eight", 8),
|
||||
("nine", 9),
|
||||
];
|
||||
|
||||
let serror = line.to_string();
|
||||
|
||||
for i in 0..serror.len() {
|
||||
for &(word, num) in &replacements {
|
||||
let mut end = i + word.len();
|
||||
if end > serror.len() {
|
||||
end = serror.len();
|
||||
}
|
||||
let fisht = &serror[i..end];
|
||||
if fisht == word {
|
||||
vec.push(num);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut sussy = vec.first().unwrap().to_owned();
|
||||
sussy = sussy * 10 + vec.last().unwrap().to_owned();
|
||||
sum += sussy;
|
||||
}
|
||||
|
||||
println!("{sum:?}");
|
||||
let mut end = timespec {
|
||||
tv_sec: 0,
|
||||
tv_nsec: 0,
|
||||
};
|
||||
unsafe {
|
||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut end);
|
||||
};
|
||||
println!(
|
||||
"time: {:.6}ms",
|
||||
(end.tv_nsec - begin.tv_nsec) as f32 / 1000.0 / 1000.0
|
||||
);
|
||||
}
|
Reference in New Issue
Block a user