cleanup and add the missing tests

This commit is contained in:
BlackDemonFire 2023-12-05 21:46:54 +01:00
parent 1ffe216c51
commit 3ba7f6066f
Signed by: lucy
SSH key fingerprint: SHA256:LOoXGgXjn6B8CcfqWHkd2Y5gFaffr0b2tAzBKlEG6Xc
14 changed files with 178 additions and 90 deletions

4
d01t1/demo_input.txt Normal file
View file

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

View file

@ -1,6 +1,12 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
let result = input println!("d01t1: {}", d01t1(&input));
}
fn d01t1(input: &str) -> u32 {
input
.lines() .lines()
.map(|line| { .map(|line| {
let digits = line let digits = line
@ -11,6 +17,15 @@ fn main() {
num_str.push(*digits.last().unwrap()); num_str.push(*digits.last().unwrap());
num_str.parse::<u32>().unwrap() num_str.parse::<u32>().unwrap()
}) })
.sum::<u32>(); .sum::<u32>()
println!("d01t1: {result}"); }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d01t1() {
let input = include_str!("../demo_input.txt");
assert_eq!(d01t1(input), 142);
}
} }

7
d01t2/demo_input.txt Normal file
View file

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

View file

@ -1,5 +1,11 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d01t2: {}", d01t2(&input));
}
fn d01t2(input: &str) -> u32 {
let translated = input let translated = input
.replace("one", "o1e") .replace("one", "o1e")
.replace("two", "t2o") .replace("two", "t2o")
@ -10,7 +16,7 @@ fn main() {
.replace("seven", "s7n") .replace("seven", "s7n")
.replace("eight", "e8t") .replace("eight", "e8t")
.replace("nine", "n9e"); .replace("nine", "n9e");
let result = translated translated
.lines() .lines()
.map(|line| { .map(|line| {
let digits = line let digits = line
@ -21,6 +27,15 @@ fn main() {
num_str.push(*digits.last().unwrap()); num_str.push(*digits.last().unwrap());
num_str.parse::<u32>().unwrap() num_str.parse::<u32>().unwrap()
}) })
.sum::<u32>(); .sum::<u32>()
println!("d01t2: {result}"); }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d01t2() {
let input = include_str!("../demo_input.txt");
assert_eq!(d01t2(input), 281);
}
} }

5
d02t1/demo_input.txt Normal file
View file

@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

View file

@ -1,5 +1,11 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d02t1: {}", d02t1(&input));
}
fn d02t1(input: &str) -> i32 {
let mut result = 0; let mut result = 0;
for line in input.lines() { for line in input.lines() {
let (game_id, game_values) = line.split_once(": ").unwrap(); let (game_id, game_values) = line.split_once(": ").unwrap();
@ -18,5 +24,15 @@ fn main() {
result += game_id.split_once(' ').unwrap().1.parse::<i32>().unwrap(); result += game_id.split_once(' ').unwrap().1.parse::<i32>().unwrap();
} }
} }
println!("d02t1: {result}"); result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d02t1() {
let input = include_str!("../demo_input.txt");
assert_eq!(d02t1(input), 8);
}
} }

5
d02t2/demo_input.txt Normal file
View file

@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

View file

@ -1,6 +1,12 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
let result = input println!("d02t2: {}", d02t2(&input));
}
fn d02t2(input: &str) -> usize {
input
.lines() .lines()
.map(|line| { .map(|line| {
let (_game_id, game_values) = line.split_once(": ").unwrap(); let (_game_id, game_values) = line.split_once(": ").unwrap();
@ -19,6 +25,15 @@ fn main() {
}); });
min_red * min_green * min_blue min_red * min_green * min_blue
}) })
.sum::<usize>(); .sum::<usize>()
println!("d02t2: {result}"); }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_d02t2() {
let input = include_str!("../demo_input.txt");
assert_eq!(d02t2(input), 2286);
}
} }

View file

@ -1,4 +1,7 @@
use std::collections::{HashSet, VecDeque}; use std::{
collections::{HashSet, VecDeque},
fs::read_to_string,
};
fn dec_if_pos(input: usize) -> usize { fn dec_if_pos(input: usize) -> usize {
if input > 0 { if input > 0 {
@ -17,7 +20,11 @@ fn inc_if_lt(input: usize, max: usize) -> usize {
} }
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d03t1: {}", d03t1(&input));
}
fn d03t1(input: &str) -> usize {
let mut result = 0; let mut result = 0;
let lines = input.lines().collect::<Vec<_>>(); let lines = input.lines().collect::<Vec<_>>();
let total_lines = lines.len(); let total_lines = lines.len();
@ -78,5 +85,5 @@ fn main() {
} }
} }
} }
println!("d03t1: {result}"); result
} }

