Cleaned up a bit
This commit is contained in:
parent
f1250271ea
commit
5118d766b3
1 changed files with 22 additions and 37 deletions
|
@ -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)]
|
||||
|
|
Loading…
Reference in a new issue