summaryrefslogtreecommitdiff
path: root/aoc2
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@entelect.co.za>2016-12-05 09:06:25 +0200
committerJustin Worthe <justin.worthe@entelect.co.za>2016-12-05 09:06:25 +0200
commit43d57662b16bf8bd5e2891e38470da34e0e59ee2 (patch)
tree00b0c50ed36dbb6f663c9db21506c1ad046bf4a7 /aoc2
parent403f2a2b43d25d30f9b0e41c548017650505f48c (diff)
Refactor day 2
Diffstat (limited to 'aoc2')
-rw-r--r--aoc2/src/main.rs58
1 files changed, 14 insertions, 44 deletions
diff --git a/aoc2/src/main.rs b/aoc2/src/main.rs
index e3188e7..f4c3bfd 100644
--- a/aoc2/src/main.rs
+++ b/aoc2/src/main.rs
@@ -4,10 +4,10 @@ use std::fs::File;
fn main() {
let lines = read_file();
- let mut current = '5';
+ let mut current = 5;
for line in lines {
- current = line.chars().fold(current, |current, dir| move_char_2(current, dir));
- println!("{}", current);
+ current = line.chars().fold(current, |current, dir| move_char_hex(current, dir));
+ println!("{:X}", current);
}
}
@@ -36,55 +36,25 @@ fn move_char(current: i32, dir: char) -> i32 {
// 5 6 7 8 9
// A B C
// D
-fn move_char_2(current: char, dir: char) -> char {
+fn move_char_hex(current: i32, dir: char) -> i32 {
match dir {
'U' => match current {
- '1'|'2'|'4'|'5'|'9' => current,
- '3' => '1',
- '6' => '2',
- '7' => '3',
- '8' => '4',
- 'A' => '6',
- 'B' => '7',
- 'C' => '8',
- 'D' => 'B',
- _ => panic!("Bad current char")
+ 1|2|4|5|9 => current,
+ 3|13 => current - 2,
+ _ => current - 4
},
'D' => match current {
- 'A'|'D'|'C'|'5'|'9' => current,
- '1' => '3',
- '2' => '6',
- '3' => '7',
- '4' => '8',
- '6' => 'A',
- '7' => 'B',
- '8' => 'C',
- 'B' => 'D',
- _ => panic!("Bad current char")
+ 5|10|13|12|9 => current,
+ 1|11 => current + 1,
+ _ => current + 4
},
'L' => match current {
- '1'|'2'|'5'|'A'|'D' => current,
- '3' => '2',
- '4' => '3',
- '6' => '5',
- '7' => '6',
- '8' => '7',
- '9' => '8',
- 'B' => 'A',
- 'C' => 'B',
- _ => panic!("Bad current char")
+ 1|2|5|10|13 => current,
+ _ => current - 1
},
'R' => match current {
- '1'|'4'|'9'|'C'|'D' => current,
- '2' => '3',
- '3' => '4',
- '5' => '6',
- '6' => '7',
- '7' => '8',
- '8' => '9',
- 'A' => 'B',
- 'B' => 'C',
- _ => panic!("Bad current char")
+ 1|4|9|12|13 => current,
+ _ => current + 1
},
_ => panic!("Bad direction character")
}