-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.rs
68 lines (58 loc) · 1.81 KB
/
part1.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
// adventofcode - day 8
// part 1
use std::io::prelude::*;
use std::fs::File;
fn main(){
println!("Advent of Code - day 8 | part 1");
// import data
let data = import_data();
// logic is, that we don't compute the length of either of the strings,
// however, we simply calculate differences on the fly
// i.e. if we discover a '\' we know that this is one character "too much"
// -> we simply increment our counter ;)
let mut total_char_diff = 0u32;
for line in data.lines() {
// substract first and last "
let mut line_char_diff = 2u32;
let mut skip = 0u8;
let mut last_char = '\x00';
for ch in line.chars(){
match ch {
'\\' => {
if skip > 0 {
skip -= 1;
} else {
line_char_diff += 1;
skip = 1;
}
},
// we only consider \x++ not \xx+
'x' if skip == 1 && last_char == '\\' => {
line_char_diff += 2;
skip = 2;
},
_ => {
if skip > 0 {
skip -= 1;
}
},
}
last_char = ch;
}
total_char_diff += line_char_diff;
}
println!("Total difference: {} chars", total_char_diff);
}
// This function simply imports the data set from a file called input.txt
fn import_data() -> String {
let mut file = match File::open("../../inputs/08.txt") {
Ok(f) => f,
Err(e) => panic!("file error: {}", e),
};
let mut data = String::new();
match file.read_to_string(&mut data){
Ok(_) => {},
Err(e) => panic!("file error: {}", e),
};
data
}