cleanup unused
This commit is contained in:
parent
64623b0446
commit
2060da287f
1 changed files with 39 additions and 39 deletions
|
@ -1,20 +1,15 @@
|
||||||
use aoc23::prelude::*;
|
use aoc23::prelude::*;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use std::{
|
use std::{
|
||||||
collections::{
|
collections::{HashMap, VecDeque},
|
||||||
HashMap,
|
|
||||||
VecDeque,
|
|
||||||
},
|
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Card {
|
struct Card {
|
||||||
id: usize,
|
id: usize,
|
||||||
winning_numbers: Vec<usize>,
|
part_2_matches: usize,
|
||||||
numbers: Vec<usize>,
|
part_1_score: usize,
|
||||||
matches: usize,
|
|
||||||
score: usize,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Card {
|
impl FromStr for Card {
|
||||||
|
@ -23,61 +18,66 @@ impl FromStr for Card {
|
||||||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
|
||||||
let mut id: Option<usize> = None;
|
let mut id: Option<usize> = None;
|
||||||
let mut winning_numbers: Vec<usize> = vec![];
|
let mut winning_numbers: Vec<usize> = vec![];
|
||||||
let mut numbers: Vec<usize> = vec![];
|
|
||||||
let mut score = 0;
|
let mut score = 0;
|
||||||
let mut matches = 0;
|
let mut matches = 0;
|
||||||
for part in s.split(':') {
|
for part in s.split(':') {
|
||||||
if id.is_some() {
|
if id.is_some() {
|
||||||
for (num_mode, wins_or_nums) in part.split('|').enumerate() {
|
for (num_mode, wins_or_nums) in part.split('|').enumerate() {
|
||||||
wins_or_nums.trim().split_whitespace().into_iter().map(|num| {
|
wins_or_nums
|
||||||
num.parse::<usize>().expect(&format!("could not parse number: {}", num))
|
.trim()
|
||||||
}).for_each(|num| {
|
.split_whitespace()
|
||||||
if num_mode == 0 {
|
.into_iter()
|
||||||
winning_numbers.push(num);
|
.map(|num| {
|
||||||
} else {
|
num.parse::<usize>()
|
||||||
numbers.push(num);
|
.expect(&format!("could not parse number: {}", num))
|
||||||
if winning_numbers.iter().any(|winner| winner == &num) {
|
})
|
||||||
matches += 1;
|
.for_each(|num| {
|
||||||
score = if score == 0 {
|
if num_mode == 0 {
|
||||||
1
|
winning_numbers.push(num);
|
||||||
} else {
|
} else {
|
||||||
score * 2
|
if winning_numbers.iter().any(|winner| winner == &num) {
|
||||||
};
|
matches += 1;
|
||||||
|
score = if score == 0 { 1 } else { score * 2 };
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
id = Some(part.split_whitespace().last().ok_or("Failed to get last item")?.parse()?)
|
id = Some(
|
||||||
|
part.split_whitespace()
|
||||||
|
.last()
|
||||||
|
.ok_or("Failed to get last item")?
|
||||||
|
.parse()?,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Card {
|
Ok(Card {
|
||||||
id: id.ok_or("no id found")?,
|
id: id.ok_or("no id found")?,
|
||||||
winning_numbers,
|
part_1_score: score,
|
||||||
numbers,
|
part_2_matches: matches,
|
||||||
score,
|
|
||||||
matches,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part1(input: String) -> Result<usize> {
|
fn part1(input: String) -> Result<usize> {
|
||||||
Ok(
|
Ok(input
|
||||||
input
|
.lines()
|
||||||
.lines()
|
.map(|line| line.parse::<Card>())
|
||||||
.map(|line| line.parse::<Card>())
|
.filter_map(|card| card.ok())
|
||||||
.filter_map(|card| card.ok())
|
.fold(0, |sum, card| sum + card.part_1_score))
|
||||||
.fold(0, |sum, card| sum + card.score),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part2(input: String) -> Result<usize> {
|
fn part2(input: String) -> Result<usize> {
|
||||||
let mut sum = 0;
|
let mut sum = 0;
|
||||||
let cards = input.lines().map(|line| line.parse::<Card>()).filter_map(|card| card.ok()).collect_vec();
|
let cards = input
|
||||||
|
.lines()
|
||||||
|
.map(|line| line.parse::<Card>())
|
||||||
|
.filter_map(|card| card.ok())
|
||||||
|
.collect_vec();
|
||||||
let mut queue: VecDeque<&Card> = cards.iter().collect();
|
let mut queue: VecDeque<&Card> = cards.iter().collect();
|
||||||
while let Some(card) = queue.pop_front() {
|
while let Some(card) = queue.pop_front() {
|
||||||
sum += 1;
|
sum += 1;
|
||||||
for card_index in card.id .. card.id + card.matches {
|
for card_index in card.id..card.id + card.part_2_matches {
|
||||||
cards.get(card_index).map(|c| queue.push_back(&c));
|
cards.get(card_index).map(|c| queue.push_back(&c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue