diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2017-12-13 07:55:33 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2017-12-13 07:55:33 +0200 |
commit | 5399e43d48eda82586b05d1e25d01c7171bf9eef (patch) | |
tree | 05bd1e11cc879934369b0b052b67b86a7c97878a | |
parent | 79bdf55df512f0610a43a8d935b3fe81e88a15c7 (diff) |
Day 13: Navigating the firewall
-rw-r--r-- | inputs/day_13.txt | 43 | ||||
-rw-r--r-- | inputs/day_13_example.txt | 5 | ||||
-rw-r--r-- | src/bin/day_13.rs | 46 |
3 files changed, 94 insertions, 0 deletions
diff --git a/inputs/day_13.txt b/inputs/day_13.txt new file mode 100644 index 0000000..87bfdb7 --- /dev/null +++ b/inputs/day_13.txt @@ -0,0 +1,43 @@ +0: 3 +1: 2 +2: 4 +4: 4 +6: 5 +8: 6 +10: 6 +12: 6 +14: 6 +16: 8 +18: 8 +20: 8 +22: 8 +24: 10 +26: 8 +28: 8 +30: 12 +32: 14 +34: 12 +36: 10 +38: 12 +40: 12 +42: 9 +44: 12 +46: 12 +48: 12 +50: 12 +52: 14 +54: 14 +56: 14 +58: 12 +60: 14 +62: 14 +64: 12 +66: 14 +70: 14 +72: 14 +74: 14 +76: 14 +80: 18 +88: 20 +90: 14 +98: 17 diff --git a/inputs/day_13_example.txt b/inputs/day_13_example.txt new file mode 100644 index 0000000..0de4bec --- /dev/null +++ b/inputs/day_13_example.txt @@ -0,0 +1,5 @@ +0: 3 +1: 2 +4: 4 +6: 4 + diff --git a/src/bin/day_13.rs b/src/bin/day_13.rs new file mode 100644 index 0000000..e85b541 --- /dev/null +++ b/src/bin/day_13.rs @@ -0,0 +1,46 @@ +extern crate advent_of_code_2017; +use advent_of_code_2017::*; + +use std::collections::HashMap; + +fn main() { + let args = AdventArgs::init(); + + let input: HashMap<u32, u32> = args.input.iter().map(|line| { + let mut split_line = line.split(": "); + (split_line.next().unwrap().parse().unwrap(), split_line.next().unwrap().parse().unwrap()) + }).collect(); + + if args.part == 1 { + let severity = calculate_severity(&input, 0, &args); + println!("Severity: {}", severity); + } else { + let optimal_delay = (0u32..).find(|&delay| calculate_severity(&input, delay, &args) == 0).unwrap(); + println!("Wait {} picoseconds", optimal_delay); + } +} + +fn calculate_severity(input: &HashMap<u32, u32>, delay: u32, args: &AdventArgs) -> u32 { + let mut severity = 0; + let max_depth = input.keys().max().cloned().unwrap(); + + for depth in 0..max_depth+1 { + severity += match input.get(&depth) { + Some(range) => { + let position = (depth + delay) % (2*range-2); + + if position == 0 { + if args.part == 1 { + range * depth + } else { + range * depth + 1 + } + } else { + 0 + } + }, + None => 0 + }; + } + severity +} |