add day 4, part 1

This commit is contained in:
Christian Ulrich 2022-12-04 15:09:10 +01:00
parent ef54a885e2
commit 7b4b22391c
No known key found for this signature in database
GPG Key ID: 8241BE099775A097
4 changed files with 1107 additions and 0 deletions

67
day04/common.rs Normal file
View File

@ -0,0 +1,67 @@
// Copyright 2022 Christian Ulrich
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
use std::io;
use std::ops::RangeInclusive;
pub struct Assignments {}
#[derive(Debug)]
pub struct Assignment {
range1: RangeInclusive<u8>,
range2: RangeInclusive<u8>,
}
impl Iterator for Assignments {
type Item = Result<Assignment, &'static str>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(line) = io::stdin().lines().next() {
let line = line.unwrap();
if line.len() > 0 {
if let Some((left, right)) = line.split_once(',') {
if let (Some((s1, e1)), Some((s2, e2))) =
(left.split_once('-'), right.split_once('-'))
{
if let (Ok(s1), Ok(e1), Ok(s2), Ok(e2)) = (
s1.parse::<u8>(),
e1.parse::<u8>(),
s2.parse::<u8>(),
e2.parse::<u8>(),
) {
return Some(Ok(Assignment::new(s1..=e1, s2..=e2)));
}
}
}
return Some(Err("input file contains invalid line"));
}
}
None
}
}
impl Assignment {
pub fn new(range1: RangeInclusive<u8>, range2: RangeInclusive<u8>) -> Self {
Self {
range1: range1,
range2: range2,
}
}
pub fn has_obsolete_range(&self) -> bool {
self.range1.contains(self.range2.start()) && self.range1.contains(self.range2.end())
|| self.range2.contains(self.range1.start()) && self.range2.contains(self.range1.end())
}
}

1001
day04/input Normal file

File diff suppressed because it is too large Load Diff

32
day04/part1.rs Normal file
View File

@ -0,0 +1,32 @@
// Copyright 2022 Christian Ulrich
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// usage: ./part1 < input
pub mod common;
use common::Assignments;
fn main() -> Result<(), &'static str> {
let assignments = Assignments {};
let mut result = 0;
for assignment in assignments {
if assignment?.has_obsolete_range() {
result += 1;
}
}
println!("{result}");
Ok(())
}

7
day04/testinput Normal file
View File

@ -0,0 +1,7 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8