mirror of
https://github.com/Smaug123/advent-of-code-2021
synced 2025-10-05 03:58:39 +00:00
Fix up a bunch of loops (#9)
This commit is contained in:
@@ -15,16 +15,13 @@ pub mod day_1 {
|
||||
{
|
||||
let mut count = 0;
|
||||
let mut previous = 0;
|
||||
loop {
|
||||
if let Some(i) = numbers.next() {
|
||||
if previous < i {
|
||||
count += 1;
|
||||
}
|
||||
previous = i;
|
||||
} else {
|
||||
return count - 1;
|
||||
for i in numbers {
|
||||
if previous < i {
|
||||
count += 1;
|
||||
}
|
||||
previous = i;
|
||||
}
|
||||
count - 1
|
||||
}
|
||||
|
||||
pub fn part_2_naive(numbers: &[u16]) -> u16 {
|
||||
|
@@ -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) => {
|
||||
boards.push(board);
|
||||
}
|
||||
}
|
||||
while let Some(board) = chomp_board(&mut input) {
|
||||
boards.push(board);
|
||||
}
|
||||
Data { boards, draws }
|
||||
} else {
|
||||
panic!("Unexpectedly no first line");
|
||||
}
|
||||
|
@@ -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) => {
|
||||
if fst > next {
|
||||
fst = next;
|
||||
} else {
|
||||
return Some(fst);
|
||||
}
|
||||
}
|
||||
}
|
||||
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) => {
|
||||
if next < min {
|
||||
min = next;
|
||||
} else if next > max {
|
||||
max = 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 {
|
||||
|
Reference in New Issue
Block a user