-
Notifications
You must be signed in to change notification settings - Fork 0
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
721a9d6
commit 123bc36
Showing
9 changed files
with
227 additions
and
5 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
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,60 @@ | ||
use advent_rs::year_2017::day_13; | ||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
use itertools::Itertools; | ||
use std::time::Duration; | ||
|
||
// Found here: https://colab.research.google.com/github/hhoppe/advent_of_code/blob/main/2017/advent_of_code_2017.ipynb#scrollTo=84e08a8d-ca2e-4bc7-99ca-8a29a98d0bf9&line=15&uniqifier=1 | ||
pub fn day_13_v2_sieve(input: impl Into<String>) -> u32 { | ||
let firewalls: Vec<_> = input | ||
.into() | ||
.lines() | ||
.map(|line| { | ||
let mut tuple: (i32, i32) = line | ||
.split(": ") | ||
.map(|num| num.parse::<i32>().unwrap()) | ||
.collect_tuple() | ||
.unwrap(); | ||
tuple.1 = (tuple.1 - 1) * 2; | ||
tuple | ||
}) | ||
.collect(); | ||
|
||
let mut delay = 0; | ||
let chunk: i32 = 200_000; | ||
loop { | ||
let mut ok = vec![true; chunk as usize]; | ||
for (depth, range) in &firewalls { | ||
let first = (-(delay as i32 + *depth as i32)) | ||
.rem_euclid(*range as i32) | ||
.abs(); | ||
for idx in (first..chunk).step_by(*range as usize) { | ||
ok[idx as usize] = false | ||
} | ||
} | ||
if let Some(index) = ok.iter().position(|v| *v == true) { | ||
return delay as u32 + index as u32; | ||
} | ||
delay += chunk as i32; | ||
} | ||
} | ||
|
||
fn bench_year_2017_day_13_v2(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("year_2017::day_13_v2"); | ||
group.warm_up_time(Duration::from_millis(100)); | ||
let input = include_str!("../inputs/year_2017/day_13_input"); | ||
assert_eq!(day_13_v2_sieve(input), 3_875_838); | ||
assert_eq!(day_13::day_13_v2(input), 3_875_838); | ||
group.bench_with_input(BenchmarkId::new("Sieve", input.len()), input, |b, input| { | ||
b.iter(|| day_13_v2_sieve(input)) | ||
}); | ||
group.bench_with_input( | ||
BenchmarkId::new("Normal", input.len()), | ||
input, | ||
|b, input| b.iter(|| day_13::day_13_v2(input)), | ||
); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(bench_year_2017_day_13, bench_year_2017_day_13_v2); | ||
criterion_main!(bench_year_2017_day_13); |
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,43 @@ | ||
0: 3 | ||
1: 2 | ||
2: 4 | ||
4: 4 | ||
6: 5 | ||
8: 6 | ||
10: 6 | ||
12: 8 | ||
14: 6 | ||
16: 6 | ||
18: 8 | ||
20: 12 | ||
22: 8 | ||
24: 8 | ||
26: 9 | ||
28: 8 | ||
30: 8 | ||
32: 12 | ||
34: 20 | ||
36: 10 | ||
38: 12 | ||
40: 12 | ||
42: 10 | ||
44: 12 | ||
46: 12 | ||
48: 12 | ||
50: 12 | ||
52: 12 | ||
54: 14 | ||
56: 14 | ||
58: 12 | ||
62: 14 | ||
64: 14 | ||
66: 14 | ||
68: 14 | ||
70: 14 | ||
72: 14 | ||
74: 14 | ||
76: 14 | ||
78: 14 | ||
80: 18 | ||
82: 17 | ||
84: 14 |
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,80 @@ | ||
use itertools::Itertools; | ||
|
||
fn parse_input(input: &str) -> Vec<(i32, i32, i32)> { | ||
input | ||
.lines() | ||
.map(|line| { | ||
let tuple: (i32, i32) = line | ||
.split(": ") | ||
.map(|num| num.parse::<i32>().unwrap()) | ||
.collect_tuple() | ||
.unwrap(); | ||
(tuple.0, tuple.1, (tuple.1 - 1) * 2) | ||
}) | ||
.collect() | ||
} | ||
|
||
pub fn day_13_v1(input: impl Into<String>) -> u32 { | ||
let firewalls = parse_input(&input.into()); | ||
let mut severity: u32 = 0; | ||
for (depth, range, position) in firewalls.iter() { | ||
if *depth == 0 || (depth % position) == 0 { | ||
severity += *range as u32 * *depth as u32; | ||
} | ||
} | ||
|
||
severity | ||
} | ||
|
||
#[inline] | ||
fn traverse_firewall(firewalls: &[(i32, i32)], delay: u32) -> bool { | ||
for (depth, range) in firewalls.iter() { | ||
if ((*depth as u32 + delay) % *range as u32) == 0 { | ||
return false; | ||
} | ||
} | ||
true | ||
} | ||
|
||
pub fn day_13_v2(input: impl Into<String>) -> u32 { | ||
let firewalls: Vec<_> = input | ||
.into() | ||
.lines() | ||
.map(|line| { | ||
let mut tuple: (i32, i32) = line | ||
.split(": ") | ||
.map(|num| num.parse::<i32>().unwrap()) | ||
.collect_tuple() | ||
.unwrap(); | ||
tuple.1 = (tuple.1 - 1) * 2; | ||
tuple | ||
}) | ||
.collect(); | ||
let mut delay = 0; | ||
while !traverse_firewall(&firewalls, delay) { | ||
delay += 1; | ||
} | ||
delay | ||
} | ||
|
||
solvable!(day_13, day_13_v1, day_13_v2, u32); | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const SAMPLE: &str = "0: 3\n\ | ||
1: 2\n\ | ||
4: 4\n\ | ||
6: 4"; | ||
|
||
#[test] | ||
fn works_with_samples_v1() { | ||
assert_eq!(day_13_v1(SAMPLE), 24); | ||
} | ||
|
||
#[test] | ||
fn works_with_samples_v2() { | ||
assert_eq!(day_13_v2(SAMPLE), 10); | ||
} | ||
} |