Skip to content

Commit 25838e6

Browse files
committed
Add solutions to day 25
1 parent 9b716b5 commit 25838e6

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/day25/mod.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
pub fn part1(door_public_key: u64, card_public_key: u64) -> u64 {
2+
let mut loop_size = 0;
3+
let mut public_key = 1;
4+
5+
while public_key != door_public_key && public_key != card_public_key {
6+
public_key = (public_key * 7) % 20201227;
7+
loop_size += 1;
8+
}
9+
10+
let subject_number = if public_key == door_public_key {
11+
card_public_key
12+
} else {
13+
door_public_key
14+
};
15+
16+
let mut encryption_key = 1;
17+
18+
while loop_size != 0 {
19+
encryption_key = (encryption_key * subject_number) % 20201227;
20+
loop_size -= 1;
21+
}
22+
23+
encryption_key
24+
}
25+
26+
#[cfg(test)]
27+
mod tests {
28+
use super::*;
29+
30+
#[test]
31+
fn part1_works() {
32+
assert_eq!(part1(5764801, 17807724), 14897079);
33+
assert_eq!(part1(1614360, 7734663), 5414549);
34+
}
35+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ pub mod day21;
2222
pub mod day22;
2323
pub mod day23;
2424
pub mod day24;
25+
pub mod day25;

0 commit comments

Comments
 (0)