summaryrefslogtreecommitdiff
path: root/2017/src/bin/day_11.rs
blob: ffb083316649bdcf145a50ba75b6ce3ac706a13a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
}