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
|
||||
codegen-units = 1
|
||||
|
||||
[[bin]]
|
||||
name = "day00"
|
||||
path = "src/day00/main.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "day01"
|
||||
path = "src/day01/main.rs"
|
||||
|
|
|
@ -1,3 +1,24 @@
|
|||
pub(crate) fn part_1(input: &'static str) {
|
||||
println!("Part 1 {input}")
|
||||
pub(crate) fn part_1(_input: &'static str) -> u64 {
|
||||
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) {
|
||||
println!("Part 2 {input}")
|
||||
pub(crate) fn part_2(_input: &'static str) -> u64 {
|
||||
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;
|
||||
|
||||
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 {
|
||||
let maximum = input
|
||||
.replace("\r\n", "\n")
|
||||
.split("\n\n")
|
||||
.map(|inventory| {
|
||||
inventory
|
||||
|
@ -17,7 +18,16 @@ pub(crate) fn part_1(input: &str) -> u32 {
|
|||
maximum
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_1(&crate::INPUT.replace("\r\n", "\n")), 69528);
|
||||
#[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.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 {
|
||||
let mut calories = input
|
||||
.replace("\r\n", "\n")
|
||||
.split("\n\n")
|
||||
.map(|inventory| {
|
||||
inventory
|
||||
|
@ -19,7 +20,16 @@ pub(crate) fn part_2(input: &str) -> u32 {
|
|||
top_three
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_2(&crate::INPUT.replace("\r\n", "\n")), 206152);
|
||||
#[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.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() {
|
||||
part_1(&parse_input_part_1());
|
||||
part_2(&parse_input_part_2());
|
||||
part_1(INPUT);
|
||||
part_2(INPUT);
|
||||
}
|
||||
|
|
|
@ -1,14 +1,42 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use crate::Shape;
|
||||
|
||||
pub(crate) fn part_1(input: &[[Shape; 2]]) -> u64 {
|
||||
let final_score = input.iter().fold(0u64, |acc, [enemy, mine]| {
|
||||
pub(crate) fn part_1(input: &'static str) -> u64 {
|
||||
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)
|
||||
});
|
||||
println!("Part 1: {final_score}");
|
||||
final_score
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_1(&crate::parse_input_part_1()), 13221);
|
||||
pub(crate) fn parse_input(input: &'static str) -> 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<_>>()
|
||||
}
|
||||
|
||||
#[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};
|
||||
|
||||
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
|
||||
.iter()
|
||||
.fold(0u64, |acc, (enemy_play, desired_result)| {
|
||||
|
@ -11,7 +14,35 @@ pub(crate) fn part_2(input: &[(Shape, GameResult)]) -> u64 {
|
|||
final_score
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_2(&crate::parse_input_part_2()), 13131);
|
||||
fn parse_input(input: &'static str) -> 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<_>>()
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_1(crate::INPUT), 7742);
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
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
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_2(crate::INPUT), 2276);
|
||||
#[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), 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;
|
||||
use part_2::part_2;
|
||||
|
||||
fn parse_input() -> Vec<[[u32; 2]; 2]> {
|
||||
INPUT
|
||||
fn parse_input(input: &'static str) -> Vec<[[u32; 2]; 2]> {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let pairs = line
|
||||
|
@ -29,6 +29,7 @@ fn parse_input() -> Vec<[[u32; 2]; 2]> {
|
|||
}
|
||||
|
||||
pub fn main() {
|
||||
part_1(&parse_input());
|
||||
part_2(&parse_input());
|
||||
let input = parse_input(INPUT);
|
||||
part_1(&input);
|
||||
part_2(&input);
|
||||
}
|
||||
|
|
|
@ -18,37 +18,48 @@ pub(crate) fn part_1(input: &[[[u32; 2]; 2]]) -> usize {
|
|||
times_contained
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_containment() {
|
||||
for num in 0..=1024u32 {
|
||||
assert!(sections_contain_each_other([[num, num], [num, num]]));
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(super::part_1(&crate::parse_input(crate::INPUT)), 584);
|
||||
}
|
||||
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a..=1024u32 {
|
||||
assert!(sections_contain_each_other([
|
||||
[num_a, num_b],
|
||||
[num_b, num_b]
|
||||
]));
|
||||
assert!(sections_contain_each_other([
|
||||
[num_a, num_b],
|
||||
[num_a, num_b]
|
||||
]));
|
||||
#[test]
|
||||
fn test_with_sample_solution() {
|
||||
assert_eq!(super::part_1(&crate::parse_input(SAMPLE_INPUT)), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Sanity check test"]
|
||||
fn test_containment() {
|
||||
for num in 0..=1024u32 {
|
||||
assert!(super::sections_contain_each_other([[num, num], [num, num]]));
|
||||
}
|
||||
}
|
||||
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
||||
assert!(!sections_contain_each_other([
|
||||
[num_a, num_a],
|
||||
[num_b, num_b]
|
||||
]));
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a..=1024u32 {
|
||||
assert!(super::sections_contain_each_other([
|
||||
[num_a, 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]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_1(&crate::parse_input()), 584);
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
||||
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
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_overlap() {
|
||||
for num in 0..=1024u32 {
|
||||
assert!(sections_overlap([[num, num], [num, num]]));
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(super::part_2(&crate::parse_input(crate::INPUT)), 933);
|
||||
}
|
||||
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a..=1024u32 {
|
||||
assert!(
|
||||
sections_overlap([[num_a, num_b], [num_b, num_b]]),
|
||||
"{:?}",
|
||||
[[num_a, num_b], [num_b, num_b]]
|
||||
);
|
||||
assert!(sections_overlap([[num_a, num_b], [num_a, num_b]]));
|
||||
#[test]
|
||||
fn test_with_sample_solution() {
|
||||
assert_eq!(super::part_2(&crate::parse_input(SAMPLE_INPUT)), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "Sanity check test"]
|
||||
fn test_overlap() {
|
||||
for num in 0..=1024u32 {
|
||||
assert!(super::sections_overlap([[num, num], [num, num]]));
|
||||
}
|
||||
}
|
||||
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
||||
assert!(!sections_overlap([[num_a, num_a], [num_b, num_b]]));
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a..=1024u32 {
|
||||
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]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(part_2(&crate::parse_input()), 933);
|
||||
for num_a in 0..=1024u32 {
|
||||
for num_b in num_a + 1u32..=num_a + 1024u32 {
|
||||
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