From c99848b907d2d63577ffdc81fc11a77e4d328a92 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Tue, 19 Apr 2022 20:24:37 +0200 Subject: Refile for merging repos --- 2017/src/bin/day_11.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 2017/src/bin/day_11.rs (limited to '2017/src/bin/day_11.rs') diff --git a/2017/src/bin/day_11.rs b/2017/src/bin/day_11.rs new file mode 100644 index 0000000..ffb0833 --- /dev/null +++ b/2017/src/bin/day_11.rs @@ -0,0 +1,57 @@ +extern crate advent_of_code_2017; +use advent_of_code_2017::*; + +fn main() { + let args = AdventArgs::init(); + + let directions: Vec<&str> = args.input[0].split(",").collect(); + + let mut x = 0.0; + let mut y = 0.0; + + let mut max_away = 0.0; + + for dir in directions { + y += match dir { + "ne" => 0.5, + "n" => 1.0, + "nw" => 0.5, + "se" => -0.5, + "s" => -1.0, + "sw" => -0.5, + _ => panic!("Unexpected direction {}", dir) + }; + + x += match dir { + "ne" => -0.5, + "n" => 0.0, + "nw" => 0.5, + "se" => -0.5, + "s" => 0.0, + "sw" => 0.5, + _ => panic!("Unexpected direction {}", dir) + }; + + let current_distance = tile_distance(x, y); + if current_distance > max_away { + max_away = current_distance; + } + } + + if args.part == 1 { + println!("Child process is {} away", tile_distance(x, y)); + } else { + println!("At most, child process was {} away", max_away); + } + +} + +fn tile_distance(x: f32, y: f32) -> f32 { + let tiles_x = x.abs()*2.0; + let tiles_y = if y.abs() < tiles_x { + 0.0 + } else { + y.abs() - tiles_x + }; + tiles_x + tiles_y +} -- cgit v1.2.3