Skip to content

Commit 708070f

Browse files
committed
day22 πŸ’
1 parent 6f62f40 commit 708070f

File tree

5 files changed

+118
-7
lines changed

5 files changed

+118
-7
lines changed

β€Ž2024/README.mdβ€Ž

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Advent of Code in Rust πŸ¦€
22

33
![AoC2024](https://img.shields.io/badge/Advent_of_Code-2024-8A2BE2)
4-
![Stars: 42](https://img.shields.io/badge/Stars-42⭐-blue)
5-
![Rust: 22](https://img.shields.io/badge/Rust-22-cyan?logo=Rust)
4+
![Stars: 44](https://img.shields.io/badge/Stars-44⭐-blue)
5+
![Rust: 23](https://img.shields.io/badge/Rust-23-cyan?logo=Rust)
66
![Python: 8](https://img.shields.io/badge/Python-8-cyan?logo=Python)
77

8-
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 42⭐
8+
## 2024 ([Calendar](https://adventofcode.com/2024)) ([Solutions](../2024/)) : 44⭐
99

1010
Puzzle | Stars | Languages
1111
---------------------------------------------------------------------- | ----- | -----------
@@ -30,3 +30,4 @@ Puzzle | Stars |
3030
[Day 19: Linen Layout](https://adventofcode.com/2024/day/19) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day19/day19.rs)
3131
[Day 20: Race Condition](https://adventofcode.com/2024/day/20) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day20/day20.rs)
3232
[Day 21: Keypad Conundrum](https://adventofcode.com/2024/day/21) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day21/day21.rs)
33+
[Day 22: Monkey Market](https://adventofcode.com/2024/day/22) | ⭐⭐ | [![Rust](../scripts/assets/rust.png)](../2024/day22/day22.rs)

β€Ž2024/day22/Cargo.tomlβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "day22"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../../aoc" }
8+
9+
[[bin]]
10+
name = "day22"
11+
path = "day22.rs"

β€Ž2024/day22/day22.rsβ€Ž

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//! [Day 22: Monkey Market](https://adventofcode.com/2024/day/22)
2+
3+
use std::collections::{HashMap, HashSet};
4+
5+
fn next_secret(secret: i64) -> i64 {
6+
let secret = (secret ^ (secret * 64)) % 16777216;
7+
let secret = (secret ^ (secret / 32)) % 16777216;
8+
let secret = (secret ^ (secret * 2048)) % 16777216;
9+
secret
10+
}
11+
12+
struct Puzzle {
13+
initial_secrets: Vec<i64>,
14+
}
15+
16+
impl Puzzle {
17+
fn new() -> Puzzle {
18+
Puzzle {
19+
initial_secrets: Vec::new(),
20+
}
21+
}
22+
23+
/// Get the puzzle input.
24+
fn configure(&mut self, path: &str) {
25+
let data = std::fs::read_to_string(path).unwrap();
26+
27+
self.initial_secrets
28+
.extend(data.lines().map_while(|s| s.parse::<i64>().ok()));
29+
}
30+
31+
/// Solve part one.
32+
fn part1(&self) -> i64 {
33+
self.initial_secrets
34+
.iter()
35+
.map(|&initial_secret| (0..2000).fold(initial_secret, |secret, _| next_secret(secret)))
36+
.sum()
37+
}
38+
39+
/// Solve part two.
40+
fn part2(&self) -> i64 {
41+
let mut bananas = HashMap::new();
42+
43+
for &initial_secret in &self.initial_secrets {
44+
let mut prices = Vec::new();
45+
46+
let mut secret = initial_secret;
47+
prices.push(secret % 10);
48+
for _ in 0..2000 {
49+
secret = next_secret(secret);
50+
prices.push(secret % 10);
51+
}
52+
53+
let mut seen = HashSet::new();
54+
for p in prices.windows(5) {
55+
let sequence = [p[1] - p[0], p[2] - p[1], p[3] - p[2], p[4] - p[3]];
56+
57+
if !seen.contains(&sequence) {
58+
seen.insert(sequence);
59+
*bananas.entry(sequence).or_default() += p[4];
60+
}
61+
}
62+
}
63+
64+
*bananas.values().max().unwrap()
65+
}
66+
}
67+
68+
fn main() {
69+
let args = aoc::parse_args();
70+
let mut puzzle = Puzzle::new();
71+
puzzle.configure(args.path.as_str());
72+
println!("{}", puzzle.part1());
73+
println!("{}", puzzle.part2());
74+
}
75+
76+
/// Test from puzzle input
77+
#[cfg(test)]
78+
mod test {
79+
use super::*;
80+
81+
#[test]
82+
fn test01() {
83+
let mut puzzle = Puzzle::new();
84+
puzzle.configure("test.txt");
85+
assert_eq!(puzzle.part1(), 37327623);
86+
}
87+
88+
#[test]
89+
fn test02() {
90+
let mut puzzle = Puzzle::new();
91+
puzzle.configure("test.txt");
92+
assert_eq!(puzzle.part2(), 23 + 1);
93+
}
94+
}

β€Ž2024/day22/test.txtβ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
10
3+
100
4+
2024

β€ŽREADME.mdβ€Ž

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# [Advent of Code](https://adventofcode.com) in Rust πŸ¦€
22

3-
![Stars: 492](https://img.shields.io/badge/Stars-492⭐-blue)
4-
![Rust: 197](https://img.shields.io/badge/Rust-197-cyan?logo=Rust)
3+
![Stars: 494](https://img.shields.io/badge/Stars-494⭐-blue)
4+
![Rust: 198](https://img.shields.io/badge/Rust-198-cyan?logo=Rust)
55
![Python: 121](https://img.shields.io/badge/Python-121-cyan?logo=Python)
66

77
Solutions of [Advent of Code](https://adventofcode.com/) in [Rust](https://www.rust-lang.org), and sometimes in [Python](https://www.python.org/) and other languages.
88

99
Made for fun 😎 and to practice Rust. Many thanks to [Eric Wastl](https://twitter.com/ericwastl).
1010

11-
## 2024 (current event) ([Calendar](https://adventofcode.com/2024)) ([Solutions](2024/)) : 42⭐
11+
## 2024 (current event) ([Calendar](https://adventofcode.com/2024)) ([Solutions](2024/)) : 44⭐
1212

1313
Puzzle | Stars | Languages
1414
---------------------------------------------------------------------- | ----- | -----------
@@ -33,12 +33,13 @@ Puzzle | Stars |
3333
[Day 19: Linen Layout](https://adventofcode.com/2024/day/19) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day19/day19.rs)
3434
[Day 20: Race Condition](https://adventofcode.com/2024/day/20) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day20/day20.rs)
3535
[Day 21: Keypad Conundrum](https://adventofcode.com/2024/day/21) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day21/day21.rs)
36+
[Day 22: Monkey Market](https://adventofcode.com/2024/day/22) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](./2024/day22/day22.rs)
3637

3738
## All years
3839

3940
Calendar | Solutions | Stars | Rust | Python | πŸŽ„
4041
-------- | --------- | ----- | ---- | ------ | --
41-
[Advent of Code 2024](https://adventofcode.com/2024) | [Solutions](2024/README.md) | 42⭐ | 21 | 8 | 3
42+
[Advent of Code 2024](https://adventofcode.com/2024) | [Solutions](2024/README.md) | 44⭐ | 22 | 8 | 3
4243
[Advent of Code 2023](https://adventofcode.com/2023) | [Solutions](2023/README.md) | 50⭐ | 25 | 11 | 1
4344
[Advent of Code 2022](https://adventofcode.com/2022) | [Solutions](2022/README.md) | 50⭐ | 24 | 18 |
4445
[Advent of Code 2021](https://adventofcode.com/2021) | [Solutions](2021/README.md) | 50⭐ | 23 | 12 |

0 commit comments

Comments
Β (0)