organised 2024 year and added day4 in rust solution

This commit is contained in:
deadvey
2024-12-14 02:27:21 +00:00
parent 8f62314da3
commit 1eb0384e35
30 changed files with 268 additions and 0 deletions

16
2024/1/day1/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 = "day1"
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
2024/1/day1/Cargo.toml Normal file
View File

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

1000
2024/1/day1/src/input.txt Normal file

File diff suppressed because it is too large Load Diff

47
2024/1/day1/src/main.rs Normal file
View File

@@ -0,0 +1,47 @@
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 mut num1 = vec![];
let mut num2 = vec![];
for line in data.lines() {
let numbers: Vec<&str> = line.split(" ").collect();
let num11: i32 = numbers[0].parse().unwrap();
let num22: i32 = numbers[1].parse().unwrap();
num1.push(num11);
num2.push(num22);
}
num1.sort_unstable();
num2.sort_unstable();
let mut sum = 0;
for i in 0..num1.len() {
let num_sum = num1[i].abs_diff(num2[i]);
sum += num_sum;
}
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
);
}

BIN
2024/1/day1bothparts/a.out Executable file

Binary file not shown.

View File

@@ -0,0 +1,46 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <chrono>
int main() {
std::vector<int> x;
std::vector<int> y;
int l, r;
long t = 0;
long t2 = 0;
int c = 0;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::ifstream f("b");
while (f >> l >> r) {
x.push_back(l);
y.push_back(r);
}
std::sort(x.begin(), x.end());
std::sort(y.begin(), y.end());
for (size_t i = 0; i < 4000000; i++) {
t += std::abs(x[i] - y[i]);
}
auto p = y.begin();
for (size_t i = 0; i < 4000000; i++) {
auto b = std::lower_bound(p, y.end(), x[i]);
auto u = std::upper_bound(p, y.end(), x[i]);
int c = u - b;
t2 += x[i] * c;
p = b;
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Part 1: " << t << "\nPart 2: " << t2 << "\n";
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << " microseconds" << std::endl;
return 0;
}

1000
2024/1/day1bothparts/m Normal file

File diff suppressed because it is too large Load Diff

8
2024/1/day1bothparts/mult.sh Executable file
View File

@@ -0,0 +1,8 @@
g++ sorter.cpp -o run
time ./run
time ./run | grep real
time ./run | grep real
time ./run | grep real
time ./run | grep real
time ./run | grep real
time ./run

View File

@@ -0,0 +1,30 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
int main() {
std::ifstream f("b");
std::vector<int> x;
std::vector<int> y;
int l, r;
while (f >> l >> r) {
x.push_back(l);
y.push_back(r);
}
std::sort(x.begin(), x.end());
std::sort(y.begin(), y.end());
long t = 0;
for (size_t i = 0; i < 4000000; i++) {
t += std::abs(x[i] - y[i]);
}
std::cout << t;
return 0;
}

View File

@@ -0,0 +1,36 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <cmath>
int main() {
std::ifstream f("b");
std::vector<int> x;
std::vector<int> y;
int l, r;
while (f >> l >> r) {
x.push_back(l);
y.push_back(r);
}
std::sort(x.begin(), x.end());
std::sort(y.begin(), y.end());
long t = 0;
int c = 0;
auto p = y.begin();
for (size_t i = 0; i < 4000000; i++) {
auto b = std::lower_bound(p, y.end(), x[i]);
auto u = std::upper_bound(p, y.end(), x[i]);
int c = u - b;
t += x[i] * c;
p = b;
}
std::cout << t;
return 0;
}

BIN
2024/1/day1bothparts/run Executable file

Binary file not shown.

6
2024/1/day1bothparts/s Normal file
View File

@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

16
2024/1/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
2024/1/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"

1000
2024/1/day1p2/src/input.txt Normal file

File diff suppressed because it is too large Load Diff

50
2024/1/day1p2/src/main.rs Normal file
View File

@@ -0,0 +1,50 @@
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 mut num1 = vec![];
let mut num2 = vec![];
for line in data.lines() {
let numbers: Vec<&str> = line.split(" ").collect();
let num11: i32 = numbers[0].parse().unwrap();
let num22: i32 = numbers[1].parse().unwrap();
num1.push(num11);
num2.push(num22);
}
let mut sum = 0;
for i in num1 {
let mut count = 0;
for num in &num2 {
if i == *num {
count += 1;
}
}
sum += i * count;
}
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
);
}