Fix up a bunch of loops (#9)

This commit is contained in:
Patrick Stevens
2021-12-10 06:32:34 +00:00
committed by GitHub
parent 2d877e7658
commit 894746e8ae
3 changed files with 22 additions and 45 deletions

View File

@@ -15,16 +15,13 @@ pub mod day_1 {
{
let mut count = 0;
let mut previous = 0;
loop {
if let Some(i) = numbers.next() {
for i in numbers {
if previous < i {
count += 1;
}
previous = i;
} else {
return count - 1;
}
}
count - 1
}
pub fn part_2_naive(numbers: &[u16]) -> u16 {

View File

@@ -47,16 +47,10 @@ pub mod day_4 {
.map(|i| i.unwrap())
.collect::<Vec<u8>>();
let mut boards = Vec::new();
loop {
match chomp_board(&mut input) {
None => {
return Data { boards, draws };
}
Some(board) => {
while let Some(board) = chomp_board(&mut input) {
boards.push(board);
}
}
}
Data { boards, draws }
} else {
panic!("Unexpectedly no first line");
}

View File

@@ -26,21 +26,14 @@ pub mod day_7 {
T: Ord,
{
let mut fst = data.next()?;
loop {
let next = data.next();
match next {
None => {
return Some(fst);
}
Some(next) => {
for next in data {
if fst > next {
fst = next;
} else {
return Some(fst);
};
}
}
}
}
Some(fst)
}
fn min_max<I, T>(mut data: I) -> Option<(T, T)>
@@ -50,21 +43,14 @@ pub mod day_7 {
{
let mut min = data.next()?;
let mut max = min;
loop {
let next = data.next();
match next {
None => {
return Some((min, max));
}
Some(next) => {
for next in data {
if next < min {
min = next;
} else if next > max {
max = next;
};
}
}
}
}
Some((min, max))
}
pub fn part_1(data: &[u16]) -> u32 {