2015/08-10, 2015/12-13, started on 2015/14
This commit is contained in:
@@ -6,20 +6,22 @@ use std::str;
|
||||
enum StateMachine {
|
||||
Normal,
|
||||
Escape,
|
||||
Hexadecimal(u8)
|
||||
Hexadecimal(u8),
|
||||
}
|
||||
|
||||
fn code_length(input: &str) -> usize {
|
||||
input.len()
|
||||
}
|
||||
|
||||
fn memory_length(input: &str) -> usize {
|
||||
let mut count: usize = 0;
|
||||
let mut count = 0;
|
||||
|
||||
let mut state = StateMachine::Normal;
|
||||
|
||||
for c in input.chars() {
|
||||
state = match (c, state) {
|
||||
// Normal.
|
||||
('\\', StateMachine::Normal) => {
|
||||
StateMachine::Escape
|
||||
}
|
||||
('\\', StateMachine::Normal) => StateMachine::Escape,
|
||||
(_, s @ StateMachine::Normal) => {
|
||||
count += 1;
|
||||
|
||||
@@ -27,9 +29,7 @@ fn memory_length(input: &str) -> usize {
|
||||
}
|
||||
|
||||
// Escape.
|
||||
('x', StateMachine::Escape) => {
|
||||
StateMachine::Hexadecimal(1)
|
||||
}
|
||||
('x', StateMachine::Escape) => StateMachine::Hexadecimal(1),
|
||||
(_, StateMachine::Escape) => {
|
||||
count += 1;
|
||||
|
||||
@@ -37,52 +37,57 @@ fn memory_length(input: &str) -> usize {
|
||||
}
|
||||
|
||||
// Hexadecimal.
|
||||
(_, StateMachine::Hexadecimal(mut hex)) => {
|
||||
if hex == 2 {
|
||||
count += 1;
|
||||
(_, StateMachine::Hexadecimal(mut hex)) => if hex == 2 {
|
||||
count += 1;
|
||||
|
||||
StateMachine::Normal
|
||||
} else {
|
||||
hex += 1;
|
||||
StateMachine::Normal
|
||||
} else {
|
||||
hex += 1;
|
||||
|
||||
StateMachine::Hexadecimal(hex)
|
||||
}
|
||||
}
|
||||
StateMachine::Hexadecimal(hex)
|
||||
},
|
||||
};
|
||||
|
||||
// println!("{} - {} => {:?}", count, c, state);
|
||||
}
|
||||
|
||||
count - 2
|
||||
}
|
||||
|
||||
fn code_length(input: &str) -> usize {
|
||||
input.len()
|
||||
fn string_length(input: &str) -> usize {
|
||||
let mut count = 0;
|
||||
|
||||
for c in input.chars() {
|
||||
count += match c {
|
||||
'"' => 2,
|
||||
'\\' => 2,
|
||||
_ => 1,
|
||||
};
|
||||
}
|
||||
|
||||
count + 2
|
||||
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let stdin = io::stdin();
|
||||
|
||||
let lines = stdin.lock().lines()
|
||||
let lines = stdin
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(|line| line.ok())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let count_1: usize = lines.iter()
|
||||
.map(|line| code_length(&line))
|
||||
.sum();
|
||||
let code: usize = lines.iter().map(|line| code_length(line)).sum();
|
||||
let memory: usize = lines.iter().map(|line| memory_length(line)).sum();
|
||||
let string: usize = lines.iter().map(|line| string_length(line)).sum();
|
||||
|
||||
let count_2: usize = lines.iter()
|
||||
.map(|line| memory_length(&line))
|
||||
.sum();
|
||||
|
||||
println!("count_1={}", count_1);
|
||||
println!("count_2={}", count_1 - count_2);
|
||||
println!("part_one={}", code - memory);
|
||||
println!("part_two={}", string - code);
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{code_length, memory_length};
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn example_01() {
|
||||
@@ -115,4 +120,32 @@ mod tests {
|
||||
assert_eq!(6, code_length(line));
|
||||
assert_eq!(1, memory_length(line));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_05() {
|
||||
let line = r#""""#;
|
||||
|
||||
assert_eq!(6, string_length(line));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_06() {
|
||||
let line = r#""abc""#;
|
||||
|
||||
assert_eq!(9, string_length(line));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_07() {
|
||||
let line = r#""aaa\"aaa""#;
|
||||
|
||||
assert_eq!(16, string_length(line));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example_08() {
|
||||
let line = r#""\x27""#;
|
||||
|
||||
assert_eq!(11, string_length(line));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user