From dfb1b25c48ed3ece94258fb26292a90cc3857995 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sun, 4 Dec 2022 01:25:36 -0600 Subject: [PATCH] Cleanup --- 2022/rust/readme.md | 2 +- 2022/rust/src/common.rs | 6 ++++ 2022/rust/src/day10.rs | 32 ------------------ 2022/rust/src/day11.rs | 32 ------------------ 2022/rust/src/day12.rs | 32 ------------------ 2022/rust/src/day13.rs | 32 ------------------ 2022/rust/src/day14.rs | 32 ------------------ 2022/rust/src/day15.rs | 32 ------------------ 2022/rust/src/day16.rs | 32 ------------------ 2022/rust/src/day17.rs | 32 ------------------ 2022/rust/src/day18.rs | 32 ------------------ 2022/rust/src/day19.rs | 32 ------------------ 2022/rust/src/day20.rs | 32 ------------------ 2022/rust/src/day21.rs | 32 ------------------ 2022/rust/src/day22.rs | 32 ------------------ 2022/rust/src/day23.rs | 32 ------------------ 2022/rust/src/day24.rs | 32 ------------------ 2022/rust/src/day25.rs | 32 ------------------ 2022/rust/src/day4-alt1.rs | 69 ++++++++++++++++++++++++++++++++++++++ 2022/rust/src/day5.rs | 2 +- 2022/rust/src/day6.rs | 32 ------------------ 2022/rust/src/day7.rs | 32 ------------------ 2022/rust/src/day8.rs | 32 ------------------ 2022/rust/src/day9.rs | 32 ------------------ 2022/rust/src/scaffold.rs | 2 ++ 25 files changed, 79 insertions(+), 642 deletions(-) delete mode 100644 2022/rust/src/day10.rs delete mode 100644 2022/rust/src/day11.rs delete mode 100644 2022/rust/src/day12.rs delete mode 100644 2022/rust/src/day13.rs delete mode 100644 2022/rust/src/day14.rs delete mode 100644 2022/rust/src/day15.rs delete mode 100644 2022/rust/src/day16.rs delete mode 100644 2022/rust/src/day17.rs delete mode 100644 2022/rust/src/day18.rs delete mode 100644 2022/rust/src/day19.rs delete mode 100644 2022/rust/src/day20.rs delete mode 100644 2022/rust/src/day21.rs delete mode 100644 2022/rust/src/day22.rs delete mode 100644 2022/rust/src/day23.rs delete mode 100644 2022/rust/src/day24.rs delete mode 100644 2022/rust/src/day25.rs create mode 100644 2022/rust/src/day4-alt1.rs delete mode 100644 2022/rust/src/day6.rs delete mode 100644 2022/rust/src/day7.rs delete mode 100644 2022/rust/src/day8.rs delete mode 100644 2022/rust/src/day9.rs diff --git a/2022/rust/readme.md b/2022/rust/readme.md index d179c88..66f8e43 100644 --- a/2022/rust/readme.md +++ b/2022/rust/readme.md @@ -52,7 +52,7 @@ time them all: ```fish cargo build --release --bins -for f in (fd 'day.' target/release/ --type executable --max-depth 1); echo $f; time $f; end +time for f in (fd 'day.' target/release/ --type executable --max-depth 1); echo $f; time $f; end ``` [at]: https://git.lyte.dev/lytedev/dotfiles/src/branch/master/common/bin/at diff --git a/2022/rust/src/common.rs b/2022/rust/src/common.rs index 4c879da..6ce6d74 100644 --- a/2022/rust/src/common.rs +++ b/2022/rust/src/common.rs @@ -1,4 +1,5 @@ use std::env::var as envvar; +use std::fmt::Debug; use std::fs; use std::path::Path; @@ -9,3 +10,8 @@ pub fn day_input(day: u8) -> String { let file_path = path_buf.to_str().unwrap(); fs::read_to_string(file_path).unwrap() } + +pub fn show_answers(answer1: &impl Debug, answer2: &impl Debug) { + println!("Part 1: {:?}", answer1); + println!("Part 2: {:?}", answer2); +} diff --git a/2022/rust/src/day10.rs b/2022/rust/src/day10.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day10.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day11.rs b/2022/rust/src/day11.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day11.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day12.rs b/2022/rust/src/day12.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day12.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day13.rs b/2022/rust/src/day13.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day13.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day14.rs b/2022/rust/src/day14.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day14.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day15.rs b/2022/rust/src/day15.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day15.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day16.rs b/2022/rust/src/day16.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day16.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day17.rs b/2022/rust/src/day17.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day17.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day18.rs b/2022/rust/src/day18.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day18.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day19.rs b/2022/rust/src/day19.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day19.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day20.rs b/2022/rust/src/day20.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day20.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day21.rs b/2022/rust/src/day21.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day21.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day22.rs b/2022/rust/src/day22.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day22.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day23.rs b/2022/rust/src/day23.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day23.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day24.rs b/2022/rust/src/day24.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day24.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day25.rs b/2022/rust/src/day25.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day25.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day4-alt1.rs b/2022/rust/src/day4-alt1.rs new file mode 100644 index 0000000..c2f35d8 --- /dev/null +++ b/2022/rust/src/day4-alt1.rs @@ -0,0 +1,69 @@ +mod common; + +use std::ops::Range; + +struct AssignmentPair { + a: Range, + b: Range, +} + +impl AssignmentPair { + fn fully_contained(&self) -> bool { + (self.a.start <= self.b.start && self.a.end >= self.b.end) + || (self.b.start <= self.a.start && self.b.end >= self.a.end) + } + + fn overlap(&self) -> bool { + self.a.start <= self.b.end && self.b.start <= self.a.end + } +} + +impl From<&str> for AssignmentPair { + fn from(s: &str) -> Self { + let mut nums = s.split(&['-', ',']).map(|s| s.parse::().unwrap()); + Self { + a: nums.next().unwrap()..nums.next().unwrap(), + b: nums.next().unwrap()..nums.next().unwrap(), + } + } +} + +fn main() { + let text = common::day_input(4); + let input = process_input(&text); + common::show_answers(&part1(&input), &part2(&input)); +} + +fn process_input(input: &str) -> Vec { + input + .lines() + .map(|l| l.into()) + .collect::>() +} + +fn part1(input: &[AssignmentPair]) -> usize { + input.into_iter().filter(|p| p.fully_contained()).count() +} + +fn part2(input: &[AssignmentPair]) -> usize { + input.iter().filter(|p| p.overlap()).count() +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = "2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8"; + + #[test] + fn test() { + let input = process_input(TEST_INPUT); + assert_eq!(part1(&input), 2); + assert_eq!(part2(&input), 4); + } +} diff --git a/2022/rust/src/day5.rs b/2022/rust/src/day5.rs index bc0a3ce..4382952 100644 --- a/2022/rust/src/day5.rs +++ b/2022/rust/src/day5.rs @@ -1,7 +1,7 @@ mod common; fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); + let input = common::day_input(5); println!("Part 1: {}", part1(&input)); println!("Part 2: {}", part2(&input)); } diff --git a/2022/rust/src/day6.rs b/2022/rust/src/day6.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day6.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day7.rs b/2022/rust/src/day7.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day7.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day8.rs b/2022/rust/src/day8.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day8.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/day9.rs b/2022/rust/src/day9.rs deleted file mode 100644 index bc0a3ce..0000000 --- a/2022/rust/src/day9.rs +++ /dev/null @@ -1,32 +0,0 @@ -mod common; - -fn main() { - let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); - println!("Part 1: {}", part1(&input)); - println!("Part 2: {}", part2(&input)); -} - -fn part1(input: &str) -> i32 { - 0 -} - -fn part2(input: &str) -> i32 { - 0 -} - -#[cfg(test)] -mod tests { - use super::*; - - const TEST_INPUT: &str = ""; - - #[test] - fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) - } - - #[test] - fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) - } -} diff --git a/2022/rust/src/scaffold.rs b/2022/rust/src/scaffold.rs index bc0a3ce..e031169 100644 --- a/2022/rust/src/scaffold.rs +++ b/2022/rust/src/scaffold.rs @@ -1,5 +1,7 @@ mod common; +type Input = &str; + fn main() { let input = common::day_input(panic!("PUT THE CORRECT DAY NUMBER HERE")); println!("Part 1: {}", part1(&input));