Swap findMax result tuple order
This commit is contained in:
parent
17f4b83fc0
commit
4a3506a58d
@ -32,9 +32,9 @@ maxBatteries :: Int -> Bank -> Maybe [Battery]
|
|||||||
maxBatteries n0 (Bank bs0) = flip unfoldrM (n0, toList bs0) \case
|
maxBatteries n0 (Bank bs0) = flip unfoldrM (n0, toList bs0) \case
|
||||||
(0, _) -> pure Nothing
|
(0, _) -> pure Nothing
|
||||||
(n, bs) -> do
|
(n, bs) -> do
|
||||||
(b, i) <- findMax <$> nonEmpty (dropEnd (n - 1) bs)
|
(i, b) <- findMax <$> nonEmpty (dropEnd (n - 1) bs)
|
||||||
pure $ Just (b, (n - 1, drop (i + 1) bs))
|
pure $ Just (b, (n - 1, drop (i + 1) bs))
|
||||||
|
|
||||||
-- returns the leftmost element in case of a tie
|
-- returns the leftmost element in case of a tie
|
||||||
findMax :: (Ord a) => NonEmpty a -> (a, Int)
|
findMax :: (Ord a) => NonEmpty a -> (Int, a)
|
||||||
findMax = foldl1' (\m x -> if fst x > fst m then x else m) . flip NE.zip (0 :| [1 ..])
|
findMax = foldl1' (\m x -> if snd x > snd m then x else m) . NE.zip (0 :| [1 ..])
|
||||||
|
|||||||
@ -53,7 +53,7 @@ fn max_batteries(n: usize, v: &[u8]) -> Option<Vec<u8>> {
|
|||||||
let mut slice = v;
|
let mut slice = v;
|
||||||
while remaining > 0 {
|
while remaining > 0 {
|
||||||
match find_max(&slice[..slice.len() - remaining + 1]) {
|
match find_max(&slice[..slice.len() - remaining + 1]) {
|
||||||
Some((b, i)) => {
|
Some((i, b)) => {
|
||||||
result.push(*b);
|
result.push(*b);
|
||||||
remaining -= 1;
|
remaining -= 1;
|
||||||
slice = &slice[i + 1..];
|
slice = &slice[i + 1..];
|
||||||
@ -64,12 +64,8 @@ fn max_batteries(n: usize, v: &[u8]) -> Option<Vec<u8>> {
|
|||||||
Some(result)
|
Some(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_max<A: Ord + Copy>(v: &[A]) -> Option<(&A, usize)> {
|
fn find_max<A: Ord + Copy>(v: &[A]) -> Option<(usize, &A)> {
|
||||||
v.iter()
|
v.iter().enumerate().rev().max_by_key(|x| x.1)
|
||||||
.enumerate()
|
|
||||||
.rev()
|
|
||||||
.max_by_key(|x| x.1)
|
|
||||||
.map(|(n, x)| (x, n))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn digits_to_int(digits: &[u8]) -> usize {
|
fn digits_to_int(digits: &[u8]) -> usize {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user