day22: part 1 solution

This commit is contained in:
Keenan Tims 2024-12-21 21:15:59 -08:00
parent 02fc154547
commit c681727fb3
Signed by: ktims
GPG Key ID: 11230674D69038D4
5 changed files with 69 additions and 1 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@ -1,6 +1,6 @@
<!-- AOC TILES BEGIN --> <!-- AOC TILES BEGIN -->
<h1 align="center"> <h1 align="center">
2024 - 40 ⭐ - Rust 2024 - 43 ⭐ - Rust
</h1> </h1>
<a href="src/day1.rs"> <a href="src/day1.rs">
<img src=".aoc_tiles/tiles/2024/01.png" width="161px"> <img src=".aoc_tiles/tiles/2024/01.png" width="161px">
@ -65,4 +65,7 @@
<a href="src/day21.rs"> <a href="src/day21.rs">
<img src=".aoc_tiles/tiles/2024/21.png" width="161px"> <img src=".aoc_tiles/tiles/2024/21.png" width="161px">
</a> </a>
<a href="src/day22.rs">
<img src=".aoc_tiles/tiles/2024/22.png" width="161px">
</a>
<!-- AOC TILES END --> <!-- AOC TILES END -->

64
src/day22.rs Normal file
View File

@ -0,0 +1,64 @@
use aoc_runner_derive::{aoc, aoc_generator};
fn evolve_secret(mut n: i64) -> i64 {
n = ((n * 64) ^ n) % 16777216;
n = ((n / 32) ^ n) % 16777216;
n = ((n * 2048) ^ n) % 16777216;
n
}
fn rounds(mut secret: i64, n: i64) -> i64 {
for _ in 0..n {
secret = evolve_secret(secret)
}
secret
}
fn parse(input: &str) -> Vec<i64> {
input.lines().map(|l| l.parse().unwrap()).collect()
}
#[aoc(day22, part1)]
fn part1(input: &str) -> i64 {
let secrets = parse(input);
secrets.iter().map(|s| rounds(*s, 2000)).sum::<i64>()
}
#[aoc(day22, part2)]
fn part2(input: &str) -> i64 {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = "1
10
100
2024";
#[test]
fn evolution() {
assert_eq!(evolve_secret(123), 15887950);
assert_eq!(evolve_secret(15887950), 16495136);
assert_eq!(evolve_secret(16495136), 527345);
}
#[test]
fn test_rounds() {
assert_eq!(rounds(1, 2000), 8685429);
assert_eq!(rounds(10, 2000), 4700978);
}
#[test]
fn part1_example() {
assert_eq!(part1(EXAMPLE),37327623);
}
#[test]
fn part2_example() {
assert_eq!(part2(EXAMPLE), 0);
}
}

View File

@ -12,6 +12,7 @@ pub mod day19;
pub mod day2; pub mod day2;
pub mod day20; pub mod day20;
pub mod day21; pub mod day21;
pub mod day22;
pub mod day3; pub mod day3;
pub mod day4; pub mod day4;
pub mod day5; pub mod day5;