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
|
#![allow(dead_code)]
use std::fs;
use std::collections::HashSet;
use regex::Regex;
fn read_file(file: &str) -> String {
fs::read_to_string(file).unwrap()
}
fn read_lines(file: &str) -> Vec<String> {
read_file(file).split('\n')
.filter(|x| !x.is_empty())
.map(String::from)
.collect()
}
fn read_numbers(file: &str) -> Vec<i32> {
read_file(file).split('\n')
.filter(|x| !x.is_empty())
.map(|x| x.parse::<i32>().unwrap())
.collect()
}
fn find_pair_with_sum(numbers: &HashSet<i32>, goal: i32) -> Option<(i32, i32)> {
for i in numbers {
if numbers.contains(&(goal - i)) {
return Some((*i, goal - i));
}
}
None
}
fn day1() {
let input = read_numbers("input01");
let mut numbers = HashSet::new();
for i in &input {
numbers.insert(*i);
}
let (x, y) = match find_pair_with_sum(&numbers, 2020) {
Some(pair) => pair,
None => panic!("nothing found"),
};
println!("1a: {}", x * y);
for i in &numbers {
let (x, y) = match find_pair_with_sum(&numbers, 2020 - i) {
Some(pair) => pair,
None => continue,
};
println!("1b: {}", i * x * y);
break;
}
}
struct PasswordEntry {
min : usize,
max : usize,
character : char,
password : String,
}
impl PasswordEntry {
fn new(input: &str) -> PasswordEntry {
let re = Regex::new(r"^([0-9]+)-([0-9]+) ([a-z]): ([a-z]+)$").unwrap();
let caps = re.captures(&input).unwrap();
PasswordEntry {
min : caps[1].parse::<usize>().unwrap(),
max : caps[2].parse::<usize>().unwrap(),
character : caps[3].parse::<char>().unwrap(),
password : caps[4].to_string(),
}
}
fn is_valid(&self) -> bool {
let count = self.password.chars().filter(|x| *x == self.character).count();
count >= self.min && count <= self.max
}
fn is_valid_new(&self) -> bool {
let c1 = self.password.chars().nth(self.min-1).unwrap();
let c2 = self.password.chars().nth(self.max-1).unwrap();
c1 != c2 && (c1 == self.character || c2 == self.character)
}
}
fn day2() {
let input : Vec<PasswordEntry> = read_lines("input02")
.iter()
.map(|x| PasswordEntry::new(&x))
.collect();
let count = input.iter()
.filter(|x| x.is_valid())
.count();
println!("2a: {}", count);
let count = input.iter()
.filter(|x| x.is_valid_new())
.count();
println!("2b: {}", count);
}
fn main() {
day2();
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_day1() {
let input = [1721, 979, 366, 299, 675, 1456];
let mut numbers = HashSet::new();
for i in &input {
numbers.insert(*i);
}
let (x, y) = find_pair_with_sum(&numbers, 2020).unwrap();
assert_eq!(x * y, 514579);
for i in &numbers {
let (x, y) = match find_pair_with_sum(&numbers, 2020 - i) {
Some(pair) => pair,
None => continue,
};
assert_eq!(i + x + y, 2020);
assert_eq!(i * x * y, 241861950);
break;
}
}
#[test]
fn test_day2() {
let lines = ["1-3 a: abcde",
"1-3 b: cdefg",
"2-9 c: ccccccccc"];
let entries : Vec<PasswordEntry> = lines.iter().map(|x| PasswordEntry::new(&x)).collect();
assert_eq!(entries[0].is_valid(), true);
assert_eq!(entries[1].is_valid(), false);
assert_eq!(entries[2].is_valid(), true);
assert_eq!(entries[0].is_valid_new(), true);
assert_eq!(entries[1].is_valid_new(), false);
assert_eq!(entries[2].is_valid_new(), false);
}
}
|