Skip to content

Commit dceccde

Browse files
committed
Added day 22 Rust based solution
1 parent e3097ab commit dceccde

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

2024/day21/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
utils = { workspace = true }
8-
itertools = "0.14.0"
98

109
[[bin]]
1110
name = "day21"

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+
utils = { workspace = true }
8+
9+
[[bin]]
10+
name = "day22"
11+
path = "day22.rs"

2024/day22/day22.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
fn generate_secret(mut num: i64) -> i64 {
2+
num = ((64 * num) ^ num) % 16777216;
3+
num = (num ^ (num / 32)) % 16777216;
4+
num = ((num * 2048) ^ num) % 16777216;
5+
num
6+
}
7+
8+
fn part1(input: String) -> i64 {
9+
let buyers = input
10+
.lines()
11+
.map(|l| l.parse::<i64>().unwrap())
12+
.collect::<Vec<_>>();
13+
14+
let mut total = 0;
15+
16+
let mut chunk = [0i64; 16];
17+
let mut chunk_index = 0;
18+
19+
for &buyer in &buyers {
20+
chunk[chunk_index] = buyer;
21+
chunk_index += 1;
22+
23+
if chunk_index == 16 {
24+
let mut temp_chunk = chunk;
25+
for _ in 0..2000 {
26+
for i in 0..16 {
27+
temp_chunk[i] = generate_secret(temp_chunk[i]);
28+
}
29+
}
30+
total += temp_chunk.iter().sum::<i64>();
31+
chunk_index = 0;
32+
}
33+
}
34+
35+
for i in 0..chunk_index {
36+
let mut buyer = chunk[i];
37+
for _ in 0..2000 {
38+
buyer = generate_secret(buyer);
39+
}
40+
total += buyer;
41+
}
42+
43+
total
44+
}
45+
46+
fn part2(input: String) -> i32 {
47+
let buyers = input
48+
.lines()
49+
.map(|l| l.parse::<i64>().unwrap())
50+
.collect::<Vec<_>>();
51+
52+
let size = 19usize.pow(4);
53+
let mut map = vec![0; size];
54+
55+
for mut buyer in buyers {
56+
let mut seen = vec![0u8; (size + 7) / 8];
57+
let mut deltas = [0i8; 4];
58+
let mut old_price = (buyer % 10) as i8;
59+
60+
for _ in 0..2000 {
61+
buyer = generate_secret(buyer);
62+
let price = (buyer % 10) as i8;
63+
64+
let delta = price - old_price;
65+
old_price = price;
66+
67+
deltas[0] = deltas[1];
68+
deltas[1] = deltas[2];
69+
deltas[2] = deltas[3];
70+
deltas[3] = delta;
71+
72+
if deltas.iter().any(|&x| x == 0) {
73+
continue;
74+
}
75+
76+
let index = (deltas[0] + 9) as usize * 19usize.pow(3)
77+
+ (deltas[1] + 9) as usize * 19usize.pow(2)
78+
+ (deltas[2] + 9) as usize * 19usize
79+
+ (deltas[3] + 9) as usize;
80+
81+
let byte_index = index / 8;
82+
let bit_index = index % 8;
83+
if seen[byte_index] & (1 << bit_index) != 0 {
84+
continue;
85+
}
86+
seen[byte_index] |= 1 << bit_index;
87+
map[index] += price as i32;
88+
}
89+
}
90+
91+
map.into_iter().max().unwrap()
92+
}
93+
94+
#[cfg(test)]
95+
mod tests {
96+
use utils::read_file;
97+
98+
use super::*;
99+
100+
#[test]
101+
fn test1() {
102+
assert_eq!(part1(read_file("sample1.txt")), 37327623);
103+
}
104+
}
105+
106+
fn main() {
107+
utils::run(22, &["sample1.txt", "input.txt"], &part1, &part2);
108+
}

2024/day22/sample1.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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Para esta edição, planejo usar três linguagens de programação: Rust, C e Ja
3131
| Dia 19 | [Rust](./2024/day19/day19.rs) |
3232
| Dia 20 | [Rust](./2024/day20/day20.rs) |
3333
| Dia 21 | [Rust](./2024/day21/day21.rs) |
34+
| Dia 22 | [Rust](./2024/day22/day22.rs) |
3435

3536
### 2023
3637

0 commit comments

Comments
 (0)