-
Notifications
You must be signed in to change notification settings - Fork 0
/
NOTES.txt
33 lines (27 loc) · 1.02 KB
/
NOTES.txt
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
Parse a string with numbers into ints:
let parts: Vec<usize> = line
.split_whitespace()
.map(|x| x.parse::<usize>().unwrap())
.collect();
Parse a whole line like "Card 1: 17 1 24 40 8 | 55 27 89 9 64"
fn parse(line: &str) -> Self {
let (card, (winners, have)) = line
.strip_prefix("Card ")
.and_then(|s| s.split_once(':'))
.and_then(|(card, rest)| Option::Some((card, rest.split_once('|').unwrap())))
.unwrap();
Self {
number: card.parse::<usize>().unwrap(),
winners: winners
.split_whitespace()
.map(|x| x.parse::<usize>().unwrap())
.collect(),
have: have
.split_whitespace()
.map(|x| x.parse::<usize>().unwrap())
.collect(),
}
}
Find an element of a vector that matches a condition:
let mapping: &ElementMapping =
mappings.iter().find(|&m| m.from == elem).unwrap();