Skip to content

Commit

Permalink
Year 2017: Day 23+24+25
Browse files Browse the repository at this point in the history
  • Loading branch information
joshleaves committed Apr 3, 2024
1 parent 63ea710 commit 6876ce9
Show file tree
Hide file tree
Showing 12 changed files with 675 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ Of note:
- The changelog 2015.5.2 has been rewritten from each commit content.
- This file may be amended entirely in the future to adhere to the [GNU Changelog style](https://www.gnu.org/prep/standards/html_node/Style-of-Change-Logs.html#Style-of-Change-Logs)

## [2017.25.1]
### Added
- Solved [exercice for 2017, day 23](src/year_2017/23.rs).
- Solved [exercice for 2017, day 24](src/year_2017/24.rs).
- Solved [exercice for 2017, day 25](src/year_2017/25.rs).

## [2017.22.1]
### Added
- Solved [exercice for 2017, day 22](src/year_2017/22.rs).
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "advent-rs"
version = "2017.22.1"
version = "2017.25.1"
edition = "2021"
authors = ["Arnaud 'red' Rouyer"]
readme = "README.md"
Expand Down
12 changes: 12 additions & 0 deletions NOTES_2017.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ That one was both funny and very annoying. Playing with patterns and turning the
## Day 22: Sporifica Virus

Very funny exercise, but I'll spend a long time trying to optimize it.

## Day 23: Coprocessor Conflagration

This exercise [is actually a trap](https://www.youtube.com/watch?v=4F4qzPbcFiA): running it as expected would last you around ten minutes. Were you to [rewrite it as pseudo-code](https://docs.rs/advent-of-code/2022.0.66/src/advent_of_code/year2017/day23.rs.html#15), you would see another algorithm, that is [way easier to implement](https://github.com/galenelias/AdventOfCode_2017/blob/master/src/Day23/mod.rs#L70).

## Day 24: Electromagnetic Moat

Your usual shortest-path algorithm.

## Day 25: The Halting Problem

Implementing a [Turing machine](https://en.wikipedia.org/wiki/Turing_machine) isn't that hard once you get to the principle. Hardest part was actually parsing the input.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ As I said, [Consistency is hard](https://github.com/joshleaves/advent-rb), and I
I'm also adding notes that may be useful if you're (like me) discovering Rust:
- [2015, complete!](NOTES_2015.md)
- [2016, complete!](NOTES_2016.md)
- [2017, up to day 22](NOTES_2017.md)
- [2017, complete!](NOTES_2017.md)

# Regarding style rules
I'm gonna use a mix of what `cargo fmt` does, with some stuff that feels more natural to me.
Expand Down
50 changes: 49 additions & 1 deletion benches/year_2017.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use advent_rs::year_2017::day_19;
use advent_rs::year_2017::day_20;
use advent_rs::year_2017::day_21;
use advent_rs::year_2017::day_22;
use advent_rs::year_2017::day_23;
use advent_rs::year_2017::day_24;
use advent_rs::year_2017::day_25;
use criterion::{black_box, criterion_group, criterion_main, Criterion};

fn year_2017_day_01(c: &mut Criterion) {
Expand Down Expand Up @@ -352,6 +355,48 @@ fn year_2017_day_22(c: &mut Criterion) {
g2017_day_22.finish();
}

fn year_2017_day_23(c: &mut Criterion) {
let input = include_str!("../inputs/year_2017/day_23_input");
assert_eq!(day_23::day_23_v1(input), 6_724);
assert_eq!(day_23::day_23_v2(input), 903);

let mut g2017_day_23 = c.benchmark_group("year_2017::day_23");
g2017_day_23.bench_function("year_2017::day_23_v1", |b| {
b.iter(|| day_23::day_23_v1(black_box(input)))
});
g2017_day_23.bench_function("year_2017::day_23_v2", |b| {
b.iter(|| day_23::day_23_v2(black_box(input)))
});
g2017_day_23.finish();
}

fn year_2017_day_24(c: &mut Criterion) {
let input = include_str!("../inputs/year_2017/day_24_input");
assert_eq!(day_24::day_24_v1(input), 1_906);
assert_eq!(day_24::day_24_v2(input), 1_824);

let mut g2017_day_24 = c.benchmark_group("year_2017::day_24");
g2017_day_24.bench_function("year_2017::day_24_v1", |b| {
b.iter(|| day_24::day_24_v1(black_box(input)))
});
g2017_day_24.bench_function("year_2017::day_24_v2", |b| {
b.iter(|| day_24::day_24_v2(black_box(input)))
});
g2017_day_24.finish();
}

fn year_2017_day_25(c: &mut Criterion) {
let input = include_str!("../inputs/year_2017/day_25_input");
assert_eq!(day_25::day_25(input), 4_387);

let mut g2017_day_25 = c.benchmark_group("year_2017::day_25");
g2017_day_25.bench_function("year_2017::day_25", |b| {
b.iter(|| day_25::day_25(black_box(input)))
});

g2017_day_25.finish();
}

criterion_group!(
benches,
year_2017_day_01,
Expand All @@ -375,6 +420,9 @@ criterion_group!(
year_2017_day_19,
year_2017_day_20,
year_2017_day_21,
year_2017_day_22
year_2017_day_22,
year_2017_day_23,
year_2017_day_24,
year_2017_day_25
);
criterion_main!(benches);
32 changes: 32 additions & 0 deletions inputs/year_2017/day_23_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
set b 84
set c b
jnz a 2
jnz 1 5
mul b 100
sub b -100000
set c b
sub c -17000
set f 1
set d 2
set e 2
set g d
mul g e
sub g b
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
set g b
sub g c
jnz g 2
jnz 1 3
sub b -17
jnz 1 -23
56 changes: 56 additions & 0 deletions inputs/year_2017/day_24_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
31/13
34/4
49/49
23/37
47/45
32/4
12/35
37/30
41/48
0/47
32/30
12/5
37/31
7/41
10/28
35/4
28/35
20/29
32/20
31/43
48/14
10/11
27/6
9/24
8/28
45/48
8/1
16/19
45/45
0/4
29/33
2/5
33/9
11/7
32/10
44/1
40/32
2/45
16/16
1/18
38/36
34/24
39/44
32/37
26/46
25/33
9/10
0/29
38/8
33/33
49/19
18/20
49/39
18/39
26/13
19/32
62 changes: 62 additions & 0 deletions inputs/year_2017/day_25_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
Begin in state A.
Perform a diagnostic checksum after 12208951 steps.

In state A:
If the current value is 0:
- Write the value 1.
- Move one slot to the right.
- Continue with state B.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state E.

In state B:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state C.
If the current value is 1:
- Write the value 0.
- Move one slot to the right.
- Continue with state A.

In state C:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state D.
If the current value is 1:
- Write the value 0.
- Move one slot to the right.
- Continue with state C.

In state D:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state E.
If the current value is 1:
- Write the value 0.
- Move one slot to the left.
- Continue with state F.

In state E:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state A.
If the current value is 1:
- Write the value 1.
- Move one slot to the left.
- Continue with state C.

In state F:
If the current value is 0:
- Write the value 1.
- Move one slot to the left.
- Continue with state E.
If the current value is 1:
- Write the value 1.
- Move one slot to the right.
- Continue with state A.
26 changes: 26 additions & 0 deletions src/year_2017.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ pub mod day_19;
pub mod day_20;
pub mod day_21;
pub mod day_22;
pub mod day_23;
pub mod day_24;
pub mod day_25;

pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
if part > 2 {
Expand Down Expand Up @@ -55,6 +58,9 @@ pub fn solve(day: u8, part: u8, input: impl Into<String>) -> Option<String> {
20 => Some(day_20::day_20(part, input).to_string()),
21 => Some(day_21::day_21(part, input).to_string()),
22 => Some(day_22::day_22(part, input).to_string()),
23 => Some(day_23::day_23(part, input).to_string()),
24 => Some(day_24::day_24(part, input).to_string()),
25 => Some(day_25::day_25(input).to_string()),
_ => None,
}
}
Expand Down Expand Up @@ -216,4 +222,24 @@ mod tests {
assert_eq!(day_22::day_22_v1(input), 5_246);
assert_eq!(day_22::day_22_v2(input), 2_512_059);
}

#[test]
fn day_23() {
let input = include_str!("../inputs/year_2017/day_23_input");
assert_eq!(day_23::day_23_v1(input), 6_724);
assert_eq!(day_23::day_23_v2(input), 903);
}

#[test]
fn day_24() {
let input = include_str!("../inputs/year_2017/day_24_input");
assert_eq!(day_24::day_24_v1(input), 1_906);
assert_eq!(day_24::day_24_v2(input), 1_824);
}

#[test]
fn day_25() {
let input = include_str!("../inputs/year_2017/day_25_input");
assert_eq!(day_25::day_25(input), 4_387);
}
}
Loading

0 comments on commit 6876ce9

Please sign in to comment.