Skip to content

Commit c899afd

Browse files
Add code for day 22 of 2024 (rust)
1 parent 288a3c2 commit c899afd

File tree

7 files changed

+83
-9
lines changed

7 files changed

+83
-9
lines changed

2024/src/day21/bin/day21.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod part1;
66
mod part2;
77

88
fn main() {
9-
let _input = read_to_string("../input.txt").unwrap();
9+
let _input = read_to_string("./day21/testinput.txt").unwrap();
1010

1111
// start timer
1212
let start = Instant::now();

2024/src/day21/testinput.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
029A
2+
980A
3+
179A
4+
456A
5+
379A

2024/src/day22/bin/day22.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod part1;
66
mod part2;
77

88
fn main() {
9-
let _input = read_to_string("../input.txt").unwrap();
9+
let _input = read_to_string("./day22/input.txt").unwrap();
1010

1111
// start timer
1212
let start = Instant::now();

2024/src/day22/bin/part1.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,38 @@
11
//! # Advent of Code - Day 22 - Part One
22
3+
use cached::proc_macro::cached;
4+
5+
fn mix(new_value: i64, secret_number: i64) -> i64 {
6+
return new_value ^ secret_number;
7+
}
8+
9+
fn prune(new_value: i64) -> i64 {
10+
return new_value.rem_euclid(16777216);
11+
}
12+
13+
#[cached]
14+
pub fn generate_next(mut secret_num: i64) -> i64 {
15+
secret_num = prune(mix(secret_num * 64, secret_num));
16+
secret_num = prune(mix((secret_num as f64 / 32.0).floor() as i64, secret_num));
17+
return prune(mix(secret_num * 2048, secret_num));
18+
}
19+
320
pub fn part1(_input: &str) -> usize {
4-
return 0;
21+
let secret_numbers = _input
22+
.split("\n")
23+
.filter(|c| !c.is_empty())
24+
.map(|c| c.parse::<i64>().unwrap())
25+
.collect::<Vec<i64>>();
26+
27+
let mut count = 0;
28+
for mut secret_num in secret_numbers {
29+
for _ in 0..2000 {
30+
secret_num = generate_next(secret_num);
31+
}
32+
count += secret_num;
33+
}
34+
35+
return count as usize;
536
}
637

738
#[cfg(test)]
@@ -10,6 +41,7 @@ mod day22 {
1041

1142
#[test]
1243
fn test_part1() {
13-
assert_eq!(part1(""), 0);
44+
let _input = "1\n10\n100\n2024";
45+
assert_eq!(part1(_input), 37327623);
1446
}
1547
}

2024/src/day22/bin/part2.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
11
//! # Advent of Code - Day 22 - Part Two
22
3+
use std::collections::{HashMap, HashSet};
4+
5+
use crate::part1::generate_next;
6+
37
pub fn part2(_input: &str) -> usize {
4-
return 0;
8+
let secret_numbers = _input
9+
.split("\n")
10+
.filter(|c| !c.is_empty())
11+
.map(|c| c.parse::<i64>().unwrap())
12+
.collect::<Vec<i64>>();
13+
14+
let mut seq_total = HashMap::new();
15+
for mut secret_num in secret_numbers {
16+
let mut buyer = vec![secret_num % 10];
17+
for _ in 0..2000 {
18+
secret_num = generate_next(secret_num);
19+
buyer.push(secret_num % 10);
20+
}
21+
let mut seen = HashSet::new();
22+
for i in 0..(buyer.len() - 4) {
23+
let diff = buyer[i..(i + 5)]
24+
.windows(2)
25+
.map(|window| window[1] - window[0])
26+
.collect::<Vec<i64>>();
27+
let diff_tup = (diff[0], diff[1], diff[2], diff[3]);
28+
if seen.contains(&diff_tup) {
29+
continue;
30+
}
31+
seen.insert(diff_tup);
32+
*seq_total.entry(diff_tup).or_insert(0) += buyer[i + 4];
33+
}
34+
}
35+
36+
return *seq_total.iter().map(|(_, val)| val).max().unwrap() as usize;
537
}
638

739
#[cfg(test)]
@@ -10,6 +42,7 @@ mod day22 {
1042

1143
#[test]
1244
fn test_part2() {
13-
assert_eq!(part2(""), 0);
45+
let _input = "1\n2\n3\n2024";
46+
assert_eq!(part2(_input), 23);
1447
}
1548
}

2024/src/day22/testinput.txt

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

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ chosen to learn Typescript. For 2024, we will do AoC in rust.
3737
| Day 17 | Chronospatial Computer | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day17/) | ⭐️⭐️ |
3838
| Day 18 | RAM Run | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day18/) | ⭐️⭐️ |
3939
| Day 19 | Linen Layout | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day19/) | ⭐️⭐️ |
40-
| Day 20 | | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day20/) | |
41-
| Day 21 | | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day21/) | |
42-
| Day 22 | | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day22/) | |
40+
| Day 20 | Race Condition | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day20/) | ⭐️⭐️ |
41+
| Day 21 | Keypad Conundrum | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day21/) | |
42+
| Day 22 | Monkey Market | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day22/) | ⭐️⭐️ |
4343
| Day 23 | | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day23/) | |
4444
| Day 24 | | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day24/) | |
4545
| Day 25 | | [Dir](https://gitlab.com/loicdiridollou/advent-of-code/-/blob/main/2024/src/day25/) | |

0 commit comments

Comments
 (0)