diff --git a/d01t1/demo_input.txt b/d01t1/demo_input.txt new file mode 100644 index 0000000..7bbc69a --- /dev/null +++ b/d01t1/demo_input.txt @@ -0,0 +1,4 @@ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet diff --git a/d01t1/src/main.rs b/d01t1/src/main.rs index 215022b..5b780da 100644 --- a/d01t1/src/main.rs +++ b/d01t1/src/main.rs @@ -1,6 +1,12 @@ +use std::fs::read_to_string; + fn main() { - let input = include_str!("../input.txt"); - let result = input + let input = read_to_string("input.txt").unwrap(); + println!("d01t1: {}", d01t1(&input)); +} + +fn d01t1(input: &str) -> u32 { + input .lines() .map(|line| { let digits = line @@ -11,6 +17,15 @@ fn main() { num_str.push(*digits.last().unwrap()); num_str.parse::().unwrap() }) - .sum::(); - println!("d01t1: {result}"); + .sum::() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d01t1() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d01t1(input), 142); + } } diff --git a/d01t2/demo_input.txt b/d01t2/demo_input.txt new file mode 100644 index 0000000..41aa89c --- /dev/null +++ b/d01t2/demo_input.txt @@ -0,0 +1,7 @@ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen diff --git a/d01t2/src/main.rs b/d01t2/src/main.rs index a3537cf..97d30e7 100644 --- a/d01t2/src/main.rs +++ b/d01t2/src/main.rs @@ -1,5 +1,11 @@ +use std::fs::read_to_string; + 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 .replace("one", "o1e") .replace("two", "t2o") @@ -10,7 +16,7 @@ fn main() { .replace("seven", "s7n") .replace("eight", "e8t") .replace("nine", "n9e"); - let result = translated + translated .lines() .map(|line| { let digits = line @@ -21,6 +27,15 @@ fn main() { num_str.push(*digits.last().unwrap()); num_str.parse::().unwrap() }) - .sum::(); - println!("d01t2: {result}"); + .sum::() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d01t2() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d01t2(input), 281); + } } diff --git a/d02t1/demo_input.txt b/d02t1/demo_input.txt new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/d02t1/demo_input.txt @@ -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 diff --git a/d02t1/src/main.rs b/d02t1/src/main.rs index 035eb5e..70fd7b0 100644 --- a/d02t1/src/main.rs +++ b/d02t1/src/main.rs @@ -1,5 +1,11 @@ +use std::fs::read_to_string; + 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; for line in input.lines() { let (game_id, game_values) = line.split_once(": ").unwrap(); @@ -18,5 +24,15 @@ fn main() { result += game_id.split_once(' ').unwrap().1.parse::().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); + } } diff --git a/d02t2/demo_input.txt b/d02t2/demo_input.txt new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/d02t2/demo_input.txt @@ -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 diff --git a/d02t2/src/main.rs b/d02t2/src/main.rs index 496d941..dd81f5a 100644 --- a/d02t2/src/main.rs +++ b/d02t2/src/main.rs @@ -1,6 +1,12 @@ +use std::fs::read_to_string; + fn main() { - let input = include_str!("../input.txt"); - let result = input + let input = read_to_string("input.txt").unwrap(); + println!("d02t2: {}", d02t2(&input)); +} + +fn d02t2(input: &str) -> usize { + input .lines() .map(|line| { let (_game_id, game_values) = line.split_once(": ").unwrap(); @@ -19,6 +25,15 @@ fn main() { }); min_red * min_green * min_blue }) - .sum::(); - println!("d02t2: {result}"); + .sum::() +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_d02t2() { + let input = include_str!("../demo_input.txt"); + assert_eq!(d02t2(input), 2286); + } } diff --git a/d03t1/src/main.rs b/d03t1/src/main.rs index d111147..510c688 100644 --- a/d03t1/src/main.rs +++ b/d03t1/src/main.rs @@ -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 { if input > 0 { @@ -17,7 +20,11 @@ fn inc_if_lt(input: usize, max: usize) -> usize { } 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 lines = input.lines().collect::>(); let total_lines = lines.len(); @@ -78,5 +85,5 @@ fn main() { } } } - println!("d03t1: {result}"); + result } diff --git a/d03t2/src/main.rs b/d03t2/src/main.rs index 27499ac..8631c89 100644 --- a/d03t2/src/main.rs +++ b/d03t2/src/main.rs @@ -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 { if input > 0 { @@ -17,7 +20,11 @@ fn inc_if_lt(input: usize, max: usize) -> usize { } 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 lines = input.lines().collect::>(); let total_lines = lines.len(); @@ -80,5 +87,5 @@ fn main() { } } } - println!("d03t2: {result}"); + result } diff --git a/d04t1/src/main.rs b/d04t1/src/main.rs index fad30ed..b5a666b 100644 --- a/d04t1/src/main.rs +++ b/d04t1/src/main.rs @@ -1,6 +1,8 @@ +use std::fs::read_to_string; + fn main() { - let input = include_str!("../input.txt"); - println!("d04t1: {}", d04t1(input)); + let input = read_to_string("input.txt").unwrap(); + println!("d04t1: {}", d04t1(&input)); } #[must_use] diff --git a/d04t2/src/main.rs b/d04t2/src/main.rs index 73f1b92..0df8326 100644 --- a/d04t2/src/main.rs +++ b/d04t2/src/main.rs @@ -1,79 +1,65 @@ +use std::fs::read_to_string; + fn main() { - let input = include_str!("../input.txt"); - println!("d04t2: {}", d04t2(input)); + let input = read_to_string("input.txt").unwrap(); + println!("d04t2: {}", d04t2(&input)); } struct Card { card_id: usize, - winning_amount: usize, + result: usize, } #[must_use] pub fn d04t2(input: &str) -> usize { - let mut result = 0; - let cards: Vec = input - .lines() - .map(|line| { - let (card_id, card_data) = line - .split_once(": ") - .map(|(cid, cd)| { - ( - cid.split_once(' ') - .unwrap() - .1 - .trim() - .parse::() - .unwrap(), - cd, - ) - }) - .unwrap(); - let (winning_numbers, actual_numbers) = card_data - .split_once(" | ") - .map(|(a, b)| { - ( - a.split_whitespace() - .map(|e| e.parse::().unwrap()) - .collect::>(), - b.split_whitespace() - .map(|e| e.parse::().unwrap()) - .collect::>(), - ) - }) - .unwrap(); - let winning_amount = actual_numbers + let mut cards: Vec = Vec::new(); + for line in input.lines().rev() { + let (card_id, card_data) = line + .split_once(": ") + .map(|(cid, cd)| { + ( + cid.split_once(' ') + .unwrap() + .1 + .trim() + .parse::() + .unwrap(), + cd, + ) + }) + .unwrap(); + let (winning_numbers, actual_numbers) = card_data + .split_once(" | ") + .map(|(a, b)| { + ( + a.split_whitespace() + .map(|e| e.parse::().unwrap()) + .collect::>(), + b.split_whitespace() + .map(|e| e.parse::().unwrap()) + .collect::>(), + ) + }) + .unwrap(); + let winning_amount = actual_numbers + .iter() + .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() - .filter(|num| winning_numbers.contains(num)) - .count(); - 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) -> 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); + .find(|c| c.card_id == winning_card) + .map_or(0, |c| c.result); } + cards.push(Card { card_id, result }); } - to_add + let mut result: usize = 0; + for card in &cards { + result += card.result; + } + + result } #[cfg(test)] diff --git a/d05t1/src/main.rs b/d05t1/src/main.rs index 031e359..e8b8b00 100644 --- a/d05t1/src/main.rs +++ b/d05t1/src/main.rs @@ -1,6 +1,8 @@ +use std::fs::read_to_string; + fn main() { - let input = include_str!("../input.txt"); - println!("d05t1: {}", d05t1(input)); + let input = read_to_string("input.txt").unwrap(); + println!("d05t1: {}", d05t1(&input)); } pub fn d05t1(input: &str) -> usize { diff --git a/d05t2/src/main.rs b/d05t2/src/main.rs index bad624e..796ac4d 100644 --- a/d05t2/src/main.rs +++ b/d05t2/src/main.rs @@ -1,6 +1,8 @@ +use std::fs::read_to_string; + fn main() { - let input = include_str!("../input.txt"); - println!("d05t2: {}", d05t2(input)); + let input = read_to_string("input.txt").unwrap(); + println!("d05t2: {}", d05t2(&input)); } pub fn d05t2(input: &str) -> usize {