Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Train Carriage Game Solutions

Name: Anonymous 2012-08-26 2:11

I'm looking to program a function that can check if you can "make 10" from some 4 digit number (0000 to 9999).

The function should use these rules when performing the check:
https://www.facebook.com/pages/Train-Carriage-Game/108348062559174?sk=info

Does /prog/ know if there's a simple way to perform such a check short of checking every combination individually? I obviously don't want to be writing the following:


boolean makesTen (int a, int b, int c, int d) {
    if (a + b + c + d == 10) {
        return true;
    } else if (-a + b + c + d == 10) {
        return true;
    } else if (a - b + c + d == 10) {
        return true;
    }
    ...
    ...
    } else {
        return false;
    }
}

Name: Anonymous 2012-08-28 5:50

some type errors are preventing ^ and / mixing as operators, but this should be enough brute force to cover the possibilities. There's a lot of redundancy that could be optimized away with a better design. I need to implement saving of the expressions so you can actually see its solutions.


collect_1_partitions list = collect_1_partitions' [] list where
  collect_1_partitions' _ [] = []
  collect_1_partitions' behind (x:rest) = (x,(behind ++ rest)):(collect_1_partitions' (behind ++ [x]) rest)

permutations [] = []
permutations [x] = [[x]]
permutations list =
  [ x:rest_perm
  | (x,rest) <- collect_1_partitions list,
    rest_perm <- permutations rest
  ]

collect_splits (x:xs) = collect_splits' [x] xs where
  collect_splits' _ [] = []
  collect_splits' behind (x:xs) = (behind, (x:xs)):(collect_splits' (behind ++ [x]) xs)

associative_reductions [] _ = []
associative_reductions [x] _ = [x]
associative_reductions list ops =
  [ behind_reduction `op` rest_reduction |
    (behind,rest) <- collect_splits list,
    behind_reduction <- associative_reductions behind ops,
    rest_reduction <- associative_reductions rest ops,
    op <- ops ]

all_reductions [] _ = []
all_reductions [x] _ = [x]
all_reductions list ops =
  [ reduction |
    perm <- permutations list,
    reduction <- associative_reductions perm ops ]

can_make_n goal numz ops =
  filter (== goal) (all_reductions numz ops)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List