-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
52 lines (37 loc) · 1.79 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
use regex::Regex;
use std::fs;
fn main() {
let contents = fs::read_to_string("input.txt").unwrap();
//let contents = fs::read_to_string("example.txt").unwrap();
let re = Regex::new(r"\\x[0-9a-fA-F]{2}").unwrap();
let mut code_chars = 0;
let mut memory_chars = 0;
//For part 2
let mut encoded_chars = 0;
for line in contents.lines() {
println!("{}", line);
let tmp_line = String::from(line);
let mut stripped = String::from(tmp_line.strip_prefix('"').unwrap());
stripped = String::from(stripped.strip_suffix('"').unwrap());
let count_slash = stripped.matches("\\\\").count();
let count_quote = stripped.matches("\\\"").count();
let count_hex = re.find_iter(line).count();
//count_quote -> +2 char per count_quote
//count_slash -> +2 chars per count_slash
//count_hex -> +1 char per count_hex
//+4 for end and start quote
// For debugging
//let tmp_memory_chars = line.len() - count_slash - count_quote - 3 * count_hex - 2;
let tmp_encoded_chars = line.len() + 2 * count_quote + 2 * count_slash + count_hex + 4;
println!("{}\n", tmp_encoded_chars);
code_chars += line.len();
memory_chars += line.len() - count_slash - count_quote - 3 * count_hex - 2;
encoded_chars += line.len() + 2 * count_quote + 2 * count_slash + count_hex + 4;
//println!("{} {} {} {}", stripped, count_slash, count_quote, count_hex);
//println!("{} {}\n", line.len(), tmp_memory_chars);
}
println!("Code chars: {}; Memory chars: {}", code_chars, memory_chars);
println!("Answer: {}", code_chars - memory_chars);
println!("Code chars: {}; Encoded chars: {}", code_chars, encoded_chars);
println!("Answer: {}", encoded_chars - code_chars);
}