2016/02
This commit is contained in:
144
2016/02/src/main.rs
Normal file
144
2016/02/src/main.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
use std::io::{self, Read};
|
||||
|
||||
|
||||
fn main() {
|
||||
let mut buffer = String::new();
|
||||
|
||||
io::stdin().read_to_string(&mut buffer).unwrap();
|
||||
|
||||
let code = process_2(&buffer);
|
||||
|
||||
println!("code={}", code);
|
||||
}
|
||||
|
||||
fn process_1(input: &str) -> String {
|
||||
let mut code = Vec::new();
|
||||
let mut state = '5';
|
||||
|
||||
for line in input.lines() {
|
||||
for ch in line.chars() {
|
||||
state = match (ch, state) {
|
||||
('R', '1') => '2',
|
||||
('D', '1') => '4',
|
||||
|
||||
('R', '2') => '3',
|
||||
('L', '2') => '1',
|
||||
('D', '2') => '5',
|
||||
|
||||
('L', '3') => '2',
|
||||
('D', '3') => '6',
|
||||
|
||||
('R', '4') => '5',
|
||||
('U', '4') => '1',
|
||||
('D', '4') => '7',
|
||||
|
||||
('R', '5') => '6',
|
||||
('L', '5') => '4',
|
||||
('U', '5') => '2',
|
||||
('D', '5') => '8',
|
||||
|
||||
('L', '6') => '5',
|
||||
('U', '6') => '3',
|
||||
('D', '6') => '9',
|
||||
|
||||
('R', '7') => '8',
|
||||
('U', '7') => '4',
|
||||
|
||||
('R', '8') => '9',
|
||||
('L', '8') => '7',
|
||||
('U', '8') => '5',
|
||||
|
||||
('L', '9') => '8',
|
||||
('U', '9') => '6',
|
||||
|
||||
_ => state,
|
||||
};
|
||||
}
|
||||
|
||||
code.push(state);
|
||||
}
|
||||
|
||||
code.iter().map(|x| x.to_string()).collect::<Vec<_>>().join("")
|
||||
}
|
||||
|
||||
fn process_2(input: &str) -> String {
|
||||
let mut code = Vec::new();
|
||||
let mut state = '5';
|
||||
|
||||
for line in input.lines() {
|
||||
for ch in line.chars() {
|
||||
state = match (ch, state) {
|
||||
('D', '1') => '3',
|
||||
|
||||
('R', '2') => '3',
|
||||
('D', '2') => '6',
|
||||
|
||||
('R', '3') => '4',
|
||||
('L', '3') => '2',
|
||||
('U', '3') => '1',
|
||||
('D', '3') => '7',
|
||||
|
||||
('L', '4') => '3',
|
||||
('D', '4') => '8',
|
||||
|
||||
('R', '5') => '6',
|
||||
|
||||
('R', '6') => '7',
|
||||
('L', '6') => '5',
|
||||
('U', '6') => '2',
|
||||
('D', '6') => 'A',
|
||||
|
||||
('R', '7') => '8',
|
||||
('L', '7') => '6',
|
||||
('U', '7') => '3',
|
||||
('D', '7') => 'B',
|
||||
|
||||
('R', '8') => '9',
|
||||
('L', '8') => '7',
|
||||
('U', '8') => '4',
|
||||
('D', '8') => 'C',
|
||||
|
||||
('L', '9') => '8',
|
||||
|
||||
('R', 'A') => 'B',
|
||||
('U', 'A') => '6',
|
||||
|
||||
('R', 'B') => 'C',
|
||||
('L', 'B') => 'A',
|
||||
('U', 'B') => '7',
|
||||
('D', 'B') => 'D',
|
||||
|
||||
('L', 'C') => 'B',
|
||||
('U', 'C') => '8',
|
||||
|
||||
('U', 'D') => 'B',
|
||||
|
||||
_ => state,
|
||||
};
|
||||
}
|
||||
|
||||
code.push(state);
|
||||
}
|
||||
|
||||
code.iter().map(|x| x.to_string()).collect::<Vec<_>>().join("")
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{process_1, process_2};
|
||||
|
||||
#[test]
|
||||
fn example_01() {
|
||||
let input = "ULL\nRRDDD\nLURDL\nUUUUD";
|
||||
|
||||
assert_eq!("1985", process_1(input));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_02() {
|
||||
let input = "ULL\nRRDDD\nLURDL\nUUUUD";
|
||||
|
||||
assert_eq!("5DB3", process_2(input));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user