finished day 4.
This commit is contained in:
parent
05d08c134b
commit
c92c8843e9
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -6,6 +6,7 @@ version = 3
|
|||
name = "advent_of_code_2022"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"regex",
|
||||
]
|
||||
|
||||
|
@ -24,6 +25,12 @@ version = "2.5.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.7.0"
|
||||
|
|
|
@ -26,5 +26,10 @@ path="src/day03/solve.rs"
|
|||
name="day04"
|
||||
path="src/day04/solve.rs"
|
||||
|
||||
[[bin]]
|
||||
name="day05"
|
||||
path="src/day05/solve.rs"
|
||||
|
||||
[dependencies]
|
||||
regex="1"
|
||||
once_cell = "1.16.0"
|
||||
|
|
1000
src/day04/input.txt
1000
src/day04/input.txt
File diff suppressed because it is too large
Load diff
|
@ -1,7 +1,10 @@
|
|||
use crate::utilities::*;
|
||||
|
||||
pub fn part1() -> usize {
|
||||
0
|
||||
pub fn part1(input: &[(Range, Range)]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.filter(|tuple| tuple.0.complete_overlap(&tuple.1))
|
||||
.count()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -10,6 +13,15 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
use crate::utilities::*;
|
||||
|
||||
pub fn part2() -> usize {
|
||||
0
|
||||
pub fn part2(input: &[(Range, Range)]) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.filter(|tuple| tuple.0.any_overlap(&tuple.1))
|
||||
.count()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -10,6 +13,15 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,8 +7,8 @@ fn main() {
|
|||
let _structured_input = utilities::parse(_input);
|
||||
|
||||
println!("Part One");
|
||||
println!("Result: {}", part1::part1());
|
||||
println!("Result: {}", part1::part1(&_structured_input));
|
||||
|
||||
println!("Part Two");
|
||||
println!("Result: {}", part2::part2());
|
||||
println!("Result: {}", part2::part2(&_structured_input));
|
||||
}
|
||||
|
|
|
@ -1,6 +1,65 @@
|
|||
pub fn parse(input: &str) -> usize {
|
||||
println!("{}", input);
|
||||
0
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
#[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)]
|
||||
|
@ -9,9 +68,24 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_parse() {
|
||||
let input =
|
||||
"test"
|
||||
;
|
||||
assert_eq!(parse(input), 0);
|
||||
let input = "2-4,6-8
|
||||
2-3,4-5
|
||||
5-7,7-9
|
||||
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
0
src/day05/input.txt
Normal file
15
src/day05/part1.rs
Normal file
15
src/day05/part1.rs
Normal 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
15
src/day05/part2.rs
Normal 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
14
src/day05/solve.rs
Normal 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
16
src/day05/utilities.rs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -1,7 +1,7 @@
|
|||
use crate::utilities::*;
|
||||
|
||||
pub fn part1() -> usize {
|
||||
0
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::utilities::*;
|
||||
|
||||
pub fn part2() -> usize {
|
||||
0
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
pub fn parse(input: &str) -> usize {
|
||||
println!("{}", input);
|
||||
0
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue