Compare commits
2 Commits
0e22e82652
...
e1cf0cf53f
Author | SHA1 | Date | |
---|---|---|---|
e1cf0cf53f | |||
96fab0b0a7 |
1000
2022/input/day04.txt
Executable file
1000
2022/input/day04.txt
Executable file
File diff suppressed because it is too large
Load Diff
52
2022/src/day04.rs
Normal file
52
2022/src/day04.rs
Normal 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,
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,12 +2,12 @@ use std::env;
|
||||||
use std::io;
|
use std::io;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
const LATEST: u8 = 3;
|
const LATEST: u8 = 4;
|
||||||
|
|
||||||
mod day01;
|
mod day01;
|
||||||
mod day02;
|
mod day02;
|
||||||
mod day03;
|
mod day03;
|
||||||
// mod day04;
|
mod day04;
|
||||||
// mod day05;
|
// mod day05;
|
||||||
// mod day06;
|
// mod day06;
|
||||||
// mod day07;
|
// mod day07;
|
||||||
|
@ -36,7 +36,7 @@ fn main() -> io::Result<()> {
|
||||||
1 => day01::execute(),
|
1 => day01::execute(),
|
||||||
2 => day02::execute(),
|
2 => day02::execute(),
|
||||||
3 => day03::execute(),
|
3 => day03::execute(),
|
||||||
// 4 => day04::execute(),
|
4 => day04::execute(),
|
||||||
// 5 => day05::execute(),
|
// 5 => day05::execute(),
|
||||||
// 6 => day06::execute(),
|
// 6 => day06::execute(),
|
||||||
// 7 => day07::execute(),
|
// 7 => day07::execute(),
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::io;
|
||||||
use crate::utils;
|
use crate::utils;
|
||||||
|
|
||||||
pub fn execute() -> Result<(), io::Error> {
|
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?;
|
let text = line?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user