-
Notifications
You must be signed in to change notification settings - Fork 3
/
lib.rs
214 lines (190 loc) · 5.03 KB
/
lib.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
extern crate core;
use crate::Type::{
Akitas, Cars, Cats, Children, Goldfish, Perfumes, Pomeranians, Samoyeds, Trees, Vizslas,
};
use std::collections::HashMap;
pub fn fn1(input: &str, m: HashMap<Type, i32>) -> i32 {
let lines: Vec<_> = input.lines().collect();
let mut sues = Vec::new();
for s in lines.iter() {
sues.push(Sue::new(s));
}
for sue in sues.iter() {
let mut found = true;
for (t, value) in m.iter() {
if let Some(v) = sue.m.get(t) {
if v != value {
found = false;
break;
}
}
}
if found {
return sue.id;
}
}
-1
}
#[derive(Debug)]
struct Sue {
id: i32,
m: HashMap<Type, i32>,
}
impl Sue {
fn new(s: &str) -> Self {
let idx = index_all(s, " ");
let v = &s[idx[0] + 1..idx[1] - 1];
let id = v.parse::<i32>().unwrap();
let mut m = HashMap::new();
if let Some(t) = find(s, "children: ") {
m.insert(Children, t);
}
if let Some(t) = find(s, "cats: ") {
m.insert(Cats, t);
}
if let Some(t) = find(s, "samoyeds: ") {
m.insert(Samoyeds, t);
}
if let Some(t) = find(s, "pomeranians: ") {
m.insert(Pomeranians, t);
}
if let Some(t) = find(s, "akitas: ") {
m.insert(Akitas, t);
}
if let Some(t) = find(s, "vizslas: ") {
m.insert(Vizslas, t);
}
if let Some(t) = find(s, "goldfish: ") {
m.insert(Goldfish, t);
}
if let Some(t) = find(s, "trees: ") {
m.insert(Trees, t);
}
if let Some(t) = find(s, "cars: ") {
m.insert(Cars, t);
}
if let Some(t) = find(s, "perfumes: ") {
m.insert(Perfumes, t);
}
Sue { id, m }
}
}
fn find(s: &str, search: &str) -> Option<i32> {
if let Some(t) = s.find(search) {
if let Some(space) = s[t + search.len()..].find(" ") {
let v = &s[t + search.len()..t + search.len() + space - 1];
return Some(v.parse::<i32>().unwrap());
} else {
let v = &s[t + search.len()..];
return Some(v.parse::<i32>().unwrap());
}
}
None
}
#[derive(Eq, Hash, PartialEq, Debug)]
pub enum Type {
Children,
Cats,
Samoyeds,
Pomeranians,
Akitas,
Vizslas,
Goldfish,
Trees,
Cars,
Perfumes,
}
pub fn fn2(input: &str, m: HashMap<Type, i32>) -> i32 {
let lines: Vec<_> = input.lines().collect();
let mut sues = Vec::new();
for s in lines.iter() {
sues.push(Sue::new(s));
}
for sue in sues.iter() {
let mut found = true;
for (t, target) in m.iter() {
if let Some(v) = sue.m.get(t) {
match t {
Cats | Trees => {
if v <= target {
found = false;
break;
}
}
Pomeranians | Goldfish => {
if v >= target {
found = false;
break;
}
}
_ => {
if v != target {
found = false;
break;
}
}
}
}
}
if found {
return sue.id;
}
}
-1
}
fn index_all(s: &str, search: &str) -> Vec<usize> {
let mut res = Vec::new();
let mut i = 0;
while i < s.len() {
let idx = s[i..].find(search);
match idx {
None => return res,
Some(t) => {
res.push(t + i);
i += t + search.len()
}
}
}
res
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_index_all() {
assert_eq!(index_all("abxxxabiiiabxx", "ab"), vec![0, 5, 10]);
}
#[test]
fn test_fn1_input() {
let mut m = HashMap::new();
m.insert(Children, 3);
m.insert(Cats, 7);
m.insert(Samoyeds, 2);
m.insert(Pomeranians, 3);
m.insert(Akitas, 0);
m.insert(Vizslas, 0);
m.insert(Goldfish, 5);
m.insert(Trees, 3);
m.insert(Cars, 2);
m.insert(Perfumes, 1);
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn1(s.as_str(), m), 373);
}
#[test]
fn test_fn2_input() {
let mut m = HashMap::new();
m.insert(Children, 3);
m.insert(Cats, 7);
m.insert(Samoyeds, 2);
m.insert(Pomeranians, 3);
m.insert(Akitas, 0);
m.insert(Vizslas, 0);
m.insert(Goldfish, 5);
m.insert(Trees, 3);
m.insert(Cars, 2);
m.insert(Perfumes, 1);
let s = fs::read_to_string("input.txt").unwrap();
assert_eq!(fn2(s.as_str(), m), 260);
}
}