mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 12:50:32 +01:00
Compare commits
3 Commits
8a0b72f111
...
9e37026f30
| Author | SHA1 | Date | |
|---|---|---|---|
| 9e37026f30 | |||
| a926243f0d | |||
| a35ae82548 |
@@ -16,6 +16,10 @@ criterion = "0.3"
|
||||
# Keep debug information in release for better flamegraphs
|
||||
debug = true
|
||||
|
||||
[profile.bench]
|
||||
# And same for benchmarking
|
||||
debug = true
|
||||
|
||||
[[bench]]
|
||||
name = "days"
|
||||
harness = false
|
||||
|
||||
@@ -7,7 +7,7 @@ use criterion::criterion_main;
|
||||
use criterion::BenchmarkId;
|
||||
use criterion::Criterion;
|
||||
|
||||
const DAYS_IMPLEMENTED: usize = 13;
|
||||
const DAYS_IMPLEMENTED: usize = 14;
|
||||
|
||||
fn read_input(day: usize) -> Vec<u8> {
|
||||
let input_path = format!("inputs/{:02}.txt", day);
|
||||
|
||||
@@ -1,86 +1,91 @@
|
||||
use std::collections::HashMap;
|
||||
use std::io::Read;
|
||||
|
||||
use crate::common::LineIter;
|
||||
use nom::bytes::complete::tag;
|
||||
use nom::character::complete::multispace1;
|
||||
use nom::multi::many1;
|
||||
use nom::multi::separated_list1;
|
||||
use nom::sequence::preceded;
|
||||
use nom::sequence::tuple;
|
||||
use nom::Finish;
|
||||
use nom::IResult;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct BingoCard {
|
||||
ticks: [[bool; 5]; 5],
|
||||
mapping: HashMap<u8, (u8, u8)>,
|
||||
}
|
||||
struct BingoCard([(bool, u8); 25]);
|
||||
|
||||
impl BingoCard {
|
||||
pub fn cross(&mut self, num: u8) -> bool {
|
||||
if let Some(&(x, y)) = self.mapping.get(&num) {
|
||||
self.ticks[y as usize][x as usize] = true;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
pub fn cross(&mut self, num: u8) -> Option<usize> {
|
||||
self.0
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.find_map(|(pos, (ticked, x))| {
|
||||
if *x == num {
|
||||
*ticked = true;
|
||||
Some(pos)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn has_won(&self) -> bool {
|
||||
pub fn has_won(&self, crossed: usize) -> bool {
|
||||
// Check horizontal lines
|
||||
if self.ticks.iter().any(|s| s.iter().all(|&b| b)) {
|
||||
if self
|
||||
.0
|
||||
.chunks_exact(5)
|
||||
.nth(crossed / 5)
|
||||
.unwrap()
|
||||
.iter()
|
||||
.all(|&b| b.0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check vertical lines
|
||||
(0..5).any(|x| self.ticks.iter().all(|row| row[x]))
|
||||
self.0.iter().skip(crossed % 5).step_by(5).all(|b| b.0)
|
||||
|
||||
// Diagonals do not count
|
||||
}
|
||||
|
||||
pub fn remaining(&self) -> u32 {
|
||||
self.mapping
|
||||
self.0
|
||||
.iter()
|
||||
.filter_map(|(&num, &(x, y))| {
|
||||
if self.ticks[y as usize][x as usize] {
|
||||
None
|
||||
} else {
|
||||
Some(num as u32)
|
||||
}
|
||||
})
|
||||
.filter_map(|&(ticked, num)| if !ticked { Some(num as u32) } else { None })
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
|
||||
fn read_input(input: &mut dyn Read) -> (Vec<u8>, Vec<BingoCard>) {
|
||||
let mut reader = LineIter::new(input);
|
||||
fn parse_numbers(input: &[u8]) -> IResult<&[u8], Vec<u8>> {
|
||||
use nom::character::complete::u8;
|
||||
separated_list1(tag(","), u8)(input)
|
||||
}
|
||||
|
||||
let numbers: Vec<u8> = reader
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(',')
|
||||
.map(|num| num.parse().unwrap())
|
||||
.collect();
|
||||
fn parse_bingo(mut input: &[u8]) -> IResult<&[u8], BingoCard> {
|
||||
use nom::character::complete::u8;
|
||||
|
||||
let mut bingo_cards = Vec::new();
|
||||
let mut card = [0; 25];
|
||||
|
||||
while reader.next().is_some() {
|
||||
let mut mapping = HashMap::with_capacity(25);
|
||||
let mut parse_num = preceded(multispace1, u8);
|
||||
|
||||
for y in 0..5 {
|
||||
reader
|
||||
.next()
|
||||
.unwrap()
|
||||
.split(' ')
|
||||
.filter_map(|s| if s.is_empty() { None } else { s.parse().ok() })
|
||||
.enumerate()
|
||||
.for_each(|(x, num)| {
|
||||
mapping.insert(num, (x as u8, y));
|
||||
});
|
||||
}
|
||||
|
||||
let card = BingoCard {
|
||||
mapping,
|
||||
ticks: Default::default(),
|
||||
};
|
||||
|
||||
bingo_cards.push(card);
|
||||
// fill doesn't work with preceded
|
||||
for num in &mut card {
|
||||
let result = parse_num(input)?;
|
||||
*num = result.1;
|
||||
input = result.0;
|
||||
}
|
||||
|
||||
(numbers, bingo_cards)
|
||||
Ok((input, BingoCard(card.map(|x| (false, x)))))
|
||||
}
|
||||
|
||||
fn parse_input(input: &[u8]) -> IResult<&[u8], (Vec<u8>, Vec<BingoCard>)> {
|
||||
tuple((parse_numbers, many1(parse_bingo)))(input)
|
||||
}
|
||||
|
||||
fn read_input(input: &mut dyn Read) -> (Vec<u8>, Vec<BingoCard>) {
|
||||
let mut buffer = Vec::new();
|
||||
|
||||
input.read_to_end(&mut buffer).unwrap();
|
||||
|
||||
parse_input(&buffer).finish().unwrap().1
|
||||
}
|
||||
|
||||
pub fn part1(input: &mut dyn Read) -> String {
|
||||
@@ -88,7 +93,7 @@ pub fn part1(input: &mut dyn Read) -> String {
|
||||
|
||||
for number in numbers {
|
||||
for card in &mut bingo_cards {
|
||||
if card.cross(number) && card.has_won() {
|
||||
if matches!(card.cross(number), Some(pos) if card.has_won(pos)) {
|
||||
return (number as u32 * card.remaining()).to_string();
|
||||
}
|
||||
}
|
||||
@@ -105,7 +110,11 @@ pub fn part2(input: &mut dyn Read) -> String {
|
||||
|
||||
for num in numbers {
|
||||
for (won, card) in bingo_won.iter_mut().zip(bingo_cards.iter_mut()) {
|
||||
if !*won && card.cross(num) && card.has_won() {
|
||||
if *won {
|
||||
continue;
|
||||
}
|
||||
|
||||
if matches!(card.cross(num), Some(pos) if card.has_won(pos)) {
|
||||
*won = true;
|
||||
num_won += 1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user