-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
72 lines (60 loc) · 1.6 KB
/
main.rs
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
62
63
64
65
66
67
68
69
70
71
72
#![warn(clippy::all, clippy::pedantic)]
pub fn main() {
let data = include_str!("input.txt");
println!("Part 1: {}", part_one(data));
println!("Part 2: {}", part_two(data));
}
fn part_one(data: &str) -> usize {
data.lines().map(read_line).map(|(m, c)| m - c).sum()
}
fn part_two(data: &str) -> usize {
data.lines().map(encode_line).map(|(m, e)| e - m).sum()
}
fn read_line(line: &str) -> (usize, usize) {
let mem_len = line.as_bytes().len();
let mut char_len = 0;
let mut string = &line.as_bytes()[1..mem_len - 1];
while !string.is_empty() {
let (ch, rest) = string.split_first().unwrap();
if let b'\\' = ch {
match rest.split_first().unwrap() {
(b'"' | b'\\', _) => {
string = &rest[1..];
}
(b'x', _) => {
string = &rest[3..];
}
_ => unreachable!(),
}
} else {
string = rest;
}
char_len += 1;
}
(mem_len, char_len)
}
fn encode_line(line: &str) -> (usize, usize) {
let mem_len = line.as_bytes().len();
let enc_len = mem_len
+ 2
+ line
.as_bytes()
.iter()
.filter(|&ch| ch == &b'"' || ch == &b'\\')
.count();
(mem_len, enc_len)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn one() {
let data = include_str!("test.txt");
assert_eq!(12, part_one(data));
}
#[test]
fn two() {
let data = include_str!("test.txt");
assert_eq!(19, part_two(data));
}
}