Skip to content

Commit

Permalink
[2024] Day 22 initial solution
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 22, 2024
1 parent 746d5a1 commit 801e25b
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
- [Day 19: Linen Layout](aoc_2024/src/day_19.rs)
- [Day 20: Race Condition](aoc_2024/src/day_20.rs)
- [Day 21: Keypad Conundrum](aoc_2024/src/day_21.rs)
- [Day 22: Monkey Market](aoc_2024/src/day_22.rs)
<!-- MARKER -->

## [2023](https://adventofcode.com/2023) [![aoc_2023](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2023.yml/badge.svg)](https://github.com/connorslade/advent-of-code/actions/workflows/aoc_2023.yml)
Expand Down
111 changes: 111 additions & 0 deletions aoc_2024/src/day_22.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::iter::repeat;

use common::{solution, Answer};
use itertools::Itertools;
use rayon::iter::{ParallelBridge, ParallelIterator};

solution!("Monkey Market", 22);

fn part_a(input: &str) -> Answer {
let mut sum = 0;

for num in input.lines() {
let mut num = num.parse::<u64>().unwrap();

for _ in 0..2000 {
num ^= num * 64;
num %= 16777216;

num ^= num / 32;
num %= 16777216;

num ^= num * 2048;
num %= 16777216;
}

sum += num;
}

sum.into()
}

fn part_b(input: &str) -> Answer {
let mut buyers = Vec::new();
let mut diffs = Vec::new();

for num in input.lines() {
let mut num = num.parse::<u64>().unwrap();
let mut seq = vec![num];

for _ in 0..2000 {
num ^= num * 64;
num %= 16777216;

num ^= num / 32;
num %= 16777216;

num ^= num * 2048;
num %= 16777216;

seq.push(num);
}

buyers.push(seq);
}

for buyer in buyers.iter() {
let mut diff = Vec::new();
for (&a, &b) in buyer.iter().tuple_windows() {
diff.push((b % 10) as i8 - (a % 10) as i8);
}
diffs.push(diff);
}

let out = repeat(-9..=9)
.take(4)
.multi_cartesian_product()
.par_bridge()
.map(|x| {
let (a, b, c, d) = (x[0], x[1], x[2], x[3]);
let mut sum = 0;

for (diff, nums) in diffs.iter().zip(buyers.iter()) {
let idx = diff
.iter()
.tuple_windows()
.position(|(&ax, &bx, &cx, &dx)| ax == a && bx == b && cx == c && dx == d);

if let Some(idx) = idx {
sum += nums[idx + 4] % 10;
}
}

sum
})
.max()
.unwrap();

out.into()
}

#[cfg(test)]
mod test {
use indoc::indoc;

const CASE: &str = indoc! {"
1
10
100
2024
"};

#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 37327623.into());
}

#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 24.into());
}
}
2 changes: 2 additions & 0 deletions aoc_2024/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod day_18;
mod day_19;
mod day_20;
mod day_21;
mod day_22;
// [import_marker]

pub const SOLUTIONS: &[Solution] = &[
Expand All @@ -45,5 +46,6 @@ pub const SOLUTIONS: &[Solution] = &[
day_19::SOLUTION,
day_20::SOLUTION,
day_21::SOLUTION,
day_22::SOLUTION,
// [list_marker]
];

0 comments on commit 801e25b

Please sign in to comment.