Last active
December 7, 2023 09:30
-
-
Save oisin/f9a1b51c6b4eba22d657852cf88a5c59 to your computer and use it in GitHub Desktop.
AOC Day 4 Part 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::env; | |
use std::fs::File; | |
use std::io::{self, BufRead}; | |
use std::collections::HashSet; | |
#[derive(Debug)] | |
#[derive(Clone)] | |
struct CardRecord { | |
wins: u32, | |
copies: u32 | |
} | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
if args.len() != 2 { | |
eprintln!("Usage: {} <filename>", args[0]); | |
std::process::exit(1); | |
} | |
let filename = &args[1]; | |
let file = match File::open(filename) { | |
Ok(file) => file, | |
Err(error) => { | |
eprintln!("Error opening file {}: {}", filename, error); | |
std::process::exit(1); | |
} | |
}; | |
let reader = io::BufReader::new(file); | |
let mut stack: Vec<CardRecord> = Vec::new(); | |
for (line_number, line) in reader.lines().enumerate() { | |
match line { | |
Ok(line_content) => { | |
let row = parse(line_number + 1, line_content); | |
// First 10 numbers are the winning numbers, rest are the guess numbers | |
let winners: HashSet<u32> = row.numbers.clone().into_iter().take(10).collect(); | |
let guesses: HashSet<u32> = row.numbers.into_iter().skip(10).collect(); | |
let wins = winners.intersection(&guesses).count() as u32; | |
stack.push(CardRecord { | |
wins, | |
copies: 1 | |
}); | |
}, | |
Err(error) => { | |
eprintln!("Error reading line {}: {}", line_number + 1, error); | |
} | |
} | |
} | |
// Detect and add all the copies into the stack. Not using an iterator for this | |
// as it will borrow the stack and then it can't be borrowed again during the | |
// loop to update the values. Sticking to good ol' for loops, innit. | |
// | |
for inx in 0..stack.len() { | |
for _ in 1..=stack[inx].copies { | |
for copies in 1..stack[inx].wins + 1 { | |
if inx + (copies as usize) < stack.len() { | |
stack[inx + copies as usize].copies += 1; | |
} | |
} | |
} | |
} | |
println!("Total = {}", stack.iter().fold(0, |a, c| a + c.copies)); | |
} | |
#[derive(PartialEq)] | |
enum ParseState { | |
Ignore, | |
Start, | |
Number | |
} | |
#[derive(Clone, Copy)] | |
struct ParserContext { | |
value: u32 | |
} | |
struct Row { | |
numbers: Vec<u32> | |
} | |
fn parse(_line_number: usize, line_content: String) -> Row { | |
let mut state = ParseState::Ignore; | |
let mut row = Row { | |
numbers: Vec::new() | |
}; | |
let mut context = ParserContext{ | |
value: 0 | |
}; | |
for (_, c) in line_content.chars().enumerate() { | |
match c { | |
':' => { | |
state = ParseState::Start; | |
} | |
('0'..='9') => { | |
match state { | |
ParseState::Start => { | |
context = ParserContext { | |
value: c.to_digit(10).expect("Bum not a digit"), | |
}; | |
state = ParseState::Number; | |
} | |
ParseState::Number => { | |
context.value = context.value * 10 + c.to_digit(10).expect("Bum not a digit") | |
} | |
_ => () | |
} | |
}, | |
_ => { | |
if state == ParseState::Number { | |
row.numbers.push(context.value); | |
} | |
state = ParseState::Start; | |
} | |
} | |
} | |
if state == ParseState::Number { | |
row.numbers.push(context.value); | |
} | |
row | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Oops lots of shite left in there.