-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday08.rs
More file actions
36 lines (29 loc) · 819 Bytes
/
Copy pathday08.rs
File metadata and controls
36 lines (29 loc) · 819 Bytes
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
//! Matchsticks
//!
//! Summary:
use regex::Regex;
pub fn parse(input: &str) -> &str {
input.trim()
}
pub fn part1(input: &str) -> usize {
solve(input, false)
}
pub fn part2(input: &str) -> usize {
solve(input, true)
}
fn solve(input: &str, part2: bool) -> usize {
let mut code_len = 0;
let mut render_len = 0;
let mut encoded_len = 0;
let re: Regex = Regex::new(r"\\x[a-f0-9A-F]{2}").unwrap();
for line in input.lines() {
// println!("{}", line);
code_len += line.len();
render_len += re.replace_all(line, "x").replace("\\\\", "x").replace("\\\"", "x").len() - 2;
encoded_len += line.len() + line.matches("\\").count() + line.matches("\"").count() + 2;
}
if part2 {
return encoded_len - code_len;
}
code_len - render_len
}