Day 01
This commit is contained in:
41
src/Year_2024/Day_01.jl
Normal file
41
src/Year_2024/Day_01.jl
Normal file
@@ -0,0 +1,41 @@
|
||||
using BenchmarkTools
|
||||
using StatsBase
|
||||
|
||||
function Day_01()
|
||||
# Robust file reading and parsing
|
||||
list_nums = Vector{Int}()
|
||||
open("files/P1.txt", "r") do file
|
||||
for line in eachline(file)
|
||||
# Split the line and parse the first two numbers
|
||||
nums = split(strip(line))
|
||||
if length(nums) >= 2
|
||||
push!(list_nums, parse(Int, nums[1]))
|
||||
push!(list_nums, parse(Int, nums[2]))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Preallocate arrays and use views to avoid unnecessary allocations
|
||||
l1 = view(list_nums, 1:2:length(list_nums))
|
||||
l2 = view(list_nums, 2:2:length(list_nums))
|
||||
|
||||
# Sort in-place to reduce memory allocations
|
||||
sort!(l1)
|
||||
sort!(l2)
|
||||
|
||||
# Optimized part 1: Use broadcast with a temporary array to avoid repeated allocations
|
||||
p1 = sum(abs, l1 .- l2)
|
||||
|
||||
# Optimized part 2: Use dictionary for efficient counting and avoid multiple passes
|
||||
freq = Dict{Int, Int}()
|
||||
for x in l2
|
||||
freq[x] = get(freq, x, 0) + 1
|
||||
end
|
||||
|
||||
p2 = sum(x -> x * get(freq, x, 0), l1)
|
||||
|
||||
return p1, p2
|
||||
end
|
||||
|
||||
println(Day_01())
|
||||
@btime Day_01()
|
||||
19
src/Year_2024/Day_01.py
Normal file
19
src/Year_2024/Day_01.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from collections import Counter
|
||||
|
||||
from utils import timeit
|
||||
|
||||
|
||||
@timeit("Problem 051")
|
||||
def Day_01():
|
||||
with open("files/P1.txt") as f:
|
||||
list_nums = [int(lines) for lines in f.read().strip().split()]
|
||||
l1, l2 = sorted(list_nums[::2]), sorted(list_nums[1::2])
|
||||
|
||||
p1 = sum(abs(n1 - n2) for n1, n2 in zip(l1, l2))
|
||||
p2 = sum(n1 * Counter(l2)[n1] for n1 in l1)
|
||||
|
||||
return p1, p2
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
Day_01()
|
||||
62
src/Year_2024/Day_01.rs
Normal file
62
src/Year_2024/Day_01.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::time::Instant;
|
||||
|
||||
fn bench<F>(f: F)
|
||||
where
|
||||
F: Fn() -> std::io::Result<()>
|
||||
{
|
||||
let t0 = Instant::now();
|
||||
f().expect("Benchmarked function failed");
|
||||
println!("Took {:?}", t0.elapsed());
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
bench(|| {
|
||||
let f = File::open("files/P1.txt")?;
|
||||
let reader = BufReader::new(f);
|
||||
|
||||
let lines = reader.lines();
|
||||
|
||||
let mut left_list = Vec::new();
|
||||
let mut right_list = Vec::new();
|
||||
for line in lines {
|
||||
let Ok(line) = line else {
|
||||
continue;
|
||||
};
|
||||
if line.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let numbers = line
|
||||
.split_whitespace()
|
||||
.map(|num| num
|
||||
.parse::<i32>()
|
||||
.unwrap())
|
||||
.collect::<Vec<i32>>();
|
||||
left_list.push(numbers[0]);
|
||||
right_list.push(numbers[1]);
|
||||
}
|
||||
left_list.sort();
|
||||
right_list.sort();
|
||||
|
||||
let p1 = part1(&left_list, &right_list);
|
||||
let p2 = part2(&left_list, &right_list);
|
||||
|
||||
println!("{:?}", (p1,p2));
|
||||
Ok(())
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn part1(left_list: &[i32], right_list: &[i32]) -> i32 {
|
||||
left_list.iter().zip(right_list.iter()).map(|(a, b)| (a - b).abs()).sum()
|
||||
}
|
||||
|
||||
fn part2(left_list: &[i32], right_list: &[i32]) -> i32 {
|
||||
let mut freq: HashMap<i32, i32> = HashMap::new();
|
||||
right_list.iter().for_each(|r| *freq.entry(*r).or_insert(0) += 1);
|
||||
|
||||
left_list.iter().filter_map(|l| freq.get(l).map(|r| l * r)).sum()
|
||||
}
|
||||
1000
src/Year_2024/files/P1.txt
Normal file
1000
src/Year_2024/files/P1.txt
Normal file
File diff suppressed because it is too large
Load Diff
21
src/Year_2024/utils.py
Normal file
21
src/Year_2024/utils.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def timeit(name):
|
||||
def profile(original):
|
||||
import time
|
||||
|
||||
@wraps(original)
|
||||
def wrapper(*args, **kwargs):
|
||||
t0 = time.perf_counter()
|
||||
result = original(*args, **kwargs)
|
||||
t1 = time.perf_counter()
|
||||
print(result)
|
||||
if (t1 - t0) > 1:
|
||||
print(f"Took: {(t1 - t0):.3f} s\n")
|
||||
else:
|
||||
print(f"Took: {(t1 - t0)*1000:.3f} ms\n")
|
||||
|
||||
return wrapper
|
||||
|
||||
return profile
|
||||
Reference in New Issue
Block a user