1
0
This commit is contained in:
Jack Bond-Preston 2022-12-23 19:34:36 +00:00
parent 8e55e961cb
commit c67298eb5a
Signed by: jack
GPG Key ID: 010071F1482BA852
4 changed files with 1048 additions and 0 deletions

8
day4/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day4"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1000
day4/input.txt Normal file

File diff suppressed because it is too large Load Diff

6
day4/sample_input.txt Normal file
View File

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

34
day4/src/main.rs Normal file
View File

@ -0,0 +1,34 @@
use std::{collections::HashSet, cmp::min};
fn main() {
const INPUT: &str = include_str!("../input.txt");
let input = INPUT.lines();
let mut count_complete: u64 = 0;
let mut count_partial: u64 = 0;
for line in input {
let (first, second) = line.split_once(',').unwrap();
let first: (&str, &str) = first.split_once('-').unwrap();
let first: (u64, u64) = (first.0.parse::<u64>().unwrap(), first.1.parse::<u64>().unwrap());
let second: (&str, &str) = second.split_once('-').unwrap();
let second: (u64, u64) = (second.0.parse::<u64>().unwrap(), second.1.parse::<u64>().unwrap());
let first_set: HashSet<u64> = HashSet::from_iter(first.0..=first.1);
let second_set: HashSet<u64> = HashSet::from_iter(second.0..=second.1);
let smallest_len = min(first_set.len(), second_set.len());
let intersection_len = first_set.intersection(&second_set).count();
if intersection_len == smallest_len {
count_complete += 1;
}
if intersection_len > 0 {
count_partial += 1;
}
}
println!("Partial: {count_partial}, Complete: {count_complete}");
}