This commit is contained in:
Daniel Flanagan 2022-12-04 01:25:36 -06:00
parent 5118d766b3
commit dfb1b25c48
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
25 changed files with 79 additions and 642 deletions

View file

@ -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

View file

@ -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);
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -0,0 +1,69 @@
mod common;
use std::ops::Range;
struct AssignmentPair {
a: Range<i32>,
b: Range<i32>,
}
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::<i32>().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<AssignmentPair> {
input
.lines()
.map(|l| l.into())
.collect::<Vec<AssignmentPair>>()
}
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);
}
}

View file

@ -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));
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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)
}
}

View file

@ -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));