View file

@ -1,4 +1,7 @@
use std::collections::{HashSet, VecDeque}; use std::{
collections::{HashSet, VecDeque},
fs::read_to_string,
};
fn dec_if_pos(input: usize) -> usize { fn dec_if_pos(input: usize) -> usize {
if input > 0 { if input > 0 {
@ -17,7 +20,11 @@ fn inc_if_lt(input: usize, max: usize) -> usize {
} }
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d03t2: {}", d03t2(&input));
}
fn d03t2(input: &str) -> usize {
let mut result = 0; let mut result = 0;
let lines = input.lines().collect::<Vec<_>>(); let lines = input.lines().collect::<Vec<_>>();
let total_lines = lines.len(); let total_lines = lines.len();
@ -80,5 +87,5 @@ fn main() {
} }
} }
} }
println!("d03t2: {result}"); result
} }

View file

@ -1,6 +1,8 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d04t1: {}", d04t1(input)); println!("d04t1: {}", d04t1(&input));
} }
#[must_use] #[must_use]

View file

@ -1,79 +1,65 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d04t2: {}", d04t2(input)); println!("d04t2: {}", d04t2(&input));
} }
struct Card { struct Card {
card_id: usize, card_id: usize,
winning_amount: usize, result: usize,
} }
#[must_use] #[must_use]
pub fn d04t2(input: &str) -> usize { pub fn d04t2(input: &str) -> usize {
let mut result = 0; let mut cards: Vec<Card> = Vec::new();
let cards: Vec<Card> = input for line in input.lines().rev() {
.lines() let (card_id, card_data) = line
.map(|line| { .split_once(": ")
let (card_id, card_data) = line .map(|(cid, cd)| {
.split_once(": ") (
.map(|(cid, cd)| { cid.split_once(' ')
( .unwrap()
cid.split_once(' ') .1
.unwrap() .trim()
.1 .parse::<usize>()
.trim() .unwrap(),
.parse::<usize>() cd,
.unwrap(), )
cd, })
) .unwrap();
}) let (winning_numbers, actual_numbers) = card_data
.unwrap(); .split_once(" | ")
let (winning_numbers, actual_numbers) = card_data .map(|(a, b)| {
.split_once(" | ") (
.map(|(a, b)| { a.split_whitespace()
( .map(|e| e.parse::<usize>().unwrap())
a.split_whitespace() .collect::<Vec<_>>(),
.map(|e| e.parse::<usize>().unwrap()) b.split_whitespace()
.collect::<Vec<_>>(), .map(|e| e.parse::<usize>().unwrap())
b.split_whitespace() .collect::<Vec<_>>(),
.map(|e| e.parse::<usize>().unwrap()) )
.collect::<Vec<_>>(), })
) .unwrap();
}) let winning_amount = actual_numbers
.unwrap(); .iter()
let winning_amount = actual_numbers .filter(|num| winning_numbers.contains(num))
.count();
let mut result = 1;
for winning_card in (card_id + 1)..=(card_id + winning_amount) {
result += cards
.iter() .iter()
.filter(|num| winning_numbers.contains(num)) .find(|c| c.card_id == winning_card)
.count(); .map_or(0, |c| c.result);
Card {
card_id,
winning_amount,
}
})
.collect();
for card in &cards {
result += 1;
println!(
"Checking card {} - {} winning",
card.card_id, card.winning_amount
);
result += add_rec(card, &cards);
}
result
}
fn add_rec(card: &Card, cards: &Vec<Card>) -> usize {
let mut to_add = 0;
for cid in 1..=card.winning_amount {
let found_card = cards.iter().find(|c| c.card_id == card.card_id + cid);
if let Some(found_card) = found_card {
to_add += 1;
to_add += add_rec(found_card, cards);
} else {
println!("\tdidn't find card {}", card.card_id + cid);
} }
cards.push(Card { card_id, result });
} }
to_add let mut result: usize = 0;
for card in &cards {
result += card.result;
}
result
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,6 +1,8 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d05t1: {}", d05t1(input)); println!("d05t1: {}", d05t1(&input));
} }
pub fn d05t1(input: &str) -> usize { pub fn d05t1(input: &str) -> usize {

View file

@ -1,6 +1,8 @@
use std::fs::read_to_string;
fn main() { fn main() {
let input = include_str!("../input.txt"); let input = read_to_string("input.txt").unwrap();
println!("d05t2: {}", d05t2(input)); println!("d05t2: {}", d05t2(&input));
} }
pub fn d05t2(input: &str) -> usize { pub fn d05t2(input: &str) -> usize {