aoc2022 day 04
This commit is contained in:
parent
9c21f2eae4
commit
228a746208
1001
src/aoc2022/day04/input.txt
Normal file
1001
src/aoc2022/day04/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
61
src/aoc2022/day04/mod.rs
Normal file
61
src/aoc2022/day04/mod.rs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
struct Assignment {
|
||||||
|
lower_border: usize,
|
||||||
|
upper_border: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Assignment {
|
||||||
|
fn contains(&self, other: &Assignment) -> bool {
|
||||||
|
self.lower_border <= other.lower_border && self.upper_border >= other.upper_border
|
||||||
|
}
|
||||||
|
|
||||||
|
fn overlaps(&self, other: &Assignment) -> bool {
|
||||||
|
let overlaps_lower = other.upper_border >= self.lower_border && other.upper_border <= self.lower_border;
|
||||||
|
let overlaps_upper = other.lower_border >= self.lower_border && other.lower_border <= self.upper_border;
|
||||||
|
|
||||||
|
overlaps_lower || overlaps_upper
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse(input: &str) -> Assignment {
|
||||||
|
match input.trim().split_once("-") {
|
||||||
|
Some((low, upp)) => Assignment {
|
||||||
|
lower_border: low.parse::<usize>().unwrap(),
|
||||||
|
upper_border: upp.parse::<usize>().unwrap(),
|
||||||
|
},
|
||||||
|
None => panic!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task_one(input: &str) -> String {
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
for line in input.lines() {
|
||||||
|
if let Some((first, second)) = line.split_once(",") {
|
||||||
|
let first = Assignment::parse(first);
|
||||||
|
let second = Assignment::parse(second);
|
||||||
|
|
||||||
|
if first.contains(&second) || second.contains(&first) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
count.to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task_two(input: &str) -> String {
|
||||||
|
let mut count = 0;
|
||||||
|
|
||||||
|
for line in input.lines() {
|
||||||
|
if let Some((first, second)) = line.split_once(",") {
|
||||||
|
let first = Assignment::parse(first);
|
||||||
|
let second = Assignment::parse(second);
|
||||||
|
|
||||||
|
if first.overlaps(&second) || second.overlaps(&first) {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
count.to_string()
|
||||||
|
}
|
@ -1,3 +1,4 @@
|
|||||||
pub mod day01;
|
pub mod day01;
|
||||||
pub mod day02;
|
pub mod day02;
|
||||||
pub mod day03;
|
pub mod day03;
|
||||||
|
pub mod day04;
|
||||||
|
@ -45,4 +45,7 @@ fn main() {
|
|||||||
|
|
||||||
puzzle = Puzzle { day: 3, year: 2022, task_one: aoc2022::day03::task_one, task_two: aoc2022::day03::task_two };
|
puzzle = Puzzle { day: 3, year: 2022, task_one: aoc2022::day03::task_one, task_two: aoc2022::day03::task_two };
|
||||||
puzzle.solve_and_print();
|
puzzle.solve_and_print();
|
||||||
|
|
||||||
|
puzzle = Puzzle { day: 4, year: 2022, task_one: aoc2022::day04::task_one, task_two: aoc2022::day04::task_two };
|
||||||
|
puzzle.solve_and_print();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user