-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
63 lines (52 loc) · 1.45 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
use std::collections::HashSet;
use std::fs::File;
use std::io;
use std::io::BufReader;
use std::io::prelude::*;
fn read_file(name: &str) -> Result<Vec<String>, io::Error>
{
let file = File::open(name)?;
let buf_reader = BufReader::new(file);
return buf_reader.lines().collect();
}
fn main() -> Result<(), io::Error>
{
let data = read_file("../data.txt")?;
let data = data.join("");
let part1 = solve(&data, None).unwrap();
let chars = data.chars()
.map(|c| c.to_ascii_uppercase())
.collect::<HashSet<_>>();
let part2 = chars
.iter()
.map(|n| solve(&data, Some(*n)).unwrap())
.min()
.unwrap();
println!("Part 1: {}", part1);
println!("Part 2: {}", part2);
return Ok(());
}
fn solve(input: &String, without: Option<char>) -> Option<u64>
{
let mut stack = Vec::<char>::new();
fn chars_match(a: char, b: char) -> bool {
a != b && a.to_ascii_uppercase() == b.to_ascii_uppercase()
}
let input = match without {
Some(w) => input
.chars()
.filter(|c| c.to_ascii_uppercase() != w.to_ascii_uppercase())
.collect::<Vec<char>>(),
None => input
.chars()
.collect::<Vec<char>>()
};
for c in input {
if stack.len() > 0 && chars_match(*stack.last().unwrap(), c) {
stack.pop();
} else {
stack.push(c);
}
}
Some(stack.len() as u64)
}