summaryrefslogtreecommitdiff
path: root/2016/aoc2/src/main.rs
blob: f4c3bfd758371c1ce0c17ddda5baa5187f10fa3e (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
58
59
60
61
use std::io::BufReader;
use std::io::prelude::*;
use std::fs::File;

fn main() {
    let lines = read_file();
    let mut current = 5;
    for line in lines {
        current = line.chars().fold(current, |current, dir| move_char_hex(current, dir));
        println!("{:X}", current);
    }
}

fn read_file() -> Vec<String> {
    let file = BufReader::new(File::open("input.txt").unwrap());
    file.lines()
        .map(|line| line.unwrap().trim().to_string())
        .filter(|line| line.len() > 0)
        .collect()
}


//assume current in 1-9 range, and char is in UDLR
fn move_char(current: i32, dir: char) -> i32 {
    match dir {
        'U' => if current <= 3 { current } else { current - 3},
        'D' => if current >= 7 { current } else { current + 3},
        'L' => if current%3 == 1 { current } else { current - 1},
        'R' => if current%3 == 0 { current } else { current + 1},
        _ => panic!("Bad direction character")
    }
}

//     1
//   2 3 4
// 5 6 7 8 9
//   A B C
//     D
fn move_char_hex(current: i32, dir: char) -> i32 {
    match dir {
        'U' => match current {
            1|2|4|5|9 => current,
            3|13 => current - 2,
            _  => current - 4
        },
        'D' => match current {
            5|10|13|12|9 => current,
            1|11 => current + 1,
            _ => current + 4
        },
        'L' => match current {
            1|2|5|10|13 => current,
            _ => current - 1
        },
        'R' => match current {
            1|4|9|12|13 => current,
            _ => current + 1
        },
        _ => panic!("Bad direction character")
    }
}