Days 4 and 5 and rust. This is really REALLY shitty

This commit is contained in:
2022-12-06 00:23:18 +00:00
parent d491307031
commit dd4ba849f2
6 changed files with 1740 additions and 0 deletions

1000
rust/4/input.txt Normal file

File diff suppressed because it is too large Load Diff

48
rust/4/part1.rs Normal file
View 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
View 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);
}