Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hsaikia committed Dec 22, 2024
1 parent 33b9bb9 commit 01a539c
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions src/bin/2024_22/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,26 @@ use std::{collections::HashMap, ops::BitXor};

use aoc::{common, io};

fn mix(secret: usize, b: usize) -> usize {
secret.bitxor(b)
fn mix_and_prune(secret: usize, b: usize) -> usize {
secret.bitxor(b) % 16777216
}

fn prune(secret: usize) -> usize {
secret % 16777216
fn transform(secret: usize) -> usize {
let mut secret = secret;
let a = secret * 64;
secret = mix_and_prune(secret, a);
let b = secret / 32;
secret = mix_and_prune(secret, b);
let c = secret * 2048;
secret = mix_and_prune(secret, c);
secret
}

fn generate(n: usize, times: usize) -> usize {
fn generate(secret: usize, times: usize) -> usize {
if times == 0 {
return n;
return secret;
}

let mut secret = n;
let a = secret * 64;
secret = mix(secret, a);
secret = prune(secret);
let b = secret / 32;
secret = mix(secret, b);
secret = prune(secret);
let c = secret * 2048;
secret = mix(secret, c);
secret = prune(secret);
generate(secret, times - 1)
generate(transform(secret), times - 1)
}

fn generate_lst(lst: &mut Vec<usize>, times: usize) {
Expand All @@ -34,15 +30,7 @@ fn generate_lst(lst: &mut Vec<usize>, times: usize) {
}

let mut secret = *lst.last().unwrap();
let n2 = secret * 64;
secret = mix(secret, n2);
secret = prune(secret);
let n4 = secret / 32;
secret = mix(secret, n4);
secret = prune(secret);
let n6 = secret * 2048;
secret = mix(secret, n6);
secret = prune(secret);
secret = transform(secret);
lst.push(secret);
generate_lst(lst, times - 1)
}
Expand Down

0 comments on commit 01a539c

Please sign in to comment.