diff --git a/04/.gitignore b/04/.gitignore new file mode 100644 index 0000000..b177307 --- /dev/null +++ b/04/.gitignore @@ -0,0 +1 @@ +day04 diff --git a/04/README.md b/04/README.md new file mode 100644 index 0000000..57ee78e --- /dev/null +++ b/04/README.md @@ -0,0 +1,9 @@ +# [Day 4](https://adventofcode.com/2023/day/4) +:gift::gift::gift::gift: + +Today's language: **Rust** + +```shell +rustc day04.rs +./day04 +``` diff --git a/04/day04.rs b/04/day04.rs new file mode 100644 index 0000000..a8a464a --- /dev/null +++ b/04/day04.rs @@ -0,0 +1,49 @@ +use std::io::{self, BufRead}; + +fn split_into_numbers(x: &str) -> impl Iterator + '_ { + return x.split(' ').filter(|&n| {n != "" && n != " "}).map(|n| n.parse::().unwrap()); +} + +fn main() { + let input = "input.txt"; + let lines = read_lines(&input); + let n_cards: usize = read_lines(&input).count(); + let mut cards: Vec = vec![1; n_cards]; + let mut sum_points: u32 = 0; + // number of copies per card + for (i, line) in lines.enumerate() { + let mut points: u32 = 0; + let mut n_matches: usize = 0; + let Ok(card) = line else { panic!("Line not ok"); }; + let Some(colon) = card.find(':') else { panic!("Could not find ':'"); }; + let Some(pipe) = card.find('|') else { panic!("Could not find '|'"); }; + // let split_into_numbers = |x: &str| { return x.split(' ').filter(|&n| {n != "" && n != " "}).map(|n| n.parse::().unwrap()); }; + let winning_numbers: Vec = split_into_numbers(&card[colon+2..pipe-1]).collect(); + for number in split_into_numbers(&card[pipe+2..]) { + if winning_numbers.contains(&number) { + // task 1 + if points == 0 { points = 1; } + else { points <<= 1; } + // task 2 + if points == 0 { points = 1; } + n_matches += 1; + } + } + // task 1 + sum_points += points; + // task 2: add #copies(card i) to the next n_matches cards + for j in i+1..i+1+n_matches { + cards[j] += cards[i]; + } + } + println!("Total points: {}", sum_points); + println!("Total number of cards: {}", cards.iter().sum::()); +} + +fn read_lines

(filename: P) -> io::Lines> +where P: AsRef, { + return match std::fs::File::open(filename) { + Err(why) => panic!("Could not open file. {}", why), + Ok(file) => std::io::BufReader::new(file).lines() + }; +}