1
0
mirror of https://gitlab.com/MisterBiggs/aoc-2023-rust.git synced 2025-07-23 22:51:32 +00:00

day 4, tbf I finished awhile ago

This commit is contained in:
2023-12-03 23:44:09 -07:00
parent e9a8cc657b
commit a6db3400ca
3 changed files with 275 additions and 0 deletions

69
src/day4.rs Normal file
View File

@@ -0,0 +1,69 @@
use std::collections::HashSet;
use std::fs;
pub fn run() {
println!("Day 4:");
let input = fs::read_to_string("./inputs/day4.txt").expect("Could not read file");
println!("\tPart 1: {}", part1(&input));
// println!("\tPart 2: {}", part2(&input));
}
fn part1(cards: &str) -> usize {
let mut total_wins = 0;
for card in cards.lines() {
let (_, card_numbers) = card.split_once(':').unwrap();
let (winners_raw, chances_raw) = card_numbers.split_once('|').unwrap();
let winners = winners_raw
.split(' ')
.filter(|s| !s.is_empty())
.map(|win| win.trim().parse::<usize>().unwrap())
.collect::<HashSet<usize>>();
let chances = chances_raw
.split(' ')
.filter(|s| !s.is_empty())
.map(|chance| chance.trim().parse::<usize>().unwrap())
.collect::<HashSet<usize>>();
let wins = winners.intersection(&chances).count();
if wins != 0 {
total_wins += usize::pow(2, wins.saturating_sub(1) as u32);
}
}
total_wins
}
fn part2(schematic: &str) -> usize {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let input = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11";
assert_eq!(part1(input), 13);
}
#[test]
fn test_2() {
let input = "Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11";
assert_eq!(part2(input), 467835);
}
}

View File

@@ -4,10 +4,12 @@ extern crate scan_fmt;
mod day1;
mod day2;
mod day3;
mod day4;
fn main() {
println!("Running Advent of Code 2023");
day1::run();
day2::run();
day3::run();
day4::run();
}