finished day 4.

This commit is contained in:
gabe 2022-12-04 00:41:09 -06:00
parent 05d08c134b
commit c92c8843e9
15 changed files with 1188 additions and 19 deletions

7
Cargo.lock generated
View file

@ -6,6 +6,7 @@ version = 3
name = "advent_of_code_2022" name = "advent_of_code_2022"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"once_cell",
"regex", "regex",
] ]
@ -24,6 +25,12 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "once_cell"
version = "1.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.7.0" version = "1.7.0"

View file

@ -26,5 +26,10 @@ path="src/day03/solve.rs"
name="day04" name="day04"
path="src/day04/solve.rs" path="src/day04/solve.rs"
[[bin]]
name="day05"
path="src/day05/solve.rs"
[dependencies] [dependencies]
regex="1" regex="1"
once_cell = "1.16.0"

File diff suppressed because it is too large Load diff

View file

@ -1,7 +1,10 @@
use crate::utilities::*; use crate::utilities::*;
pub fn part1() -> usize { pub fn part1(input: &[(Range, Range)]) -> usize {
0 input
.iter()
.filter(|tuple| tuple.0.complete_overlap(&tuple.1))
.count()
} }
#[cfg(test)] #[cfg(test)]
@ -10,6 +13,15 @@ mod tests {
#[test] #[test]
fn test_part1() { fn test_part1() {
assert_eq!(part1(), 0); let input = vec![
(Range::new(2, 4), Range::new(6, 8)),
(Range::new(2, 3), Range::new(4, 5)),
(Range::new(5, 7), Range::new(7, 9)),
(Range::new(2, 8), Range::new(3, 7)),
(Range::new(6, 6), Range::new(4, 6)),
(Range::new(2, 6), Range::new(4, 8)),
(Range::new(19, 30), Range::new(5, 18)),
];
assert_eq!(part1(&input), 2);
} }
} }

View file

@ -1,7 +1,10 @@
use crate::utilities::*; use crate::utilities::*;
pub fn part2() -> usize { pub fn part2(input: &[(Range, Range)]) -> usize {
0 input
.iter()
.filter(|tuple| tuple.0.any_overlap(&tuple.1))
.count()
} }
#[cfg(test)] #[cfg(test)]
@ -10,6 +13,15 @@ mod tests {
#[test] #[test]
fn test_part2() { fn test_part2() {
assert_eq!(part2(), 0); let input = vec![
(Range::new(2, 4), Range::new(6, 8)),
(Range::new(2, 3), Range::new(4, 5)),
(Range::new(5, 7), Range::new(7, 9)),
(Range::new(2, 8), Range::new(3, 7)),
(Range::new(6, 6), Range::new(4, 6)),
(Range::new(2, 6), Range::new(4, 8)),
(Range::new(19, 30), Range::new(5, 18)),
];
assert_eq!(part2(&input), 4);
} }
} }

View file

@ -7,8 +7,8 @@ fn main() {
let _structured_input = utilities::parse(_input); let _structured_input = utilities::parse(_input);
println!("Part One"); println!("Part One");
println!("Result: {}", part1::part1()); println!("Result: {}", part1::part1(&_structured_input));
println!("Part Two"); println!("Part Two");
println!("Result: {}", part2::part2()); println!("Result: {}", part2::part2(&_structured_input));
} }

View file

@ -1,6 +1,65 @@
pub fn parse(input: &str) -> usize { use once_cell::sync::Lazy;
println!("{}", input); use regex::Regex;
0 #[derive(Debug, PartialEq, Eq)]
pub struct Range {
start: u16,
end: u16,
}
impl Range {
pub fn new(start: u16, end: u16) -> Self {
Self {
start: start.min(end),
end: end.max(start),
}
}
pub fn calc_size(&self) -> u16 {
self.start.abs_diff(self.end)
}
pub fn any_overlap(&self, other: &Self) -> bool {
self.start <= other.end && self.end >= other.start
}
pub fn calc_overlap(&self, other: &Self) -> Range {
let overlap_start = self.start.min(other.start);
let overlap_end = self.end.max(other.end);
Range::new(overlap_start, overlap_end)
}
pub fn complete_overlap(&self, other: &Self) -> bool {
self.calc_overlap(other) == *self || self.calc_overlap(other) == *other
}
pub fn start(&self) -> u16 {
self.start
}
pub fn end(&self) -> u16 {
self.end
}
}
static PARSE_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^(\d+)-(\d+),(\d+)-(\d+)").unwrap());
pub fn parse(input: &str) -> Vec<(Range, Range)> {
input
.lines()
.map(|line| {
let cap = PARSE_REGEX.captures(line).unwrap();
(
Range::new(
cap.get(1).unwrap().as_str().parse().unwrap(),
cap.get(2).unwrap().as_str().parse().unwrap(),
),
Range::new(
cap.get(3).unwrap().as_str().parse().unwrap(),
cap.get(4).unwrap().as_str().parse().unwrap(),
),
)
})
.collect()
} }
#[cfg(test)] #[cfg(test)]
@ -9,9 +68,24 @@ mod tests {
#[test] #[test]
fn test_parse() { fn test_parse() {
let input = let input = "2-4,6-8
"test" 2-3,4-5
; 5-7,7-9
assert_eq!(parse(input), 0); 2-8,3-7
6-6,4-6
2-6,4-8
19-30,5-18";
assert_eq!(
parse(input),
vec![
(Range::new(2, 4), Range::new(6, 8)),
(Range::new(2, 3), Range::new(4, 5)),
(Range::new(5, 7), Range::new(7, 9)),
(Range::new(2, 8), Range::new(3, 7)),
(Range::new(6, 6), Range::new(4, 6)),
(Range::new(2, 6), Range::new(4, 8)),
(Range::new(19, 30), Range::new(5, 18)),
]
);
} }
} }

0
src/day05/input.txt Normal file
View file

15
src/day05/part1.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::utilities::*;
pub fn part1() -> usize {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part1() {
assert_eq!(part1(), 0);
}
}

15
src/day05/part2.rs Normal file
View file

@ -0,0 +1,15 @@
use crate::utilities::*;
pub fn part2() -> usize {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part2() {
assert_eq!(part2(), 0);
}
}

14
src/day05/solve.rs Normal file
View file

@ -0,0 +1,14 @@
mod part1;
mod part2;
mod utilities;
fn main() {
let _input = include_str!("./input.txt");
let _structured_input = utilities::parse(_input);
println!("Part One");
println!("Result: {}", part1::part1());
println!("Part Two");
println!("Result: {}", part2::part2());
}

16
src/day05/utilities.rs Normal file
View file

@ -0,0 +1,16 @@
pub fn parse(input: &str) -> usize {
unimplemented!()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse() {
let input =
"test"
;
assert_eq!(parse(input), 0);
}
}

View file

@ -1,7 +1,7 @@
use crate::utilities::*; use crate::utilities::*;
pub fn part1() -> usize { pub fn part1() -> usize {
0 unimplemented!()
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,7 +1,7 @@
use crate::utilities::*; use crate::utilities::*;
pub fn part2() -> usize { pub fn part2() -> usize {
0 unimplemented!()
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,6 +1,5 @@
pub fn parse(input: &str) -> usize { pub fn parse(input: &str) -> usize {
println!("{}", input); unimplemented!()
0
} }
#[cfg(test)] #[cfg(test)]