Compare commits

..

2 Commits

Author SHA1 Message Date
e1cf0cf53f Template update. 2024-02-17 20:11:41 +00:00
96fab0b0a7 Day 04 solution. 2024-02-17 20:11:27 +00:00
4 changed files with 1056 additions and 4 deletions

1000
2022/input/day04.txt Executable file

File diff suppressed because it is too large Load Diff

52
2022/src/day04.rs Normal file
View File

@ -0,0 +1,52 @@
use std::io;
use std::vec::Vec;
use crate::utils;
struct Assignment {
min: u32,
max: u32,
}
pub fn execute() -> Result<(), io::Error> {
let mut subsets = 0;
let mut overlaps = 0;
for line in utils::read_lines("input/day04.txt")? {
let text = line?;
let inputs: Vec<_> = text.split(',').collect();
let ass1 = parse_assignment(inputs[0]);
let ass2 = parse_assignment(inputs[1]);
if (ass2.max >= ass1.max && ass2.min <= ass1.min) ||
(ass1.max >= ass2.max && ass1.min <= ass2.min) {
subsets += 1;
}
if (ass2.max >= ass1.min && ass2.min <= ass1.max) ||
(ass1.max >= ass2.min && ass1.min <= ass2.max) {
overlaps += 1;
}
}
utils::print_step(1, subsets);
utils::print_step(2, overlaps);
Ok(())
}
fn parse_assignment(input: &str) -> Assignment {
let values: Vec<_> = input.split('-').collect();
let Ok(min) = values[0].parse::<u32>() else {
panic!("Min value {} is non-numeric.", values[0])
};
let Ok(max) = values[1].parse::<u32>() else {
panic!("Max value {} is non-numeric.", values[1])
};
Assignment {
min: min,
max: max,
}
}

View File

@ -2,12 +2,12 @@ use std::env;
use std::io;
mod utils;
const LATEST: u8 = 3;
const LATEST: u8 = 4;
mod day01;
mod day02;
mod day03;
// mod day04;
mod day04;
// mod day05;
// mod day06;
// mod day07;
@ -36,7 +36,7 @@ fn main() -> io::Result<()> {
1 => day01::execute(),
2 => day02::execute(),
3 => day03::execute(),
// 4 => day04::execute(),
4 => day04::execute(),
// 5 => day05::execute(),
// 6 => day06::execute(),
// 7 => day07::execute(),

View File

@ -3,7 +3,7 @@ use std::io;
use crate::utils;
pub fn execute() -> Result<(), io::Error> {
for line in utils::read_lines("input/day03.txt")? {
for line in utils::read_lines("input/dayXX.txt")? {
let text = line?;
}