-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca1ad54
commit 6755684
Showing
7 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! # Dueling Generators | ||
//! | ||
//! Simple but slow solution, implementing each generator as an [`Iterator`]. | ||
use crate::util::iter::*; | ||
use crate::util::parse::*; | ||
use std::iter::from_fn; | ||
|
||
type Input = [u64; 2]; | ||
|
||
pub fn parse(input: &str) -> Input { | ||
input.iter_unsigned().chunk::<2>().next().unwrap() | ||
} | ||
|
||
pub fn part1(input: &Input) -> usize { | ||
let first = generator(input[0], 16807); | ||
let second = generator(input[1], 48271); | ||
judge(first, second, 40_000_000) | ||
} | ||
|
||
pub fn part2(input: &Input) -> usize { | ||
let first = generator(input[0], 16807).filter(|&a| a % 4 == 0); | ||
let second = generator(input[1], 48271).filter(|&b| b % 8 == 0); | ||
judge(first, second, 5_000_000) | ||
} | ||
|
||
fn generator(mut state: u64, factor: u64) -> impl Iterator<Item = u64> { | ||
from_fn(move || { | ||
state = (state * factor) % 0x7fffffff; | ||
Some(state) | ||
}) | ||
} | ||
|
||
fn judge( | ||
first: impl Iterator<Item = u64>, | ||
second: impl Iterator<Item = u64>, | ||
items: usize, | ||
) -> usize { | ||
first.zip(second).take(items).filter(|&(a, b)| a & 0xffff == b & 0xffff).count() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,7 @@ mod year2017 { | |
mod day12_test; | ||
mod day13_test; | ||
mod day14_test; | ||
mod day15_test; | ||
} | ||
|
||
mod year2019 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use aoc::year2017::day15::*; | ||
|
||
const EXAMPLE: &str = "\ | ||
Generator A starts with 65 | ||
Generator B starts with 8921"; | ||
|
||
#[test] | ||
fn part1_test() { | ||
let input = parse(EXAMPLE); | ||
assert_eq!(part1(&input), 588); | ||
} | ||
|
||
#[test] | ||
fn part2_test() { | ||
let input = parse(EXAMPLE); | ||
assert_eq!(part2(&input), 309); | ||
} |