part2 done
This commit is contained in:
parent
d2dac0ce84
commit
8dc7f8bf53
1 changed files with 50 additions and 0 deletions
50
2022-aoc/src/d4.rs
Normal file
50
2022-aoc/src/d4.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use std::ops::RangeInclusive;
|
||||
|
||||
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
|
||||
|
||||
type Contents = (RangeInclusive<u32>, RangeInclusive<u32>);
|
||||
|
||||
#[aoc_generator(day4)]
|
||||
fn parse_input(input: &str) -> Vec<Contents> {
|
||||
let mut out = Vec::with_capacity(input.len());
|
||||
for line in input.lines() {
|
||||
let mut split = line.split(',');
|
||||
let a = split.next().unwrap();
|
||||
let b = split.next().unwrap();
|
||||
let mut asplit = a.split('-');
|
||||
let mut bsplit = b.split('-');
|
||||
let alo = asplit.next().unwrap().parse().unwrap();
|
||||
let ahi = asplit.next().unwrap().parse().unwrap();
|
||||
let blo = bsplit.next().unwrap().parse().unwrap();
|
||||
let bhi = bsplit.next().unwrap().parse().unwrap();
|
||||
out.push(((alo..=ahi), (blo..=bhi)));
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
#[aoc_run(day4, part1)]
|
||||
fn part1(scheds: &[Contents]) -> u32 {
|
||||
let mut out = 0;
|
||||
for (a, b) in scheds {
|
||||
if (a.contains(b.start()) && a.contains(b.end()))
|
||||
|| (b.contains(a.start()) && b.contains(a.end()))
|
||||
{
|
||||
out += 1;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[aoc_run(day4, part2)]
|
||||
fn part2(scheds: &[Contents]) -> u32 {
|
||||
let mut out = 0;
|
||||
for (a, b) in scheds {
|
||||
if (a.contains(b.start()) || a.contains(b.end()))
|
||||
|| (b.contains(a.start()) || b.contains(a.end()))
|
||||
{
|
||||
out += 1;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
Loading…
Reference in a new issue