Skip to content

Commit 3d8bcf3

Browse files
committed
AoC 2027 Day 15 - rust
1 parent 9bd06a6 commit 3d8bcf3

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
| bash | [](src/main/bash/AoC2017_01.sh) | [](src/main/bash/AoC2017_02.sh) | | [](src/main/bash/AoC2017_04.sh) | [](src/main/bash/AoC2017_05.sh) | | | | | | | | | | | | | | | | | | | | |
123123
| c++ | [](src/main/cpp/2017/01/AoC2017_01.cpp) | [](src/main/cpp/2017/02/AoC2017_02.cpp) | [](src/main/cpp/2017/03/AoC2017_03.cpp) | [](src/main/cpp/2017/04/AoC2017_04.cpp) | [](src/main/cpp/2017/05/AoC2017_05.cpp) | | | | [](src/main/cpp/2017/09/AoC2017_09.cpp) | | [](src/main/cpp/2017/11/AoC2017_11.cpp) | | [](src/main/cpp/2017/13/AoC2017_13.cpp) | | | | | [](src/main/cpp/2017/18/AoC2017_18.cpp) | | | [](src/main/cpp/2017/21/AoC2017_21.cpp) | | | | [](src/main/cpp/2017/25/AoC2017_25.cpp) |
124124
| julia | [](src/main/julia/AoC2017_01.jl) | [](src/main/julia/AoC2017_02.jl) | [](src/main/julia/AoC2017_03.jl) | [](src/main/julia/AoC2017_04.jl) | | | | | | | | | [](src/main/julia/AoC2017_13.jl) | | | | | | | | | | | | |
125+
| rust | | | | | | | | | | | | | | | [](src/main/rust/AoC2017_15/src/main.rs) | | | | | | | | | | |
125126
<!-- @END:ImplementationsTable:2017@ -->
126127

127128
## 2016

src/main/rust/AoC2017_15/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "AoC2017_15"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
aoc = { path = "../aoc" }

src/main/rust/AoC2017_15/src/main.rs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#![allow(non_snake_case)]
2+
3+
use aoc::Puzzle;
4+
5+
const FACTOR_A: u64 = 16807;
6+
const FACTOR_B: u64 = 48271;
7+
const MOD: u64 = 2147483647;
8+
9+
enum Criteria {
10+
A1,
11+
B1,
12+
A2,
13+
B2,
14+
}
15+
16+
impl Criteria {
17+
fn apply(&self, val: u64) -> bool {
18+
match self {
19+
Self::A1 | Self::B1 => true,
20+
Self::A2 => val % 4 == 0,
21+
Self::B2 => val % 8 == 0,
22+
}
23+
}
24+
}
25+
26+
struct Generator {
27+
factor: u64,
28+
val: u64,
29+
criteria: Criteria,
30+
}
31+
32+
impl Generator {
33+
fn new(factor: u64, val: u64, criteria: Criteria) -> Self {
34+
Self {
35+
factor,
36+
val,
37+
criteria,
38+
}
39+
}
40+
}
41+
42+
impl Iterator for Generator {
43+
type Item = u64;
44+
45+
fn next(&mut self) -> Option<Self::Item> {
46+
let mut nxt_val = self.val;
47+
loop {
48+
nxt_val = (nxt_val * self.factor) % MOD;
49+
if self.criteria.apply(nxt_val) {
50+
self.val = nxt_val;
51+
return Some(nxt_val);
52+
}
53+
}
54+
}
55+
}
56+
57+
struct AoC2017_15;
58+
59+
impl AoC2017_15 {
60+
fn solve(
61+
&self,
62+
seeds: (u64, u64),
63+
criteria: (Criteria, Criteria),
64+
reps: usize,
65+
) -> usize {
66+
let mut gen_a = Generator::new(FACTOR_A, seeds.0, criteria.0);
67+
let mut gen_b = Generator::new(FACTOR_B, seeds.1, criteria.1);
68+
(0..reps)
69+
.filter(|_| {
70+
gen_a.next().unwrap() & 0xFFFF == gen_b.next().unwrap() & 0xFFFF
71+
})
72+
.count()
73+
}
74+
}
75+
76+
impl aoc::Puzzle for AoC2017_15 {
77+
type Input = (u64, u64);
78+
type Output1 = usize;
79+
type Output2 = usize;
80+
81+
aoc::puzzle_year_day!(2017, 15);
82+
83+
fn parse_input(&self, lines: Vec<String>) -> Self::Input {
84+
let mut it = (0..=1).map(|i| {
85+
lines[i]
86+
.split_whitespace()
87+
.last()
88+
.unwrap()
89+
.parse::<u64>()
90+
.unwrap()
91+
});
92+
(it.next().unwrap(), it.next().unwrap())
93+
}
94+
95+
fn part_1(&self, seeds: &Self::Input) -> Self::Output1 {
96+
self.solve(*seeds, (Criteria::A1, Criteria::B1), 40_000_000)
97+
}
98+
99+
fn part_2(&self, seeds: &Self::Input) -> Self::Output2 {
100+
self.solve(*seeds, (Criteria::A2, Criteria::B2), 5_000_000)
101+
}
102+
103+
fn samples(&self) {
104+
aoc::puzzle_samples! {
105+
self, part_1, TEST, 588,
106+
self, part_2, TEST, 309
107+
};
108+
}
109+
}
110+
111+
fn main() {
112+
AoC2017_15 {}.run(std::env::args());
113+
}
114+
115+
const TEST: &str = "\
116+
Generator A starts with 65
117+
Generator B starts with 8921
118+
";
119+
120+
#[cfg(test)]
121+
mod tests {
122+
use super::*;
123+
124+
#[test]
125+
pub fn samples() {
126+
AoC2017_15 {}.samples();
127+
}
128+
}

src/main/rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy