Skip to content

Commit

Permalink
feat(22/2024): solve first part
Browse files Browse the repository at this point in the history
  • Loading branch information
manhunto committed Dec 27, 2024
1 parent bf1694b commit 0ca3726
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
| [Day 19: Linen Layout](src/solutions/year2024/day19.rs) | ⭐⭐ | 2.923 | 22.751 |
| [Day 20: Race Condition](src/solutions/year2024/day20.rs) | ⭐⭐ | 7.355 | 280.627 |
| [Day 21: Keypad Conundrum](src/solutions/year2024/day21.rs) || 0.454 | - |
| [Day 22: Monkey Market](src/solutions/year2024/day22.rs) | - | - | - |
| [Day 22: Monkey Market](src/solutions/year2024/day22.rs) | | 16.325 | - |

# 2023

Expand Down
63 changes: 59 additions & 4 deletions src/solutions/year2024/day22.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,79 @@ use crate::solutions::Solution;
pub struct Day22;

impl Solution for Day22 {
fn part_one(&self, _input: &str) -> String {
String::from("0")
fn part_one(&self, input: &str) -> String {
input
.lines()
.map(|line| {
let initial: usize = line.parse().unwrap();
let secrets = self.next_secrets(initial, 2000);

*secrets.last().unwrap()
})
.sum::<usize>()
.to_string()
}

fn part_two(&self, _input: &str) -> String {
String::from("0")
}
}

impl Day22 {
fn next_secrets(&self, initial: usize, number_of_secrets: usize) -> Vec<usize> {
let mut secret = initial;
let mut next_secrets = Vec::new();

for _ in 0..number_of_secrets {
let tmp = secret * 64;
secret ^= tmp;
secret %= 16777216;

let tmp = secret / 32;
secret ^= tmp;
secret %= 16777216;

let tmp = secret * 2048;
secret ^= tmp;
secret %= 16777216;

next_secrets.push(secret);
}

next_secrets
}
}

#[cfg(test)]
mod tests {
use crate::solutions::year2024::day22::Day22;
use crate::solutions::Solution;

const EXAMPLE: &str = r#""#;
const EXAMPLE: &str = r#"1
10
100
2024"#;

#[test]
fn part_one_example() {
assert_eq!("0", Day22.part_one(EXAMPLE));
assert_eq!("37327623", Day22.part_one(EXAMPLE));
}

#[test]
fn next_secrets() {
let tmp = Day22.next_secrets(123, 10);
let mut result = tmp.iter();

assert_eq!(Some(&15887950), result.next());
assert_eq!(Some(&16495136), result.next());
assert_eq!(Some(&527345), result.next());
assert_eq!(Some(&704524), result.next());
assert_eq!(Some(&1553684), result.next());
assert_eq!(Some(&12683156), result.next());
assert_eq!(Some(&11100544), result.next());
assert_eq!(Some(&12249484), result.next());
assert_eq!(Some(&7753432), result.next());
assert_eq!(Some(&5908254), result.next());
assert_eq!(None, result.next());
}
}

0 comments on commit 0ca3726

Please sign in to comment.