Skip to content

Commit

Permalink
Year 2017 Day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Jun 1, 2024
1 parent ca1ad54 commit 6755684
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
| 12 | [Digital Plumber](https://adventofcode.com/2017/day/12) | [Source](src/year2017/day12.rs) | 59 |
| 13 | [Packet Scanners](https://adventofcode.com/2017/day/13) | [Source](src/year2017/day13.rs) | 2 |
| 14 | [Disk Defragmentation](https://adventofcode.com/2017/day/14) | [Source](src/year2017/day14.rs) | 422 |
| 15 | [Dueling Generators](https://adventofcode.com/2017/day/15) | [Source](src/year2017/day15.rs) | 425000 |

## 2016

Expand Down
1 change: 1 addition & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ mod year2017 {
benchmark!(year2017, day12);
benchmark!(year2017, day13);
benchmark!(year2017, day14);
benchmark!(year2017, day15);
}

mod year2019 {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub mod year2017 {
pub mod day12;
pub mod day13;
pub mod day14;
pub mod day15;
}

/// # Rescue Santa from deep space with a solar system adventure.
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ fn year2017() -> Vec<Solution> {
solution!(year2017, day12),
solution!(year2017, day13),
solution!(year2017, day14),
solution!(year2017, day15),
]
}

Expand Down
39 changes: 39 additions & 0 deletions src/year2017/day15.rs
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()
}
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ mod year2017 {
mod day12_test;
mod day13_test;
mod day14_test;
mod day15_test;
}

mod year2019 {
Expand Down
17 changes: 17 additions & 0 deletions tests/year2017/day15_test.rs
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);
}

0 comments on commit 6755684

Please sign in to comment.