1
0
forked from danmax/aoc
This commit is contained in:
2025-12-03 10:52:12 +00:00
parent ba2391bd8e
commit 3e214636ca
3 changed files with 139 additions and 33 deletions

View File

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

View File

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