d4
This commit is contained in:
parent
72c1ab9f9e
commit
33593739d8
7
day4/Cargo.lock
generated
Normal file
7
day4/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day4"
|
||||
version = "0.1.0"
|
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
25
day4/src/main.rs
Normal file
25
day4/src/main.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap();
|
||||
let mut sum = 0;
|
||||
|
||||
for line in input.lines() {
|
||||
let (s1, e1, s2, e2) = readline(line);
|
||||
if s1 <= e2 && s2 <= e1 {
|
||||
sum += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("Total: {}", sum);
|
||||
}
|
||||
|
||||
fn readline(line: &str) -> (i64, i64, i64, i64) {
|
||||
let nums: Vec<i64> = line
|
||||
.split(['-', ','])
|
||||
.map(|line| line.parse::<i64>().unwrap())
|
||||
.collect();
|
||||
(nums[0], nums[1], nums[2], nums[3])
|
||||
}
|
||||
|
25
day4/src/main1.rs
Normal file
25
day4/src/main1.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let input = fs::read_to_string(&env::args().nth(1).unwrap()).unwrap();
|
||||
let mut sum = 0;
|
||||
|
||||
for line in input.lines() {
|
||||
let (s1, e1, s2, e2) = readline(line);
|
||||
if (s1 <= s2 && e1 >= e2) || (s1 >= s2 && e1 <= e2) {
|
||||
sum += 1;
|
||||
}
|
||||
}
|
||||
|
||||
println!("Total: {}", sum);
|
||||
}
|
||||
|
||||
fn readline(line: &str) -> (i64, i64, i64, i64) {
|
||||
let nums: Vec<i64> = line
|
||||
.split(['-', ','])
|
||||
.map(|line| line.parse::<i64>().unwrap())
|
||||
.collect();
|
||||
(nums[0], nums[1], nums[2], nums[3])
|
||||
}
|
||||
|
6
day4/test.txt
Normal file
6
day4/test.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
|
Loading…
Reference in New Issue
Block a user