Cleaned up a bit

This commit is contained in:
Daniel Flanagan 2022-12-04 00:00:56 -06:00
parent f1250271ea
commit 5118d766b3
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -6,47 +6,32 @@ fn main() {
println!("Part 2: {}", part2(&input));
}
fn part1(input: &str) -> i32 {
let mut r = 0;
for l in input.lines() {
let mut c = l.split(",");
let p1 = c.next().unwrap();
let p2 = c.next().unwrap();
let mut p1c = p1.split("-");
let p1n1 = p1c.next().unwrap().parse::<i32>().unwrap();
let p1n2 = p1c.next().unwrap().parse::<i32>().unwrap();
let mut p2c = p2.split("-");
let p2n1 = p2c.next().unwrap().parse::<i32>().unwrap();
let p2n2 = p2c.next().unwrap().parse::<i32>().unwrap();
fn nums(input: &str) -> Vec<i32> {
input
.replace(",", "-")
.split("-")
.map(|s| s.parse::<i32>().unwrap())
.collect()
}
if (p1n1 <= p2n1 && p1n2 >= p2n2) || (p2n1 <= p1n1 && p2n2 >= p1n2) {
r += 1
}
}
r
fn part1(input: &str) -> i32 {
input
.lines()
.map(|l| {
let n = nums(l);
((n[0] <= n[2] && n[1] >= n[3]) || (n[2] <= n[0] && n[3] >= n[1])) as i32
})
.sum()
}
fn part2(input: &str) -> i32 {
let mut r = 0;
for l in input.lines() {
println!("{}", l);
let mut c = l.split(",");
let p1 = c.next().unwrap();
let p2 = c.next().unwrap();
let mut p1c = p1.split("-");
let x1 = p1c.next().unwrap().parse::<i32>().unwrap();
let x2 = p1c.next().unwrap().parse::<i32>().unwrap();
let mut p2c = p2.split("-");
let y1 = p2c.next().unwrap().parse::<i32>().unwrap();
let y2 = p2c.next().unwrap().parse::<i32>().unwrap();
println!("{}-{} , {}-{}", x1, x2, y1, y2);
if x1 <= y2 && y1 <= x2 {
r += 1;
println!("overlap");
}
}
r
input
.lines()
.map(|l| {
let n = nums(l);
(n[0] <= n[3] && n[2] <= n[1]) as i32
})
.sum()
}
#[cfg(test)]