1
0
forked from danmax/aoc

Compare commits

..

2 Commits

Author SHA1 Message Date
bad7dafe2b Merge branch 'main' of ssh://git.javalsai.tuxcord.net:1022/deadvey/aoc 2025-12-03 10:53:15 +00:00
3e214636ca Day 3 2025-12-03 10:52:12 +00:00
3 changed files with 139 additions and 33 deletions

View File

@@ -1,15 +1,48 @@
use std::fs; use std::fs;
use std::time::Instant; use std::time::Instant;
fn algorithm(vector: &Vec<Vec<&str>>) -> (u64, u64) {
let mut total1: u64 = 0;
let mut total2: u64 = 0;
for pair in vector
{
let num_1: u64 = pair[0].parse().unwrap();
let num_2: u64 = pair[1].parse().unwrap();
for num in num_1..=num_2 { // Loop over each number in the range
let num_string = num.to_string();
let num_length = num_string.len();
for chunk_width in (1..=(num_length/2)).rev() {
if num_length % chunk_width == 0 {
let chunks = num_length / chunk_width;
let mut same: bool = true;
let mut previous = &num_string[0..chunk_width];
for i in 0..chunks {
let current = &num_string[i*chunk_width..i*chunk_width+chunk_width];
if current != previous {
same = false;
break;
}
previous = current;
}
if same {
total2 += num;
if chunks == 2 {
total1 += num;
}
break;
}
}
}
}
}
(total1, total2)
}
// you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice. // you can find the invalid IDs by looking for any ID which is made only of some sequence of digits repeated twice.
// So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs. // So, 55 (5 twice), 6464 (64 twice), and 123123 (123 twice) would all be invalid IDs.
fn main() { fn main() {
let mut total1: u64 = 0;
let mut total2: u64 = 0;
let mut string_temp_1: String = "".to_string();
let mut string_temp_2: String = "".to_string();
let start = Instant::now(); let start = Instant::now();
let mut file_contents: String = fs::read_to_string("../large.txt").unwrap(); let mut file_contents: String = fs::read_to_string("../large.txt").unwrap();
file_contents.pop();// Remove \n file_contents.pop();// Remove \n
// Split it up // Split it up
@@ -21,35 +54,9 @@ fn main() {
.collect(); .collect();
let file_read = start.elapsed(); let file_read = start.elapsed();
let (total1, total2) = algorithm(&pairs);
// There's a lot of converting between int and string // There's a lot of converting between int and string
for pair in pairs
{
let num_1: u64 = pair[0].parse().unwrap();
let num_2: u64 = pair[1].parse().unwrap();
for num in num_1..=num_2 { // Loop over each number in the range
let num_string = num.to_string();
for divider in 2..=num_string.len() {
if num_string.len() % divider == 0 {
let chunk_size = num_string.len() / divider;
let substring_vector: Vec<String> = num_string.chars()
.collect::<Vec<char>>()
.chunks(chunk_size)
.map(|chunk| chunk.iter().collect::<String>())
.collect();
if substring_vector.iter().all(|x| x == &substring_vector[0]) {
total2 += num;
if divider == 2 {
total1 += num;
}
break
}
}
}
}
//clear both strings
string_temp_1.clear();
string_temp_2.clear();
}
let end = start.elapsed(); let end = start.elapsed();
println!("part1: {total1}"); println!("part1: {total1}");
println!("part2: {total2}"); println!("part2: {total2}");

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);
}