1
0
Fork 0

First star complete

This commit is contained in:
Vivianne 2022-12-07 19:14:55 -08:00
parent 0272d77d6b
commit b14a2306c1
3 changed files with 1064 additions and 1 deletions

View file

@ -2,7 +2,7 @@
name = "aoc-vv"
version = "0.1.0"
edition = "2021"
default-run = "p03"
default-run = "p04"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

1000
etc/p04.txt Normal file

File diff suppressed because it is too large Load diff

63
src/bin/p04.rs Normal file
View file

@ -0,0 +1,63 @@
#![feature(iterator_try_collect, iter_collect_into)]
use std::fs;
use std::io::{self, BufRead};
use std::ops::Range;
use std::str::FromStr;
#[derive(Debug)]
struct WorkRange(Range<u32>);
impl WorkRange {
fn fully_contains(&self, other: &Self) -> bool {
self.0.start <= other.0.start && self.0.end >= other.0.end
}
}
impl FromStr for WorkRange {
type Err = <u32 as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let spl: Vec<u32> = s.split('-').map(|n| n.parse()).try_collect()?;
Ok(WorkRange(spl[0]..spl[1]))
}
}
#[derive(Debug)]
struct ElfPair(WorkRange, WorkRange);
impl FromStr for ElfPair {
type Err = <WorkRange as FromStr>::Err;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let spl: Vec<&str> = s.split(',').collect();
Ok(ElfPair(spl[0].parse()?, spl[1].parse()?))
}
}
fn main() {
let filename = "etc/p04.txt";
let file = fs::File::open(filename).expect("Can't open file");
let reader = io::BufReader::new(file);
println!("PART ONE ---------------");
let pairs: Vec<ElfPair> = reader
.lines()
.flat_map(|l| l.expect("Expect valid line").parse())
.collect();
part1(&pairs);
println!("\n PART TWO ---------------");
part2(&pairs);
}
fn part1(pairs: &[ElfPair]) {
let sum = pairs
.iter()
.filter(|ElfPair(l, r)| l.fully_contains(r) || r.fully_contains(l))
.count();
println!("num containing: {}", sum);
}
fn part2(_pairs: &[ElfPair]) {}