191 Prize Strings.pl 815 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 19 October 2017
  4. # https://github.com/trizen
  5. # Runtime: 0.019s
  6. # https://projecteuler.net/problem=191
  7. use 5.022;
  8. use strict;
  9. use warnings;
  10. use experimental qw(signatures);
  11. sub p_191($days) {
  12. my %cache;
  13. sub ($len, $hasL, $hasA1, $hasA2) {
  14. exists($cache{"@_"})
  15. and return $cache{"@_"};
  16. my $count = ($len == $days) ? 1 : 0;
  17. if ($len < $days) {
  18. if (!$hasA1 || !$hasA2) {
  19. $count += __SUB__->($len + 1, $hasL, $hasA2, 1);
  20. }
  21. $count += __SUB__->($len + 1, $hasL, $hasA2, 0);
  22. if (!$hasL) {
  23. $count += __SUB__->($len + 1, 1, $hasA2, 0);
  24. }
  25. }
  26. return ($cache{"@_"} = $count);
  27. }->(0, 0, 0, 0);
  28. }
  29. say p_191(30);