summaryrefslogtreecommitdiff
path: root/2017/src/bin/day_13.rs
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:29:56 +0200
committerJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:29:56 +0200
commit34c0aa87fada4bf3bc75ff0493e0876e65289697 (patch)
tree599148bcbb7f05941edfac5ab3454a878129e997 /2017/src/bin/day_13.rs
parent174772b5b8d9f5bf5e3c8e8152adfd89f0e83f6b (diff)
parentc99848b907d2d63577ffdc81fc11a77e4d328a92 (diff)
Merge branch '2017-main'
Diffstat (limited to '2017/src/bin/day_13.rs')
-rw-r--r--2017/src/bin/day_13.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/2017/src/bin/day_13.rs b/2017/src/bin/day_13.rs
new file mode 100644
index 0000000..e85b541
--- /dev/null
+++ b/2017/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
+}