Day 4 part 2
This commit is contained in:
parent
65eac56f1c
commit
9491fe5d9f
@ -52,6 +52,15 @@
|
||||
//! The score of the winning board can now be calculated. Start by finding the sum of all unmarked numbers on that board; in this case, the sum is 188. Then, multiply that sum by the number that was just called when the board won, 24, to get the final score, 188 * 24 = 4512.
|
||||
//!
|
||||
//! To guarantee victory against the giant squid, figure out which board will win first. What will your final score be if you choose that board?
|
||||
//!
|
||||
//! --- Part Two ---
|
||||
//! On the other hand, it might be wise to try a different strategy: let the giant squid win.
|
||||
//!
|
||||
//! You aren't sure how many bingo boards a giant squid could play at once, so rather than waste time counting its arms, the safe thing to do is to figure out which board will win last and choose that one. That way, no matter which boards it picks, it will win for sure.
|
||||
//!
|
||||
//! In the above example, the second board is the last to win, which happens after 13 is eventually called and its middle column is completely marked. If you were to keep playing until this point, the second board would have a sum of unmarked numbers equal to 148 for a final score of 148 * 13 = 1924.
|
||||
//!
|
||||
//! Figure out which board will win last. Once it wins, what would its final score be?
|
||||
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
@ -62,13 +71,14 @@ use std::{
|
||||
|
||||
use ansi_term::Color::Green;
|
||||
use anyhow::Result;
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
use aoc_runner_derive::aoc;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Game {
|
||||
numbers: Vec<u64>,
|
||||
boards: Vec<Board>,
|
||||
skip_boards: HashSet<usize>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@ -92,6 +102,24 @@ impl Game {
|
||||
}
|
||||
None
|
||||
}
|
||||
// If return not None, it contains a winning board. This will remove winning boards until only
|
||||
// one remains.
|
||||
fn apply_number_part2(&mut self, number: u64) -> Option<&Board> {
|
||||
let num_boards = self.boards.len();
|
||||
for (idx, b) in self.boards.iter_mut().enumerate() {
|
||||
if self.skip_boards.contains(&idx) {
|
||||
continue;
|
||||
}
|
||||
b.mark(number);
|
||||
if b.is_bingo() {
|
||||
self.skip_boards.insert(idx);
|
||||
if self.skip_boards.len() == num_boards {
|
||||
return Some(b);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Game {
|
||||
@ -106,7 +134,11 @@ impl FromStr for Game {
|
||||
.map(|s| s.parse())
|
||||
.collect::<Result<_, ParseIntError>>()?;
|
||||
let boards: Vec<_> = it.map(|s| s.parse()).collect::<Result<_, BoardError>>()?;
|
||||
Ok(Game { numbers, boards })
|
||||
Ok(Game {
|
||||
numbers,
|
||||
boards,
|
||||
skip_boards: Default::default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -219,13 +251,18 @@ fn part1(input: &str) -> Result<u64> {
|
||||
unreachable!("We should have had a winner by now");
|
||||
}
|
||||
|
||||
/*
|
||||
#[aoc(day4, part2)]
|
||||
fn part2(depths: &[u32]) -> Result<u32> {
|
||||
todo!("part2")
|
||||
Ok(())
|
||||
fn part2(input: &str) -> Result<u64> {
|
||||
let mut g: Game = input.parse()?;
|
||||
let numbers = g.numbers.clone();
|
||||
for n in numbers {
|
||||
if let Some(b) = g.apply_number_part2(n) {
|
||||
println!("winning board {:?}", b);
|
||||
return Ok(n as u64 * b.sum_uncovered());
|
||||
}
|
||||
}
|
||||
unreachable!("We should have had a winner by now");
|
||||
}
|
||||
*/
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
@ -281,14 +318,31 @@ mod tests {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn test_part2()->Result<()> {
|
||||
let input = r#"
|
||||
"#
|
||||
.trim();
|
||||
assert_eq!(part2(&parse(input)?)?, TODO);
|
||||
Ok(())
|
||||
fn test_part2() -> Result<()> {
|
||||
let input = r#"
|
||||
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
|
||||
|
||||
22 13 17 11 0
|
||||
8 2 23 4 24
|
||||
21 9 14 16 7
|
||||
6 10 3 18 5
|
||||
1 12 20 15 19
|
||||
|
||||
3 15 0 2 22
|
||||
9 18 13 17 5
|
||||
19 8 7 25 23
|
||||
20 11 10 24 4
|
||||
14 21 16 12 6
|
||||
|
||||
14 21 17 24 4
|
||||
10 16 15 9 19
|
||||
18 8 23 26 20
|
||||
22 11 13 6 5
|
||||
2 0 12 3 7
|
||||
"#
|
||||
.trim();
|
||||
assert_eq!(part2(input)?, 1924);
|
||||
Ok(())
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user