Skip to content

Commit

Permalink
feat(22/2024): calculate prices and diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 27, 2024
1 parent ad00465 commit e922536
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions src/solutions/year2024/day22.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::solutions::Solution;
use itertools::Itertools;

pub struct Day22;

Expand All @@ -16,15 +17,26 @@ impl Solution for Day22 {
.to_string()
}

fn part_two(&self, _input: &str) -> String {
fn part_two(&self, input: &str) -> String {
let _diffs: Vec<Vec<i8>> = input
.lines()
.map(|line| {
let initial: usize = line.parse().unwrap();
let secrets = self.next_secrets(initial, 2000);
let prices = self.prices(secrets);

self.diffs(prices)
})
.collect();

String::from("0")
}
}

impl Day22 {
fn next_secrets(&self, initial: usize, number_of_secrets: usize) -> Vec<usize> {
let mut secret = initial;
let mut next_secrets = Vec::new();
let mut next_secrets = vec![initial];

for _ in 0..number_of_secrets {
secret = self.mix_and_prune(secret, |s| s * 64);
Expand All @@ -40,38 +52,68 @@ impl Day22 {
fn mix_and_prune(&self, current: usize, calculations: fn(usize) -> usize) -> usize {
(current ^ calculations(current)) % 16777216
}

fn prices(&self, secrets: Vec<usize>) -> Vec<i8> {
secrets.iter().map(|secret| (secret % 10) as i8).collect()
}

fn diffs(&self, secrets: Vec<i8>) -> Vec<i8> {
secrets.iter().tuple_windows().map(|(a, b)| b - a).collect()
}
}

#[cfg(test)]
mod tests {
use crate::solutions::year2024::day22::Day22;
use crate::solutions::Solution;

const EXAMPLE: &str = r#"1
const PART_ONE_EXAMPLE: &str = r#"1
10
100
2024"#;

#[test]
fn part_one_example() {
assert_eq!("37327623", Day22.part_one(EXAMPLE));
assert_eq!("37327623", Day22.part_one(PART_ONE_EXAMPLE));
}

const PART_TWO_EXAMPLE: &str = r#"1
2
3
2024"#;

#[test]
#[ignore]
fn part_two_example() {
assert_eq!("23", Day22.part_two(PART_TWO_EXAMPLE));
}

#[test]
fn next_secrets() {
let tmp = Day22.next_secrets(123, 10);
let mut result = tmp.iter();

assert_eq!(Some(&15887950), result.next());
assert_eq!(Some(&16495136), result.next());
assert_eq!(Some(&527345), result.next());
assert_eq!(Some(&704524), result.next());
assert_eq!(Some(&1553684), result.next());
assert_eq!(Some(&12683156), result.next());
assert_eq!(Some(&11100544), result.next());
assert_eq!(Some(&12249484), result.next());
assert_eq!(Some(&7753432), result.next());
assert_eq!(Some(&5908254), result.next());
assert_eq!(None, result.next());
let secrets = Day22.next_secrets(123, 10);
let mut iter = secrets.iter();

assert_eq!(Some(&123), iter.next());
assert_eq!(Some(&15887950), iter.next());
assert_eq!(Some(&16495136), iter.next());
assert_eq!(Some(&527345), iter.next());
assert_eq!(Some(&704524), iter.next());
assert_eq!(Some(&1553684), iter.next());
assert_eq!(Some(&12683156), iter.next());
assert_eq!(Some(&11100544), iter.next());
assert_eq!(Some(&12249484), iter.next());
assert_eq!(Some(&7753432), iter.next());
assert_eq!(Some(&5908254), iter.next());
assert_eq!(None, iter.next());
}

#[test]
fn prices_and_diffs() {
let secrets = Day22.next_secrets(123, 9);
let prices = Day22.prices(secrets);
assert_eq!(vec![3, 0, 6, 5, 4, 4, 6, 4, 4, 2], prices);

let diffs = Day22.diffs(prices);
assert_eq!(vec![-3, 6, -1, -1, 0, 2, -2, 0, -2], diffs);
}
}

0 comments on commit e922536

Please sign in to comment.