-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2020day2more.rs
72 lines (66 loc) · 2.09 KB
/
aoc2020day2more.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
use std::collections::VecDeque;
use std::io;
struct Scan {
buffer: std::collections::VecDeque<String>,
}
impl Scan {
fn new() -> Scan {
Scan {
buffer: VecDeque::new(),
}
}
fn next_line(&self) -> io::Result<String> {
let mut line = String::new();
match std::io::stdin().read_line(&mut line)? {
0 => Err(io::Error::new(io::ErrorKind::Other, "EOF")),
_ => Ok(line),
}
}
fn next<T: std::str::FromStr>(&mut self) -> io::Result<T> {
loop {
if let Some(token) = self.buffer.pop_front() {
break match token.parse::<T>() {
Ok(x) => Ok(x),
_ => Err(io::Error::new(io::ErrorKind::Other, "parsing is fucked")),
};
}
let line = self.next_line()?;
self.buffer = line.split_whitespace().map(String::from).collect();
}
}
}
fn _main() -> io::Result<()> {
let mut scan = Scan::new();
let mut result = 0usize;
loop {
let policy: String = match scan.next() {
Ok(x) => x,
_ => break,
};
let separator = policy.find('-').unwrap();
let pol1: isize = (&policy[0..separator]).parse::<isize>().unwrap() - 1isize;
let pol2: isize = (&policy[(separator + 1)..]).parse::<isize>().unwrap() - 1isize;
let _letter: String = scan.next().unwrap();
let letter: char = _letter.chars().next().unwrap();
let _pass: String = scan.next().unwrap();
let pass: Vec<char> = _pass.chars().collect();
let pol1valid: bool =
(pol1 as usize) < pass.len() && pol1 >= 0 && pass[pol1 as usize] == letter;
let pol2valid: bool =
(pol2 as usize) < pass.len() && pol2 >= 0 && pass[pol2 as usize] == letter;
if pol1valid ^ pol2valid {
result += 1;
}
}
println!("{}", result);
Ok(())
}
fn main() -> io::Result<()> {
std::thread::Builder::new()
.stack_size(1 << 23)
.spawn(_main)
.unwrap()
.join()
.unwrap()?;
Ok(())
}