Created
December 2, 2020 14:23
-
-
Save DutchGhost/d210d6a13ddc9682affd2db010a946ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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