day 4
This commit is contained in:
parent
8e55e961cb
commit
c67298eb5a
8
day4/Cargo.toml
Normal file
8
day4/Cargo.toml
Normal 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
1000
day4/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
day4/sample_input.txt
Normal file
6
day4/sample_input.txt
Normal 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
34
day4/src/main.rs
Normal 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}");
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user