mirror of
https://github.com/anthonyoteri/advent-of-code-2023.git
synced 2026-06-05 19:46:54 -04:00
+57
-7
@@ -1,3 +1,5 @@
|
|||||||
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
||||||
struct Card {
|
struct Card {
|
||||||
id: usize,
|
id: usize,
|
||||||
@@ -13,12 +15,15 @@ impl Card {
|
|||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
count
|
||||||
|
}
|
||||||
|
|
||||||
let base: usize = 2;
|
fn value(&self) -> usize {
|
||||||
if count == 0 {
|
let num_wins = self.winner();
|
||||||
|
if num_wins == 0 {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
base.pow(count - 1)
|
2_usize.pow(num_wins as u32 - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,15 +58,41 @@ fn parse_input(input: &str) -> Vec<Card> {
|
|||||||
cards
|
cards
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn make_pass(input: &Vec<Card>, cards: &BTreeMap<usize, Card>) -> Vec<Card> {
|
||||||
|
let mut output = Vec::new();
|
||||||
|
for card in input {
|
||||||
|
let win_count = card.winner();
|
||||||
|
for j in card.id + 1..card.id + win_count + 1 {
|
||||||
|
output.push(cards.get(&j).unwrap().clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
fn part_1(input: &str) -> usize {
|
fn part_1(input: &str) -> usize {
|
||||||
let cards = parse_input(input);
|
let cards = parse_input(input);
|
||||||
|
|
||||||
let total: usize = cards.iter().map(|c| c.winner()).sum();
|
let total: usize = cards.iter().map(|c| c.value()).sum();
|
||||||
total
|
total
|
||||||
}
|
}
|
||||||
|
|
||||||
fn part_2(_input: &str) -> u32 {
|
fn part_2(input: &str) -> usize {
|
||||||
0
|
let mut cards = parse_input(input);
|
||||||
|
let cards_by_id = BTreeMap::from_iter(cards.iter().map(|c| (c.id, c.clone())));
|
||||||
|
|
||||||
|
let mut result = Vec::new();
|
||||||
|
result.append(&mut cards.clone());
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let pass = make_pass(&cards, &cards_by_id);
|
||||||
|
if pass.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result.append(&mut pass.clone());
|
||||||
|
cards = pass;
|
||||||
|
}
|
||||||
|
|
||||||
|
result.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -82,12 +113,31 @@ mod test {
|
|||||||
game_numbers: vec![83, 86, 6, 31, 17, 9, 48, 53],
|
game_numbers: vec![83, 86, 6, 31, 17, 9, 48, 53],
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(card.winner(), 8)
|
assert_eq!(card.winner(), 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_value() {
|
||||||
|
let card = Card {
|
||||||
|
id: 1,
|
||||||
|
winning_numbers: vec![41, 48, 83, 86, 17],
|
||||||
|
game_numbers: vec![83, 86, 6, 31, 17, 9, 48, 53],
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(card.value(), 8);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part_1() {
|
fn test_part_1() {
|
||||||
let input = include_str!("../test-input.txt");
|
let input = include_str!("../test-input.txt");
|
||||||
|
|
||||||
assert_eq!(part_1(input), 13);
|
assert_eq!(part_1(input), 13);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_part_2() {
|
||||||
|
let input = include_str!("../test-input.txt");
|
||||||
|
|
||||||
|
assert_eq!(part_2(input), 30);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user