just clean it up
This commit is contained in:
parent
63eaad3ad2
commit
c2cd2efe3d
1 changed files with 17 additions and 63 deletions
|
@ -1,59 +1,24 @@
|
||||||
use std::ops::RangeInclusive;
|
|
||||||
|
|
||||||
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
|
use aoc_runner_derive::{aoc as aoc_run, aoc_generator};
|
||||||
|
|
||||||
type Contents = (RangeInclusive<u32>, RangeInclusive<u32>);
|
type Assignments = ((u32, u32), (u32, u32));
|
||||||
|
|
||||||
#[aoc_generator(day4)]
|
#[aoc_generator(day4)]
|
||||||
fn parse_input(input: &str) -> Vec<Contents> {
|
fn parse_input(input: &str) -> Vec<Assignments> {
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// with tuples instead of ranges
|
|
||||||
type AltContents = ((u32, u32), (u32, u32));
|
|
||||||
|
|
||||||
#[aoc_generator(day4, part1, alt1)]
|
|
||||||
fn parse_input1a(input: &str) -> Vec<AltContents> {
|
|
||||||
let mut out = Vec::with_capacity(input.len());
|
let mut out = Vec::with_capacity(input.len());
|
||||||
for line in input.lines() {
|
for line in input.lines() {
|
||||||
let (a, b) = line.split_once(',').unwrap();
|
let (a, b) = line.split_once(',').unwrap();
|
||||||
let (alo, ahi) = a.split_once('-').unwrap();
|
let (a, b) = (get_range(a), get_range(b));
|
||||||
let (alo, ahi) = (alo.parse().unwrap(), ahi.parse().unwrap());
|
out.push((a, b));
|
||||||
let (blo, bhi) = b.split_once('-').unwrap();
|
|
||||||
let (blo, bhi) = (blo.parse().unwrap(), bhi.parse().unwrap());
|
|
||||||
out.push(((alo, ahi), (blo, bhi)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
// can't define a blanket alt generator?
|
|
||||||
#[aoc_generator(day4, part2, alt1)]
|
|
||||||
fn p2a(input: &str) -> Vec<AltContents> {
|
|
||||||
parse_input1a(input)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[aoc_run(day4, part1)]
|
#[aoc_run(day4, part1)]
|
||||||
fn part1(scheds: &[Contents]) -> u32 {
|
fn part1(scheds: &[Assignments]) -> u32 {
|
||||||
let mut out = 0;
|
let mut out = 0;
|
||||||
for (a, b) in scheds {
|
for (a, b) in scheds {
|
||||||
if (a.contains(b.start()) && a.contains(b.end()))
|
if contains(a, b) || contains(b, a) {
|
||||||
|| (b.contains(a.start()) && b.contains(a.end()))
|
|
||||||
{
|
|
||||||
out += 1;
|
out += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,36 +26,25 @@ fn part1(scheds: &[Contents]) -> u32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_run(day4, part2)]
|
#[aoc_run(day4, part2)]
|
||||||
fn part2(scheds: &[Contents]) -> u32 {
|
fn part2(scheds: &[Assignments]) -> u32 {
|
||||||
let mut out = 0;
|
let mut out = 0;
|
||||||
for (a, b) in scheds {
|
for (a, b) in scheds {
|
||||||
if (a.contains(b.start()) || a.contains(b.end()))
|
if overlaps(a, b) || overlaps(b, a) {
|
||||||
|| (b.contains(a.start()) || b.contains(a.end()))
|
|
||||||
{
|
|
||||||
out += 1;
|
out += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_run(day4, part1, alt1)]
|
fn get_range(r: &str) -> (u32, u32) {
|
||||||
fn part1a(scheds: &[AltContents]) -> u32 {
|
let (lo, hi) = r.split_once('-').unwrap();
|
||||||
let mut out = 0;
|
(lo.parse().unwrap(), hi.parse().unwrap())
|
||||||
for (a, b) in scheds {
|
|
||||||
if (a.0 <= b.0 && b.1 <= a.1) || (b.0 <= a.0 && a.1 <= b.1) {
|
|
||||||
out += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc_run(day4, part2, alt1)]
|
fn contains(a: &(u32, u32), b: &(u32, u32)) -> bool {
|
||||||
fn part2a(scheds: &[AltContents]) -> u32 {
|
a.0 <= b.0 && b.1 <= a.1
|
||||||
let mut out = 0;
|
|
||||||
for (a, b) in scheds {
|
|
||||||
if (a.0 <= b.0 && b.0 <= a.1) || (b.0 <= a.0 && a.0 <= b.1) {
|
|
||||||
out += 1;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
out
|
fn overlaps(a: &(u32, u32), b: &(u32, u32)) -> bool {
|
||||||
|
a.0 <= b.0 && b.0 <= a.1
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue