Clean up more; Add tests for sample input
This commit is contained in:
parent
cef5587b56
commit
47c420c630
19 changed files with 275 additions and 123 deletions
|
@ -23,10 +23,6 @@ codegen-units = 1
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "day00"
|
|
||||||
path = "src/day00/main.rs"
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "day01"
|
name = "day01"
|
||||||
path = "src/day01/main.rs"
|
path = "src/day01/main.rs"
|
||||||
|
|
|
@ -1,3 +1,24 @@
|
||||||
pub(crate) fn part_1(input: &'static str) {
|
pub(crate) fn part_1(_input: &'static str) -> u64 {
|
||||||
println!("Part 1 {input}")
|
todo!("Part 1")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(
|
||||||
|
super::part_1(crate::INPUT),
|
||||||
|
todo!("Add result for solved part 1")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(
|
||||||
|
super::part_1(SAMPLE_INPUT),
|
||||||
|
todo!("Add result from example part 1")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,24 @@
|
||||||
pub(crate) fn part_2(input: &'static str) {
|
pub(crate) fn part_2(_input: &'static str) -> u64 {
|
||||||
println!("Part 2 {input}")
|
todo!("Part 2")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(
|
||||||
|
super::part_2(crate::INPUT),
|
||||||
|
todo!("Add result for solved part 2")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(
|
||||||
|
super::part_2(SAMPLE_INPUT),
|
||||||
|
todo!("Add result from example part 2")
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
0
rust/src/day00/sample_input.txt
Normal file
0
rust/src/day00/sample_input.txt
Normal file
|
@ -6,8 +6,6 @@ mod part_2;
|
||||||
use part_2::part_2;
|
use part_2::part_2;
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let input = INPUT.replace("\r\n", "\n");
|
part_1(INPUT);
|
||||||
|
part_2(INPUT);
|
||||||
part_1(&input);
|
|
||||||
part_2(&input);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
pub(crate) fn part_1(input: &str) -> u32 {
|
pub(crate) fn part_1(input: &str) -> u32 {
|
||||||
let maximum = input
|
let maximum = input
|
||||||
|
.replace("\r\n", "\n")
|
||||||
.split("\n\n")
|
.split("\n\n")
|
||||||
.map(|inventory| {
|
.map(|inventory| {
|
||||||
inventory
|
inventory
|
||||||
|
@ -17,7 +18,16 @@ pub(crate) fn part_1(input: &str) -> u32 {
|
||||||
maximum
|
maximum
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_with_solution() {
|
mod tests {
|
||||||
assert_eq!(part_1(&crate::INPUT.replace("\r\n", "\n")), 69528);
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(super::part_1(&crate::INPUT.replace("\r\n", "\n")), 69528);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(super::part_1(&SAMPLE_INPUT.replace("\r\n", "\n")), 24000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
pub(crate) fn part_2(input: &str) -> u32 {
|
pub(crate) fn part_2(input: &str) -> u32 {
|
||||||
let mut calories = input
|
let mut calories = input
|
||||||
|
.replace("\r\n", "\n")
|
||||||
.split("\n\n")
|
.split("\n\n")
|
||||||
.map(|inventory| {
|
.map(|inventory| {
|
||||||
inventory
|
inventory
|
||||||
|
@ -19,7 +20,16 @@ pub(crate) fn part_2(input: &str) -> u32 {
|
||||||
top_three
|
top_three
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_with_solution() {
|
mod tests {
|
||||||
assert_eq!(part_2(&crate::INPUT.replace("\r\n", "\n")), 206152);
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(super::part_2(&crate::INPUT.replace("\r\n", "\n")), 206152);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(super::part_2(&SAMPLE_INPUT.replace("\r\n", "\n")), 45000);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
14
rust/src/day01/sample_input.txt
Normal file
14
rust/src/day01/sample_input.txt
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
1000
|
||||||
|
2000
|
||||||
|
3000
|
||||||
|
|
||||||
|
4000
|
||||||
|
|
||||||
|
5000
|
||||||
|
6000
|
||||||
|
|
||||||
|
7000
|
||||||
|
8000
|
||||||
|
9000
|
||||||
|
|
||||||
|
10000
|
|
@ -123,42 +123,7 @@ impl FromStr for GameResult {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_input_part_1() -> Vec<[Shape; 2]> {
|
|
||||||
INPUT
|
|
||||||
.lines()
|
|
||||||
.map(|round_string| {
|
|
||||||
let round = round_string
|
|
||||||
.split(' ')
|
|
||||||
.map(|play| {
|
|
||||||
assert_eq!(play.len(), 1, "play: {play}");
|
|
||||||
Shape::from_str(play).unwrap_or_else(|_| panic!("Found invalid play {play}"))
|
|
||||||
})
|
|
||||||
.take(2)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
[round[0], round[1]]
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_input_part_2() -> Vec<(Shape, GameResult)> {
|
|
||||||
INPUT
|
|
||||||
.lines()
|
|
||||||
.map(|round_string| {
|
|
||||||
let round = round_string.split(' ').take(2).collect::<Vec<_>>();
|
|
||||||
assert_eq!(round.len(), 2);
|
|
||||||
assert_eq!(round[0].len(), 1);
|
|
||||||
assert_eq!(round[1].len(), 1);
|
|
||||||
let enemy_shape = Shape::from_str(round[0])
|
|
||||||
.unwrap_or_else(|_| panic!("Found invalid play {}", round[0]));
|
|
||||||
let desired_result = GameResult::from_str(round[1])
|
|
||||||
.unwrap_or_else(|_| panic!("Found invalid desired result {}", round[1]));
|
|
||||||
|
|
||||||
(enemy_shape, desired_result)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
part_1(&parse_input_part_1());
|
part_1(INPUT);
|
||||||
part_2(&parse_input_part_2());
|
part_2(INPUT);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,42 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::Shape;
|
use crate::Shape;
|
||||||
|
|
||||||
pub(crate) fn part_1(input: &[[Shape; 2]]) -> u64 {
|
pub(crate) fn part_1(input: &'static str) -> u64 {
|
||||||
let final_score = input.iter().fold(0u64, |acc, [enemy, mine]| {
|
let parsed_input = parse_input(input);
|
||||||
|
let final_score = parsed_input.iter().fold(0u64, |acc, [enemy, mine]| {
|
||||||
acc + u64::from(&mine.play_against(enemy)) + u64::from(mine)
|
acc + u64::from(&mine.play_against(enemy)) + u64::from(mine)
|
||||||
});
|
});
|
||||||
println!("Part 1: {final_score}");
|
println!("Part 1: {final_score}");
|
||||||
final_score
|
final_score
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
pub(crate) fn parse_input(input: &'static str) -> Vec<[Shape; 2]> {
|
||||||
fn test_with_solution() {
|
input
|
||||||
assert_eq!(part_1(&crate::parse_input_part_1()), 13221);
|
.lines()
|
||||||
|
.map(|round_string| {
|
||||||
|
let round = round_string
|
||||||
|
.split(' ')
|
||||||
|
.map(|play| {
|
||||||
|
assert_eq!(play.len(), 1, "play: {play}");
|
||||||
|
Shape::from_str(play).unwrap_or_else(|_| panic!("Found invalid play {play}"))
|
||||||
|
})
|
||||||
|
.take(2)
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
[round[0], round[1]]
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(super::part_1(crate::INPUT), 13221);
|
||||||
|
}
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(super::part_1(SAMPLE_INPUT), 15);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::{GameResult, Shape};
|
use crate::{GameResult, Shape};
|
||||||
|
|
||||||
pub(crate) fn part_2(input: &[(Shape, GameResult)]) -> u64 {
|
pub(crate) fn part_2(input: &'static str) -> u64 {
|
||||||
|
let input = parse_input(input);
|
||||||
let final_score = input
|
let final_score = input
|
||||||
.iter()
|
.iter()
|
||||||
.fold(0u64, |acc, (enemy_play, desired_result)| {
|
.fold(0u64, |acc, (enemy_play, desired_result)| {
|
||||||
|
@ -11,7 +14,35 @@ pub(crate) fn part_2(input: &[(Shape, GameResult)]) -> u64 {
|
||||||
final_score
|
final_score
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
fn parse_input(input: &'static str) -> Vec<(Shape, GameResult)> {
|
||||||
fn test_with_solution() {
|
input
|
||||||
assert_eq!(part_2(&crate::parse_input_part_2()), 13131);
|
.lines()
|
||||||
|
.map(|round_string| {
|
||||||
|
let round = round_string.split(' ').take(2).collect::<Vec<_>>();
|
||||||
|
assert_eq!(round.len(), 2);
|
||||||
|
assert_eq!(round[0].len(), 1);
|
||||||
|
assert_eq!(round[1].len(), 1);
|
||||||
|
let enemy_shape = Shape::from_str(round[0])
|
||||||
|
.unwrap_or_else(|_| panic!("Found invalid play {}", round[0]));
|
||||||
|
let desired_result = GameResult::from_str(round[1])
|
||||||
|
.unwrap_or_else(|_| panic!("Found invalid desired result {}", round[1]));
|
||||||
|
|
||||||
|
(enemy_shape, desired_result)
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(super::part_2(crate::INPUT), 13131);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(super::part_2(SAMPLE_INPUT), 12);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
3
rust/src/day02/sample_input.txt
Normal file
3
rust/src/day02/sample_input.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
A Y
|
||||||
|
B X
|
||||||
|
C Z
|
|
@ -36,7 +36,17 @@ pub(crate) fn part_1(input: &'static str) -> u64 {
|
||||||
running_sum
|
running_sum
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_with_solution() {
|
mod tests {
|
||||||
assert_eq!(part_1(crate::INPUT), 7742);
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(crate::part_1(crate::INPUT), 7742);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(crate::part_1(SAMPLE_INPUT), 157);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,17 @@ pub(crate) fn part_2(input: &'static str) -> u64 {
|
||||||
running_sum
|
running_sum
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_with_solution() {
|
mod tests {
|
||||||
assert_eq!(part_2(crate::INPUT), 2276);
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(super::part_2(crate::INPUT), 2276);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_sample_solution() {
|
||||||
|
assert_eq!(super::part_2(SAMPLE_INPUT), 70);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
6
rust/src/day03/sample_input.txt
Normal file
6
rust/src/day03/sample_input.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
vJrwpWtwJgWrhcsFMMfFFhFp
|
||||||
|
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
|
||||||
|
PmmdzqPrVvPwwTWBwg
|
||||||
|
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
|
||||||
|
ttgJtRGJQctTZtZT
|
||||||
|
CrZsJsPPZsGzwwsLwLmpwMDw
|
|
@ -5,8 +5,8 @@ use part_1::part_1;
|
||||||
mod part_2;
|
mod part_2;
|
||||||
use part_2::part_2;
|
use part_2::part_2;
|
||||||
|
|
||||||
fn parse_input() -> Vec<[[u32; 2]; 2]> {
|
fn parse_input(input: &'static str) -> Vec<[[u32; 2]; 2]> {
|
||||||
INPUT
|
input
|
||||||
.lines()
|
.lines()
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
let pairs = line
|
let pairs = line
|
||||||
|
@ -29,6 +29,7 @@ fn parse_input() -> Vec<[[u32; 2]; 2]> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
part_1(&parse_input());
|
let input = parse_input(INPUT);
|
||||||
part_2(&parse_input());
|
part_1(&input);
|
||||||
|
part_2(&input);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,37 +18,48 @@ pub(crate) fn part_1(input: &[[[u32; 2]; 2]]) -> usize {
|
||||||
times_contained
|
times_contained
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_containment() {
|
mod tests {
|
||||||
for num in 0..=1024u32 {
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
assert!(sections_contain_each_other([[num, num], [num, num]]));
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(super::part_1(&crate::parse_input(crate::INPUT)), 584);
|
||||||
}
|
}
|
||||||
|
|
||||||
for num_a in 0..=1024u32 {
|
#[test]
|
||||||
for num_b in num_a..=1024u32 {
|
fn test_with_sample_solution() {
|
||||||
assert!(sections_contain_each_other([
|
assert_eq!(super::part_1(&crate::parse_input(SAMPLE_INPUT)), 2);
|
||||||
[num_a, num_b],
|
}
|
||||||
[num_b, num_b]
|
|
||||||
]));
|
#[test]
|
||||||
assert!(sections_contain_each_other([
|
#[ignore = "Sanity check test"]
|
||||||
[num_a, num_b],
|
fn test_containment() {
|
||||||
[num_a, num_b]
|
for num in 0..=1024u32 {
|
||||||
]));
|
assert!(super::sections_contain_each_other([[num, num], [num, num]]));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for num_a in 0..=1024u32 {
|
for num_a in 0..=1024u32 {
|
||||||
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
for num_b in num_a..=1024u32 {
|
||||||
assert!(!sections_contain_each_other([
|
assert!(super::sections_contain_each_other([
|
||||||
[num_a, num_a],
|
[num_a, num_b],
|
||||||
[num_b, num_b]
|
[num_b, num_b]
|
||||||
]));
|
]));
|
||||||
|
assert!(super::sections_contain_each_other([
|
||||||
|
[num_a, num_b],
|
||||||
|
[num_a, num_b]
|
||||||
|
]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
assert!(!sections_contain_each_other([[1, 3], [3, 83]]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
for num_a in 0..=1024u32 {
|
||||||
fn test_with_solution() {
|
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
||||||
assert_eq!(part_1(&crate::parse_input()), 584);
|
assert!(!super::sections_contain_each_other([
|
||||||
|
[num_a, num_a],
|
||||||
|
[num_b, num_b]
|
||||||
|
]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(!super::sections_contain_each_other([[1, 3], [3, 83]]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,32 +18,43 @@ pub(crate) fn part_2(input: &[[[u32; 2]; 2]]) -> usize {
|
||||||
times_overlapped
|
times_overlapped
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn test_overlap() {
|
mod tests {
|
||||||
for num in 0..=1024u32 {
|
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||||
assert!(sections_overlap([[num, num], [num, num]]));
|
|
||||||
|
#[test]
|
||||||
|
fn test_with_solution() {
|
||||||
|
assert_eq!(super::part_2(&crate::parse_input(crate::INPUT)), 933);
|
||||||
}
|
}
|
||||||
|
|
||||||
for num_a in 0..=1024u32 {
|
#[test]
|
||||||
for num_b in num_a..=1024u32 {
|
fn test_with_sample_solution() {
|
||||||
assert!(
|
assert_eq!(super::part_2(&crate::parse_input(SAMPLE_INPUT)), 4);
|
||||||
sections_overlap([[num_a, num_b], [num_b, num_b]]),
|
}
|
||||||
"{:?}",
|
|
||||||
[[num_a, num_b], [num_b, num_b]]
|
#[test]
|
||||||
);
|
#[ignore = "Sanity check test"]
|
||||||
assert!(sections_overlap([[num_a, num_b], [num_a, num_b]]));
|
fn test_overlap() {
|
||||||
|
for num in 0..=1024u32 {
|
||||||
|
assert!(super::sections_overlap([[num, num], [num, num]]));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for num_a in 0..=1024u32 {
|
for num_a in 0..=1024u32 {
|
||||||
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
for num_b in num_a..=1024u32 {
|
||||||
assert!(!sections_overlap([[num_a, num_a], [num_b, num_b]]));
|
assert!(
|
||||||
|
super::sections_overlap([[num_a, num_b], [num_b, num_b]]),
|
||||||
|
"{:?}",
|
||||||
|
[[num_a, num_b], [num_b, num_b]]
|
||||||
|
);
|
||||||
|
assert!(super::sections_overlap([[num_a, num_b], [num_a, num_b]]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
assert!(sections_overlap([[1, 3], [3, 83]]));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
for num_a in 0..=1024u32 {
|
||||||
fn test_with_solution() {
|
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
||||||
assert_eq!(part_2(&crate::parse_input()), 933);
|
assert!(!super::sections_overlap([[num_a, num_a], [num_b, num_b]]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(super::sections_overlap([[1, 3], [3, 83]]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
6
rust/src/day04/sample_input.txt
Normal file
6
rust/src/day04/sample_input.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
2-4,6-8
|
||||||
|
2-3,4-5
|
||||||
|
5-7,7-9
|
||||||
|
2-8,3-7
|
||||||
|
6-6,4-6
|
||||||
|
2-6,4-8
|
Loading…
Reference in a new issue