remove all those unwraps and just index directly into the vector

This commit is contained in:
David 2022-12-04 19:25:13 -05:00
parent c3a6c3d117
commit c875ec5826

View File

@ -32,16 +32,14 @@ fn part_one() {
let mut overlaps = 0; let mut overlaps = 0;
for line in lines { for line in lines {
if let Ok(ranges) = line { if let Ok(ranges) = line {
let v: Vec<Vec<i32>> = ranges let v: Vec<Vec<i8>> = ranges
.split(",") .split(",")
.map(|r| r.split("-").map(|i| i.parse::<i32>().unwrap()).collect()) .map(|r| r.split("-").map(|i| i.parse::<i8>().unwrap()).collect())
.collect(); .collect();
// 00 ... 01 // 00 ... 01
// 10 ... 11 // 10 ... 11
if (v.get(0).unwrap().get(0) <= v.get(1).unwrap().get(0) if (v[0][0] <= v[1][0] && v[0][1] >= v[1][1])
&& v.get(0).unwrap().get(1) >= v.get(1).unwrap().get(1)) || (v[1][0] <= v[0][0] && v[1][1] >= v[0][1])
|| (v.get(1).unwrap().get(0) <= v.get(0).unwrap().get(0)
&& v.get(1).unwrap().get(1) >= v.get(0).unwrap().get(1))
{ {
overlaps += 1; overlaps += 1;
} }
@ -56,21 +54,17 @@ fn part_two() {
let mut overlaps = 0; let mut overlaps = 0;
for line in lines { for line in lines {
if let Ok(ranges) = line { if let Ok(ranges) = line {
let v: Vec<Vec<i32>> = ranges let v: Vec<Vec<i8>> = ranges
.split(",") .split(",")
.map(|r| r.split("-").map(|i| i.parse::<i32>().unwrap()).collect()) .map(|r| r.split("-").map(|i| i.parse::<i8>().unwrap()).collect())
.collect(); .collect();
// 00 ... 01 // 00 ... 01
// 10 ... 11 // 10 ... 11
// uuuuuugh // uuuuuugh
if (v.get(0).unwrap().get(0) <= v.get(1).unwrap().get(0) if (v[0][0] <= v[1][0] && v[0][1] >= v[1][1])
&& v.get(0).unwrap().get(1) >= v.get(1).unwrap().get(1)) || (v[1][0] <= v[0][0] && v[1][1] >= v[0][1])
|| (v.get(1).unwrap().get(0) <= v.get(0).unwrap().get(0) || (v[0][0] <= v[1][1] && v[0][0] >= v[1][0])
&& v.get(1).unwrap().get(1) >= v.get(0).unwrap().get(1)) || (v[0][1] >= v[1][0] && v[0][1] <= v[1][1])
|| (v.get(0).unwrap().get(0) <= v.get(1).unwrap().get(1)
&& v.get(0).unwrap().get(0) >= v.get(1).unwrap().get(0))
|| (v.get(0).unwrap().get(1) >= v.get(1).unwrap().get(0)
&& v.get(0).unwrap().get(1) <= v.get(1).unwrap().get(1))
{ {
overlaps += 1; overlaps += 1;
} }