Snooker
March 30, 2018
Here is our solution:
(define colors '(black pink blue brown green yellow red)) (define (points color) (length (member color colors))) (define (scores xs) (cdr (scan + 0 (map points xs)))) (define perfect (append (copies 15 '(red black)) '(yellow green brown blue pink black))) > (scores perfect) (1 8 9 16 17 24 25 32 33 40 41 48 49 56 57 64 65 72 73 80 81 88 89 96 97 104 105 112 113 120 122 125 129 134 140 147)
You can run the program at https://ideone.com/AQtuyB.
Couldn’t resist perl golf
[sourcecode language="perl"]
say”@{[map{$t+=$_}(map{(1,7)}1..15),2..7]}”
[sourcecode]
I interpreted the question to require mapping a sequence of potted
balls (including fouls) to scores, so this code handles such cases.
However, my knowledge of snooker rules is limited to what’s mentioned
in the question, so only those violations are caught.
Here’s a solution in Python 3.
Output:
Here’s a solution in C.
[sourecode lang=”c”]
/* snooker.c */
#include <stdio.h>
#include <stdlib.h>
#define NUM_POTS 36
void print_array(int* array, size_t n) {
printf(“[“);
for (size_t i = 0; i < n; ++i) {
if (i > 0) printf(“, “);
printf(“%d”, array[i]);
}
printf(“]\n”);
}
void accumulate(int* array, size_t n) {
for (size_t i = 1; i < n; ++i) {
array[i] += array[i-1];
}
}
void calc_perfect_game(int* scores) {
for (size_t i = 0; i < 15; ++i) {
scores[i2] = 1;
scores[i2 + 1] = 7;
}
for (size_t i = 0; i < 6; ++i) {
scores[i+30] = i + 2;
}
accumulate(scores, NUM_POTS);
}
int main(void) {
int scores[NUM_POTS];
calc_perfect_game(scores);
print_array(scores, NUM_POTS);
return EXIT_SUCCESS;
}
[/sourcecode]
Output:
I spelled “sourcecode” wrong. Here it is again with the correct spelling (and formatting).
/* snooker.c */
#include <stdio.h>
#include <stdlib.h>
#define NUM_POTS 36
void print_array(int* array, size_t n) {
printf("[");
for (size_t i = 0; i < n; ++i) {
if (i > 0) printf(", ");
printf("%d", array[i]);
}
printf("]\n");
}
void accumulate(int* array, size_t n) {
for (size_t i = 1; i < n; ++i) {
array[i] += array[i-1];
}
}
void calc_perfect_game(int* scores) {
for (size_t i = 0; i < 15; ++i) {
scores[i*2] = 1;
scores[i*2 + 1] = 7;
}
for (size_t i = 0; i < 6; ++i) {
scores[i+30] = i + 2;
}
accumulate(scores, NUM_POTS);
}
int main(void) {
int scores[NUM_POTS];
calc_perfect_game(scores);
print_array(scores, NUM_POTS);
return EXIT_SUCCESS;
}
Output:
Last try…
Output:
Here’s a Haskell version. For fun we generate a random snooker game.