-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.rs
160 lines (129 loc) · 4.07 KB
/
day2.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#![allow(unused_imports, dead_code)]
use std::io::{self, BufRead, Lines, StdinLock};
use std::convert::TryFrom;
use std::error::Error;
use std::fmt::{self, Display};
#[derive(Debug)]
struct ParsedLine(i32, i32, char, String);
impl Display for ParsedLine {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ParsedLine({}, {}, {}, {})", self.0, self.1, self.2, self.3)
}
}
/*
impl<T> Display for Vec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[");
for item in self.iter() {
write!(f, "{}", item);
}
write!(f, "]");
Ok(())
}
}
*/
#[derive(Debug)]
struct ParseLineError(String);
impl Display for ParseLineError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "ParseLineError({})", self.0)
}
}
impl Error for ParseLineError {}
fn validate_line(line: &ParsedLine) -> bool {
let ParsedLine(min, max, letter, password) = line;
let count: i32 = password.chars().fold(0, |count, c| {
count + if c == *letter { 1 } else { 0 }
});
count >= *min && count <= *max
}
fn validate_line2(line: &ParsedLine) -> bool {
let ParsedLine(num1, num2, letter, password) = line;
let password = password.chars().collect::<Vec<char>>();
let index1 = usize::try_from(*num1);
let index2 = usize::try_from(*num2);
if let (Ok(index1), Ok(index2)) = (index1, index2) {
let match1 = password[index1 - 1usize] == *letter;
let match2 = password[index2 - 1usize] == *letter;
return match1 != match2;
}
false
}
/*
fn invalid_line(line: &String) -> String {
format!("Invalid line: '{}'", line)
}
*/
fn parse_line(line: &String) -> Result<ParsedLine, Box<dyn Error>> {
let parts = line.split(": ").collect::<Vec<&str>>();
let err = || ParseLineError(line.to_string());
let left = parts.get(0).ok_or_else(err)?;
let password = parts.get(1).ok_or_else(err)?.to_string();
let parts = left.split(" ").collect::<Vec<&str>>();
let left = parts.get(0).ok_or_else(err)?;
let c = parts.get(1).ok_or_else(err)?.parse::<char>().map_err(|_| err())?;
let parts = left.split("-").collect::<Vec<&str>>();
let num1 = parts.get(0).ok_or_else(err)?.parse::<i32>().map_err(|_| err())?;
let num2 = parts.get(1).ok_or_else(err)?.parse::<i32>().map_err(|_| err())?;
Ok(ParsedLine(num1, num2, c, password))
}
fn parse_lines(lines: &Vec<String>) -> Result<Vec<ParsedLine>, Box<dyn Error>> {
lines.iter().map(parse_line).collect()
}
fn stdin_lines() -> Vec<String> {
io::stdin().lock().lines().map(|x| x.unwrap()).collect()
}
fn part1(lines: &Vec<String>) -> Result<usize, Box<dyn Error>> {
//let result = lines.iter()
/*
.map(|x| parse_line(x))
.filter(|x| {
//validate_line(&x.as_ref().unwrap()))
match x {
Some(x) => *x,
_ => false,
}
})
*/
/*
.filter_map(|x| {
//println!("filter_map: {:?}", x);
None
})
*/
/*
.map(|x| parse_line(x))
.filter(|x| validate_line(x))
.collect::<Result<Vec<ParsedLine>,Box<dyn Error>>>();
*/
Ok(parse_lines(lines)?
.iter()
.filter(|x| validate_line(x))
.count())
/*
println!("Part1: {}", match result {
Ok(answer) => answer.len().to_string(),
Err(e) => format!("{:?}", e),
});
*/
}
fn part2(lines: &Vec<String>) -> Result<usize, Box<dyn Error>> {
let result = lines.iter()
.map(|x| parse_line(x))
//.filter(|x| validate_line2(&x.as_ref().unwrap()))
.filter(|x| validate_line2(x.as_ref().unwrap()))
.collect::<Result<Vec<ParsedLine>,_>>();
/*
println!("Part2: {}", match result {
Ok(answer) => answer.len().to_string(),
Err(e) => e.to_string()
});
*/
Ok(result?.len())
}
fn main() -> Result<(), Box<dyn Error>> {
let lines = stdin_lines();
println!("Part1: {}", part1(&lines)?);
println!("Part2: {}", part2(&lines)?);
Ok(())
}