Days 4 and 5 and rust. This is really REALLY shitty
This commit is contained in:
1000
rust/4/input.txt
Normal file
1000
rust/4/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
48
rust/4/part1.rs
Normal file
48
rust/4/part1.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
};
|
||||
|
||||
struct Pair {
|
||||
low: i32,
|
||||
high: i32,
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
fn new(input: &str) -> Self {
|
||||
let tmpvec = input
|
||||
.split('-')
|
||||
.map(|val| val.parse::<i32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
return Self {
|
||||
low: tmpvec[0],
|
||||
high: tmpvec[1],
|
||||
};
|
||||
}
|
||||
|
||||
fn contains_fully(self: &Self, other: &Self) -> bool {
|
||||
self.low <= other.low && self.high >= other.high
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> () {
|
||||
let path = "input.txt";
|
||||
|
||||
let input = File::open(path).unwrap();
|
||||
let buf = BufReader::new(input);
|
||||
|
||||
let score: i32 = buf
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let line = line.unwrap();
|
||||
let pairs = line
|
||||
.split(',')
|
||||
.map(|pair_str| Pair::new(pair_str))
|
||||
.collect::<Vec<_>>();
|
||||
pairs[0].contains_fully(&pairs[1]) || pairs[1].contains_fully(&pairs[0])
|
||||
})
|
||||
.map(|b| if b { 1 } else { 0 })
|
||||
.sum();
|
||||
|
||||
println!("{}", score);
|
||||
}
|
51
rust/4/part2.rs
Normal file
51
rust/4/part2.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
};
|
||||
|
||||
struct Pair {
|
||||
low: i32,
|
||||
high: i32,
|
||||
}
|
||||
|
||||
impl Pair {
|
||||
fn new(input: &str) -> Self {
|
||||
let tmpvec = input
|
||||
.split('-')
|
||||
.map(|val| val.parse::<i32>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
return Self {
|
||||
low: tmpvec[0],
|
||||
high: tmpvec[1],
|
||||
};
|
||||
}
|
||||
|
||||
fn contains_fully(self: &Self, other: &Self) -> bool {
|
||||
self.low <= other.low && self.high >= other.high
|
||||
}
|
||||
fn contains_partially(self: &Self, other: &Self) -> bool {
|
||||
self.low <= other.high && self.high >= other.low
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> () {
|
||||
let path = "input.txt";
|
||||
|
||||
let input = File::open(path).unwrap();
|
||||
let buf = BufReader::new(input);
|
||||
|
||||
let score: i32 = buf
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let line = line.unwrap();
|
||||
let pairs = line
|
||||
.split(',')
|
||||
.map(|pair_str| Pair::new(pair_str))
|
||||
.collect::<Vec<_>>();
|
||||
pairs[0].contains_partially(&pairs[1])
|
||||
})
|
||||
.map(|b| if b { 1 } else { 0 })
|
||||
.sum();
|
||||
|
||||
println!("{}", score);
|
||||
}
|
Reference in New Issue
Block a user