rusty
This commit is contained in:
parent
6804109da6
commit
32c3671947
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -1,17 +1,22 @@
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
|
|
||||||
const DEBUG: bool = true;
|
|
||||||
|
|
||||||
struct Card {
|
struct Card {
|
||||||
card_number: u32,
|
match_count: usize,
|
||||||
match_count: u32,
|
copies: usize,
|
||||||
copies: u32,
|
}
|
||||||
|
|
||||||
|
fn get_points(match_count: usize) -> usize {
|
||||||
|
match match_count{
|
||||||
|
0 => 0,
|
||||||
|
_ => 2_usize.pow((match_count-1).try_into().unwrap()),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut lines_processed = 0;
|
let mut lines_processed = 0;
|
||||||
let mut cards: Vec<Card> = Vec::new();
|
let mut cards: Vec<Card> = Vec::new();
|
||||||
|
let mut total_points: usize = 0;
|
||||||
|
|
||||||
|
|
||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
|
|
@ -19,64 +24,48 @@ fn main() {
|
||||||
println!("input {}", line.trim());
|
println!("input {}", line.trim());
|
||||||
let card_strings = line.trim().split_once(':').unwrap();
|
let card_strings = line.trim().split_once(':').unwrap();
|
||||||
//dbg!(card_strings);
|
//dbg!(card_strings);
|
||||||
let card_number = card_strings.0[5..].trim().parse::<u32>().unwrap();
|
//let card_number: usize = card_strings.0[5..].trim().parse::<usize>().unwrap();
|
||||||
let card_data_string = card_strings.1.trim().split_once('|').unwrap();
|
let card_data_string = card_strings.1.trim().split_once('|').unwrap();
|
||||||
|
|
||||||
let winning_numbers: Vec<u32> = card_data_string.0
|
let winning_numbers: Vec<usize> = card_data_string.0
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.map(|s| s.parse().unwrap())
|
.map(|s| s.parse().unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
let my_numbers: Vec<u32> = card_data_string.1
|
let my_numbers: Vec<usize> = card_data_string.1
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.map(|s| s.parse().unwrap())
|
.map(|s| s.parse().unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
//dbg!(my_numbers.iter().filter(|x| winning_numbers.contains(x)).cloned().collect::<Vec<u32>>());
|
let match_count: usize = my_numbers.iter()
|
||||||
let match_count: u32 = my_numbers.iter().filter(|x| winning_numbers.contains(x)).count().try_into().unwrap();
|
.filter(|x| winning_numbers.contains(x))
|
||||||
|
.count().try_into().unwrap();
|
||||||
println!("------------ card_number: {}, match_count: {}\n", card_number, match_count);
|
|
||||||
|
|
||||||
|
total_points += get_points(match_count);
|
||||||
cards.push(Card {
|
cards.push(Card {
|
||||||
card_number: card_number,
|
|
||||||
match_count: match_count,
|
match_count: match_count,
|
||||||
copies: 1,
|
copies: 1,
|
||||||
});
|
});
|
||||||
|
|
||||||
for card in cards.iter() {
|
|
||||||
println!("CARD card: {}, match count: {}", card.card_number, card.match_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//dbg!(winning_numbers, my_numbers, match_count);
|
|
||||||
//.map(|x| x.parse).unwrap())
|
|
||||||
//.collect()
|
|
||||||
|
|
||||||
// dbg!( card_data_string.0
|
|
||||||
// .trim()
|
|
||||||
// .split_whitespace()
|
|
||||||
// //.split(' ').map(|s| s.trim())
|
|
||||||
// .filter(|s| !s.is_empty())
|
|
||||||
// .map(|s| s.parse().unwrap())
|
|
||||||
// .collect()
|
|
||||||
// //.map(|x| x.parse).unwrap())
|
|
||||||
// //.collect()
|
|
||||||
// );
|
|
||||||
//let winning_numbers: Vec<u32> = card_data_string.0.trim().split_whitespace().iter().flat_map(|s| s.parse().ok()).collect();
|
|
||||||
let my_numbers = card_data_string.1.trim();
|
|
||||||
|
|
||||||
dbg!(card_number);
|
|
||||||
//dbg!(winning_numbers, my_numbers);
|
|
||||||
lines_processed += 1;
|
lines_processed += 1;
|
||||||
line.clear()
|
line.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("\n Ingested {} Lines", lines_processed);
|
println!("\n Ingested {} Lines", lines_processed);
|
||||||
println!("===================================");
|
println!("===================================");
|
||||||
|
|
||||||
// Insert processing code
|
|
||||||
|
|
||||||
println!("\n Processed XXX");
|
for i in 1..cards.len() {
|
||||||
|
let current_card_match_no = cards[i-1].match_count;
|
||||||
|
let current_card_copies_no = cards[i-1].copies;
|
||||||
|
//dbg!(current_card_match_no);
|
||||||
|
for j in 1..=current_card_match_no {
|
||||||
|
cards[(i-1)+j].copies += current_card_copies_no;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let total_card_copies: usize = cards.iter().map(|x| x.copies).sum();
|
||||||
|
|
||||||
|
println!("\n Processed {} Cards", cards.len());
|
||||||
println!("===================================");
|
println!("===================================");
|
||||||
|
println!("Total Points: \n{}", total_points);
|
||||||
println!("Answer: \nXXXX");
|
println!("Total Cards: \n{}", total_card_copies);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,71 @@
|
||||||
fn main() {
|
use std::io;
|
||||||
println!("Hello, world!");
|
use std::io::BufRead;
|
||||||
|
|
||||||
|
struct Card {
|
||||||
|
match_count: usize,
|
||||||
|
copies: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_points(match_count: usize) -> usize {
|
||||||
|
match match_count{
|
||||||
|
0 => 0,
|
||||||
|
_ => 2_usize.pow((match_count-1).try_into().unwrap()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut lines_processed = 0;
|
||||||
|
let mut cards: Vec<Card> = Vec::new();
|
||||||
|
let mut total_points: usize = 0;
|
||||||
|
|
||||||
|
|
||||||
|
let mut line = String::new();
|
||||||
|
while io::stdin().lock().read_line(&mut line).unwrap() > 0 {
|
||||||
|
println!("input {}", line.trim());
|
||||||
|
let card_strings = line.trim().split_once(':').unwrap();
|
||||||
|
//dbg!(card_strings);
|
||||||
|
//let card_number: usize = card_strings.0[5..].trim().parse::<usize>().unwrap();
|
||||||
|
let card_data_string = card_strings.1.trim().split_once('|').unwrap();
|
||||||
|
|
||||||
|
let winning_numbers: Vec<usize> = card_data_string.0
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|s| s.parse().unwrap())
|
||||||
|
.collect();
|
||||||
|
let my_numbers: Vec<usize> = card_data_string.1
|
||||||
|
.split_whitespace()
|
||||||
|
.map(|s| s.parse().unwrap())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let match_count: usize = my_numbers.iter()
|
||||||
|
.filter(|x| winning_numbers.contains(x))
|
||||||
|
.count().try_into().unwrap();
|
||||||
|
|
||||||
|
total_points += get_points(match_count);
|
||||||
|
cards.push(Card {
|
||||||
|
match_count: match_count,
|
||||||
|
copies: 1,
|
||||||
|
});
|
||||||
|
lines_processed += 1;
|
||||||
|
line.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\n Ingested {} Lines", lines_processed);
|
||||||
|
println!("===================================");
|
||||||
|
|
||||||
|
|
||||||
|
for i in 1..cards.len() {
|
||||||
|
let current_card_match_no = cards[i-1].match_count;
|
||||||
|
let current_card_copies_no = cards[i-1].copies;
|
||||||
|
//dbg!(current_card_match_no);
|
||||||
|
for j in 1..=current_card_match_no {
|
||||||
|
cards[(i-1)+j].copies += current_card_copies_no;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let total_card_copies: usize = cards.iter().map(|x| x.copies).sum();
|
||||||
|
|
||||||
|
println!("\n Processed {} Cards", cards.len());
|
||||||
|
println!("===================================");
|
||||||
|
println!("Total Points: \n{}", total_points);
|
||||||
|
println!("Total Cards: \n{}", total_card_copies);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue