Skip to content

Instantly share code, notes, and snippets.

@DutchGhost
Created December 2, 2020 14:23
Show Gist options
  • Select an option

  • Save DutchGhost/d210d6a13ddc9682affd2db010a946ce to your computer and use it in GitHub Desktop.

Select an option

Save DutchGhost/d210d6a13ddc9682affd2db010a946ce to your computer and use it in GitHub Desktop.
macro_rules! monadic {
(
[$($e:tt)*] | [$head:ident : $tail:ident <- tails $origin:ident,
$($heads:ident : $tails:ident <- tails $origins:ident),*], $condition:expr
) => {{
let mut f = move || {
monadic![@INNER [$($e)*] | [$head : $tail <- tails $origin $(,$heads : $tails <- tails $origins)*], $condition ];
None
};
f()
}};
(
@INNER [$($e:tt)*] | [$head:ident : $tail:ident <- tails $origin:ident,
$($heads:ident : $tails:ident <- tails $origins:ident),*], $condition:expr
) => {
while let Some(($head, mut $tail)) = $origin.split_first() {
$origin = $tail;
monadic![@INNER [$($e)*] | [$($heads : $tails <- tails $origins),*], $condition];
}
};
(
@INNER [$($e:tt)*] | [$head:ident : $tail:ident <- tails $origin:ident], $condition:expr
) => {
while let Some(($head, $tail)) = $origin.split_first() {
$origin = $tail;
if ($condition) {
return Some($($e)*)
}
}
}
}
//[x * y * z | x : xs <- tails inpt, y : ys <- tails xs, z <- ys, x + y + z == 2020]
fn solve1(s: impl Deref<Target = [usize]>) -> Option<usize> {
let mut s: &[_] = &*s;
monadic![[x * y * z] | [x : xs <- tails s, y : ys <- tails xs, z : zs <- tails ys], x + y + z == 2020]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment