Skip to content

Commit

Permalink
Year 2017 Day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Jun 15, 2024
1 parent 6abde26 commit d3a6420
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
| 17 | [Spinlock](https://adventofcode.com/2017/day/17) | [Source](src/year2017/day17.rs) | 84 |
| 18 | [Duet](https://adventofcode.com/2017/day/18) | [Source](src/year2017/day18.rs) | 7 |
| 19 | [A Series of Tubes](https://adventofcode.com/2017/day/19) | [Source](src/year2017/day19.rs) | 20 |
| 23 | [Coprocessor Conflagration](https://adventofcode.com/2017/day/23) | [Source](src/year2017/day23.rs) | 55 |

## 2016

Expand Down
1 change: 1 addition & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ mod year2017 {
benchmark!(year2017, day17);
benchmark!(year2017, day18);
benchmark!(year2017, day19);
benchmark!(year2017, day23);
}

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 @@ -102,6 +102,7 @@ pub mod year2017 {
pub mod day17;
pub mod day18;
pub mod day19;
pub mod day23;
}

/// # 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 @@ -167,6 +167,7 @@ fn year2017() -> Vec<Solution> {
solution!(year2017, day17),
solution!(year2017, day18),
solution!(year2017, day19),
solution!(year2017, day23),
]
}

Expand Down
81 changes: 81 additions & 0 deletions src/year2017/day23.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! # Coprocessor Conflagration
//!
//! Just like [`Day 18`] reverse engineering the code is essential. The entire input can be reduced
//! to only the very first number.
//!
//! ```none
//! set b $NUMBER if a == 0 {
//! set c b b = $NUMBER;
//! jnz a 2 c = b;
//! jnz 1 5 } else {
//! mul b 100 b = 100000 + 100 * $NUMBER;
//! sub b -100000 c = b + 17000;
//! set c b }
//! sub c -17000
//! set f 1 for b in (b..=c).step_by(17) {
//! set d 2 f = 1;
//! set e 2 for d in 2..b {
//! set g d for e in 2..b {
//! mul g e if d * e == b {
//! sub g b f = 0;
//! jnz g 2 }
//! set f 0
//! sub e -1
//! set g e
//! sub g b
//! jnz g -8 }
//! sub d -1
//! set g d
//! sub g b
//! jnz g -13 }
//! jnz f 2
//! sub h -1 if f == 0 {
//! set g b h += 1;
//! sub g c }
//! jnz g 2
//! jnz 1 3
//! sub b -17
//! jnz 1 -23 }
//! ```
//!
//! ## Part One
//!
//! The number of `mul` operations is the product of the two inner loops from 2 to `n` exclusive.
//!
//! ## Part Two
//!
//! Counts the number of composite numbers starting from `100,000 + 100 * n` checking the next
//! 1,000 numbers in steps of 17. The raw code take `O(n²)` complexity for each number so emulating
//! this directly would take at least 10⁵.10⁵.10³ = 10¹³ = 10,000,000,000,000 steps.
//!
//! [`Day 18`]: crate::year2017::day18

use crate::util::math::*;
use crate::util::parse::*;

/// We only need the vrey first number from the input.
pub fn parse(input: &str) -> u32 {
input.unsigned()
}

/// The number of `mul` operations is `(n - 2)²`
pub fn part1(input: &u32) -> u32 {
(input - 2) * (input - 2)
}

/// Count the number of composite numbers in a range calculated from the input number.
pub fn part2(input: &u32) -> usize {
(0..=1000).filter_map(|n| composite(100_000 + 100 * input + 17 * n)).count()
}

/// Simple [prime number check](https://en.wikipedia.org/wiki/Primality_test)
/// of all factors from 2 to √n inclusive.
fn composite(n: u32) -> Option<u32> {
for f in 2..=n.sqrt() {
if n % f == 0 {
return Some(n);
}
}

None
}
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ mod year2017 {
mod day17_test;
mod day18_test;
mod day19_test;
mod day23_test;
}

mod year2019 {
Expand Down
15 changes: 15 additions & 0 deletions tests/year2017/day23_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use aoc::year2017::day23::*;

const EXAMPLE: &str = "123";

#[test]
fn part1_test() {
let input = parse(EXAMPLE);
assert_eq!(part1(&input), 14641);
}

#[test]
fn part2_test() {
let input = parse(EXAMPLE);
assert_eq!(part2(&input), 913);
}

0 comments on commit d3a6420

Please sign in to comment.