diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2017-12-22 07:24:47 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2017-12-22 07:24:47 +0200 |
commit | 60bf2180f94a4ae4f3a12fe9b469f559d345a403 (patch) | |
tree | bb90f905b6d1e35023f10477430e4ad15d407def /src/bin/day_22.rs | |
parent | caa129205756ea1d3d6a57046a5075272be39785 (diff) |
Day 22: Viruses on a plane
Diffstat (limited to 'src/bin/day_22.rs')
-rw-r--r-- | src/bin/day_22.rs | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/src/bin/day_22.rs b/src/bin/day_22.rs index 1fc3eca..917ed63 100644 --- a/src/bin/day_22.rs +++ b/src/bin/day_22.rs @@ -1,6 +1,73 @@ extern crate advent_of_code_2017; use advent_of_code_2017::*; +use std::collections::HashSet; + fn main() { let args = AdventArgs::init(); + let input_width = args.input[0].len(); + let input_height = args.input.len(); + + let mut position = Point { + x: (input_width / 2) as i32, + y: (input_height / 2) as i32, + }; + let mut direction = Direction::Up; + + let mut weakened: HashSet<Point> = HashSet::new(); + let mut infected: HashSet<Point> = HashSet::new(); + let mut flagged: HashSet<Point> = HashSet::new(); + + for (y, line) in args.input.iter().enumerate() { + for (x, c) in line.chars().enumerate() { + if c == '#' { + infected.insert(Point { + x: x as i32, + y: y as i32 + }); + } + } + } + + let mut infections_caused = 0; + + let bursts = if args.part == 1 { + 10_000 + } else { + 10_000_000 + }; + + for _ in 0..bursts { + if args.part == 1 { + if infected.contains(&position) { + direction = direction.rotate_right(); + infected.remove(&position); + } else { + direction = direction.rotate_left(); + infected.insert(position); + infections_caused += 1; + } + } + else { + if weakened.contains(&position) { + infected.insert(position); + weakened.remove(&position); + infections_caused += 1; + } else if infected.contains(&position) { + direction = direction.rotate_right(); + flagged.insert(position); + infected.remove(&position); + } else if flagged.contains(&position) { + direction = direction.rotate_right().rotate_right(); + flagged.remove(&position); + } else { + direction = direction.rotate_left(); + weakened.insert(position); + } + } + position = position.shift(&direction); + } + + println!("Infections caused {}", infections_caused); + } |