[rust][8.1]

wip
Alpha Chen 9 years ago
parent 34dfdf0131
commit 7d39af4bc0

@ -1,14 +1,14 @@
#[test] #[test]
fn test_day_08() { fn test_day_08() {
assert_eq!(2, solve(r#""""#)); assert_eq!(4, solve(r#""""#));
assert_eq!(2, solve(r#""abc""#)); assert_eq!(4, solve(r#""abc""#));
assert_eq!(3, solve(r#""aaa\"aaa""#)); assert_eq!(6, solve(r#""aaa\"aaa""#));
assert_eq!(5, solve(r#""\x27""#)); assert_eq!(5, solve(r#""\x27""#));
assert_eq!(5, solve(r#""\xfa""#)); assert_eq!(5, solve(r#""\xfa""#));
} }
pub fn solve(input: &str) -> usize { pub fn solve(input: &str) -> usize {
input.lines().fold(0, |sum, s| sum + s.len() - decode(s).chars().count()) input.lines().fold(0, |sum, s| sum + encode(s).chars().count() - s.len())
} }
#[test] #[test]
@ -44,3 +44,16 @@ fn decode(string: &str) -> String {
} }
out out
} }
#[test]
fn test_encode() {
assert_eq!(r#""\"\"""#, encode(r#""""#));
assert_eq!(r#""\"abc\"""#, encode(r#""abc""#));
assert_eq!(r#""\"aaa\\\"aaa\"""#, encode(r#""aaa\"aaa""#));
assert_eq!(r#""\"\\x27\"""#, encode(r#""\x27""#));
assert_eq!(r#""\"\\xfa\"""#, encode(r#""\xfa""#));
}
fn encode(string: &str) -> String {
format!(r#""{}""#, string.replace(r#"\"#, r#"\\"#).replace(r#"""#, r#"\""#))
}

Loading…
Cancel
Save