Skip to content

Commit

Permalink
refactor(22/2024): simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 27, 2024
1 parent 84316e9 commit 2d1fd55
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/solutions/year2024/day22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ impl Solution for Day22 {
fn part_one(&self, input: &str) -> String {
input
.lines()
.map(|line| {
let initial: usize = line.parse().unwrap();
.map(|line| line.parse::<usize>().unwrap())
.map(|initial| {
let secrets = self.next_secrets(initial, SECRET_COUNT);

*secrets.last().unwrap()
Expand All @@ -25,29 +25,21 @@ impl Solution for Day22 {

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() {
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;
if seen_map.insert(key) {
*map.entry(key).or_insert(0) += prices[i] as usize;
}

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

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

max.1.to_string()
map.values().max().unwrap().to_string()
}
}

Expand Down

0 comments on commit 2d1fd55

Please sign in to comment.