Skip to content

Commit

Permalink
feat(22/2024): solve second part
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 27, 2024
1 parent e922536 commit 84316e9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
| [Day 19: Linen Layout](src/solutions/year2024/day19.rs) | ⭐⭐ | 2.923 | 22.751 |
| [Day 20: Race Condition](src/solutions/year2024/day20.rs) | ⭐⭐ | 7.355 | 280.627 |
| [Day 21: Keypad Conundrum](src/solutions/year2024/day21.rs) || 0.454 | - |
| [Day 22: Monkey Market](src/solutions/year2024/day22.rs) | | 16.325 | - |
| [Day 22: Monkey Market](src/solutions/year2024/day22.rs) | | 16.325 | 270.527 |

# 2023

Expand Down
45 changes: 31 additions & 14 deletions src/solutions/year2024/day22.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use crate::solutions::Solution;
use itertools::Itertools;
use std::collections::{HashMap, HashSet};

pub struct Day22;

const SECRET_COUNT: usize = 2000;

impl Solution for Day22 {
fn part_one(&self, input: &str) -> String {
input
.lines()
.map(|line| {
let initial: usize = line.parse().unwrap();
let secrets = self.next_secrets(initial, 2000);
let secrets = self.next_secrets(initial, SECRET_COUNT);

*secrets.last().unwrap()
})
Expand All @@ -18,18 +21,33 @@ impl Solution for Day22 {
}

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);
let mut map: HashMap<[i8; 4], usize> = HashMap::new();

self.diffs(prices)
})
.collect();
input.lines().for_each(|line| {
let mut seen_map: HashSet<[i8; 4]> = HashSet::new();

let initial: usize = line.parse().unwrap();
let secrets = self.next_secrets(initial, SECRET_COUNT);
let prices = self.prices(secrets);
let diffs = self.diffs(&prices);

assert_eq!(SECRET_COUNT, diffs.len());

for i in 4..=diffs.len() {
let key = [diffs[i - 4], diffs[i - 3], diffs[i - 2], diffs[i - 1]];

if seen_map.contains(&key) {
continue;
}

*map.entry(key).or_insert(0) += prices[i] as usize;
seen_map.insert(key);
}
});

let max = map.iter().max_by_key(|(_, &v)| v).unwrap();

String::from("0")
max.1.to_string()
}
}

Expand Down Expand Up @@ -57,7 +75,7 @@ impl Day22 {
secrets.iter().map(|secret| (secret % 10) as i8).collect()
}

fn diffs(&self, secrets: Vec<i8>) -> Vec<i8> {
fn diffs(&self, secrets: &[i8]) -> Vec<i8> {
secrets.iter().tuple_windows().map(|(a, b)| b - a).collect()
}
}
Expand All @@ -83,7 +101,6 @@ mod tests {
2024"#;

#[test]
#[ignore]
fn part_two_example() {
assert_eq!("23", Day22.part_two(PART_TWO_EXAMPLE));
}
Expand Down Expand Up @@ -113,7 +130,7 @@ mod tests {
let prices = Day22.prices(secrets);
assert_eq!(vec![3, 0, 6, 5, 4, 4, 6, 4, 4, 2], prices);

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

0 comments on commit 84316e9

Please sign in to comment.