1
0
forked from danmax/aoc

DeaDvey d3 optimisations & danmax

This commit is contained in:
2025-12-03 20:40:47 +00:00
parent bad7dafe2b
commit fbc32d12de
3 changed files with 50 additions and 21 deletions

6
2025/3/danmax/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "danmax"
version = "0.1.0"
edition = "2024"
[dependencies]

18
2025/3/danmax/src/main.rs Normal file
View File

@@ -0,0 +1,18 @@
fn main() {
let raw = std::fs::read_to_string("../large.txt").unwrap();
let start = std::time::Instant::now();
let mut banks = raw.split('\n').collect::<Vec<&str>>();
banks.pop();
let mut sol = 0;
for bank in banks {
let batteries = bank.chars().map(|x| (x as u8) - 48).collect::<Vec<u8>>();
let mut combos = vec![];
for i in 0..batteries.len() - 1 {
let num = (batteries[i] * 10) + batteries[(i + 1)..batteries.len()].iter().max().unwrap();
combos.push(num);
}
sol += *combos.iter().max().unwrap() as u64;
}
let end = start.elapsed();
dbg!(sol, end);
}

View File

@@ -1,5 +1,6 @@
use std::fs; use std::fs;
use std::time::Instant; use std::time::Instant;
use std::ptr;
fn algorithm(banks: &Vec<&str>) -> (u64, u64) fn algorithm(banks: &Vec<&str>) -> (u64, u64)
{ {
@@ -11,39 +12,43 @@ fn algorithm(banks: &Vec<&str>) -> (u64, u64)
let mut joltages: [u64; 12] = [0; 12]; let mut joltages: [u64; 12] = [0; 12];
let mut highest: u64 = 0; let mut highest: u64 = 0;
let mut overall_index = 0; let mut overall_index = 0;
// part 2
for i in 0..=11 { for i in 0..=11 {
for joltage in bank[prev_index..(bank_length-(11-i))].bytes() { for joltage in bank[prev_index..(bank_length-(11-i))].bytes() {
overall_index += 1; overall_index += 1;
if joltage == 57 { if joltage as u64 > highest {
highest = 9;
prev_index = overall_index; prev_index = overall_index;
break; unsafe {
} ptr::write(&mut highest as *mut u64, joltage as u64);
let joltage_int = joltage ^ 48; // ASCII mask using XOR (^) }
if joltage_int as u64 > highest { if joltage == 57 { break; }
prev_index = overall_index;
highest = joltage_int as u64;
} }
} }
overall_index = prev_index; overall_index = prev_index;
joltages[i] = highest as u64; joltages[i] = highest ^ 48;
highest = 0; highest = 0;
} }
prev_index = 0; prev_index = 0;
for i in 0..=10 { for i in 0..=10 {
if joltages[i] > highest.into() { if joltages[i] > highest.into() {
highest = joltages[i] as u64; unsafe {
ptr::write(&mut highest as *mut u64, joltages[i] as u64);
}
prev_index = i+1; prev_index = i+1;
if joltages[i] == 9 { break; }
} }
} }
total1 += (highest * 10) as u64; total1 += highest * 10;
highest = 0; highest = 0;
for i in prev_index..=11 { for i in prev_index..=11 {
if joltages[i] > highest.into() { if joltages[i] > highest.into() {
highest = joltages[i] as u64; unsafe {
ptr::write(&mut highest as *mut u64, joltages[i] as u64);
}
if joltages[i] == 9 { break; }
} }
} }
total1 += (highest) as u64; total1 += highest;
total2 += joltages[0] * 10_u64.pow(11) total2 += joltages[0] * 10_u64.pow(11)
+ joltages[1] * 10_u64.pow(10) + joltages[1] * 10_u64.pow(10)
+ joltages[2] * 10_u64.pow(9) + joltages[2] * 10_u64.pow(9)
@@ -66,28 +71,28 @@ fn algorithm(banks: &Vec<&str>) -> (u64, u64)
// 234234234234278 // 234234234234278
// 818181911112111 // 818181911112111
fn main() { fn main() {
let start = Instant::now();
let file_contents: String = fs::read_to_string("../large.txt").unwrap(); let file_contents: String = fs::read_to_string("../large.txt").unwrap();
let start = Instant::now();
let mut banks: Vec<&str> = file_contents let mut banks: Vec<&str> = file_contents
.split('\n') .split('\n')
.collect::<Vec<&str>>(); .collect::<Vec<&str>>();
banks.pop(); banks.pop();
let file_read = start.elapsed(); let preamble = start.elapsed();
let (total1, total2) = algorithm(&banks); let (total1, total2) = algorithm(&banks);
let end = start.elapsed(); let end = start.elapsed();
let end = start.elapsed();
println!("part1: {total1}"); println!("part1: {total1}");
println!("part2: {total2}"); println!("part2: {total2}");
println!("Times: println!("Times:
File read: {:.2?} Preamble: {:.2?}
Loop: {:.2?} Loop: {:.2?}
Avg time per pair: {:.2?} Avg time per bank: {:.2?}
Total: {:.4?} Total: {:.4?}
", ",
file_read, preamble,
end-file_read, end-preamble,
(end-file_read)/(10000).try_into().unwrap(), (end-preamble)/(10000).try_into().unwrap(),
end); end);
} }