Skip to content

Commit 801e25b

Browse files
committed
[2024] Day 22 initial solution
1 parent 746d5a1 commit 801e25b

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Thank you to [Eric Wastl](http://was.tl) for running this incredible yearly even
2626
- [Day 19: Linen Layout](aoc_2024/src/day_19.rs)
2727
- [Day 20: Race Condition](aoc_2024/src/day_20.rs)
2828
- [Day 21: Keypad Conundrum](aoc_2024/src/day_21.rs)
29+
- [Day 22: Monkey Market](aoc_2024/src/day_22.rs)
2930
<!-- MARKER -->
3031

3132
## [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)

aoc_2024/src/day_22.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
use std::iter::repeat;
2+
3+
use common::{solution, Answer};
4+
use itertools::Itertools;
5+
use rayon::iter::{ParallelBridge, ParallelIterator};
6+
7+
solution!("Monkey Market", 22);
8+
9+
fn part_a(input: &str) -> Answer {
10+
let mut sum = 0;
11+
12+
for num in input.lines() {
13+
let mut num = num.parse::<u64>().unwrap();
14+
15+
for _ in 0..2000 {
16+
num ^= num * 64;
17+
num %= 16777216;
18+
19+
num ^= num / 32;
20+
num %= 16777216;
21+
22+
num ^= num * 2048;
23+
num %= 16777216;
24+
}
25+
26+
sum += num;
27+
}
28+
29+
sum.into()
30+
}
31+
32+
fn part_b(input: &str) -> Answer {
33+
let mut buyers = Vec::new();
34+
let mut diffs = Vec::new();
35+
36+
for num in input.lines() {
37+
let mut num = num.parse::<u64>().unwrap();
38+
let mut seq = vec![num];
39+
40+
for _ in 0..2000 {
41+
num ^= num * 64;
42+
num %= 16777216;
43+
44+
num ^= num / 32;
45+
num %= 16777216;
46+
47+
num ^= num * 2048;
48+
num %= 16777216;
49+
50+
seq.push(num);
51+
}
52+
53+
buyers.push(seq);
54+
}
55+
56+
for buyer in buyers.iter() {
57+
let mut diff = Vec::new();
58+
for (&a, &b) in buyer.iter().tuple_windows() {
59+
diff.push((b % 10) as i8 - (a % 10) as i8);
60+
}
61+
diffs.push(diff);
62+
}
63+
64+
let out = repeat(-9..=9)
65+
.take(4)
66+
.multi_cartesian_product()
67+
.par_bridge()
68+
.map(|x| {
69+
let (a, b, c, d) = (x[0], x[1], x[2], x[3]);
70+
let mut sum = 0;
71+
72+
for (diff, nums) in diffs.iter().zip(buyers.iter()) {
73+
let idx = diff
74+
.iter()
75+
.tuple_windows()
76+
.position(|(&ax, &bx, &cx, &dx)| ax == a && bx == b && cx == c && dx == d);
77+
78+
if let Some(idx) = idx {
79+
sum += nums[idx + 4] % 10;
80+
}
81+
}
82+
83+
sum
84+
})
85+
.max()
86+
.unwrap();
87+
88+
out.into()
89+
}
90+
91+
#[cfg(test)]
92+
mod test {
93+
use indoc::indoc;
94+
95+
const CASE: &str = indoc! {"
96+
1
97+
10
98+
100
99+
2024
100+
"};
101+
102+
#[test]
103+
fn part_a() {
104+
assert_eq!(super::part_a(CASE), 37327623.into());
105+
}
106+
107+
#[test]
108+
fn part_b() {
109+
assert_eq!(super::part_b(CASE), 24.into());
110+
}
111+
}

aoc_2024/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod day_18;
2121
mod day_19;
2222
mod day_20;
2323
mod day_21;
24+
mod day_22;
2425
// [import_marker]
2526

2627
pub const SOLUTIONS: &[Solution] = &[
@@ -45,5 +46,6 @@ pub const SOLUTIONS: &[Solution] = &[
4546
day_19::SOLUTION,
4647
day_20::SOLUTION,
4748
day_21::SOLUTION,
49+
day_22::SOLUTION,
4850
// [list_marker]
4951
];

0 commit comments

Comments
 (0)