summaryrefslogtreecommitdiff
path: root/2017/src/bin/day_22.rs
diff options
context:
space:
mode:
Diffstat (limited to '2017/src/bin/day_22.rs')
-rw-r--r--2017/src/bin/day_22.rs73
1 files changed, 73 insertions, 0 deletions
diff --git a/2017/src/bin/day_22.rs b/2017/src/bin/day_22.rs
new file mode 100644
index 0000000..917ed63
--- /dev/null
+++ b/2017/src/bin/day_22.rs
@@ -0,0 +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);
+
+}