-
Notifications
You must be signed in to change notification settings - Fork 0
/
rst
63 lines (46 loc) · 1.75 KB
/
rst
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::env;
use std::fs::File;
use std::io::{self, BufRead};
fn read_lines_into_vector(file_path: &str) -> Result<Vec<String>, io::Error> {
let file = File::open(file_path)?;
let reader = io::BufReader::new(file);
let mut lines = Vec::new();
for line in reader.lines() {
lines.push(line?);
}
Ok(lines)
}
fn get_card_matches(card_nums: &str, winning_nums: &str) -> u32 {
let card_nums: Vec<&str> = card_nums.split_whitespace().collect();
let winning_nums: Vec<&str> = winning_nums.split_whitespace().collect();
let mut matches: u32 = 0;
for num in card_nums {
if winning_nums.contains(&num) {
matches += 1;
}
}
return matches;
}
fn get_num_cards(indices: &Vec<usize>, all_cards: &Vec<String>,) -> u32 {
let mut num_cards: u32 = 0;
for idx in indices {
let card = &all_cards[*idx];
let card: Vec<&str> = card.split(":").collect();
let card_num: u32 = card[0].split_whitespace().nth(1).unwrap().parse().unwrap();
let nums: Vec<&str> = card.get(1).unwrap().split("|").collect();
let matches: u32 = get_card_matches(nums[0], nums[1]);
num_cards += matches;
let new_indices: Vec<usize> = (card_num as usize ..(card_num as usize +matches as usize)).collect();
num_cards += get_num_cards(&new_indices, all_cards);
}
return num_cards
}
fn main() {
let args: Vec<String> = env::args().collect();
let lines: Vec<String> = match args.get(1) {
Some(fp) => read_lines_into_vector(&fp).unwrap(),
None => panic!("Expected file path of input!"),
};
let initial_idx: Vec<usize> = (0..lines.len()).collect();
println!("{}", lines.len() as u32 + get_num_cards(&initial_idx, &lines))
}