16-bit x86 machine code under MS-DOS - 45 bytes
Hexdump:
0E5F576A12595188ECE44088C3E44130D8240374F400C4AAE2EF595E80FC2475DFAC0432CD29B020CD29E2F5C3
Base64 coded binary:
Dl9XahJZUYjs5ECIw+RBMNgkA3T0AMSq4u9ZXoD8JHXfrAQyzSmwIM0p4vXD
Actual source code with some comments:
bits 16
org 0x100
again:
push cs ; Save whatever CS we get.
pop di ; Use CS:DI as our course buffer..
push di ; Save for later use in the print loop
push 18 ; We need 18 holes for our golf course.
pop cx ; ch = 0, cl = 18.
push cx ; Save for later use.
mov ah, ch ; Zero out ah.
generate_course:
in al, 0x40 ; Port 0x40 is the 8253 PIT Counter 0.
mov bl, al ; Save the first "random" value in bl.
in al, 0x41 ; Port 0x41 is the 8253 PIT Counter 1.
xor al, bl ; Add some more pseudo randomness.
and al, 3 ; We only need the two lower bits.
jz generate_course ; If zero, re-generate a value, since we need only 3, 4, 5 holes.
add ah, al ; Sum in ah register.
stosb ; Store in the course buffer.
loop generate_course ; Loop for 18 holes.
pop cx ; cx = 18.
pop si ; si = course buffer.
cmp ah, 36 ; 72 holes?
jne again ; No, re-generate the whole course.
print: ; Yup, we have a nice course.
lodsb ; Load the next hole.
add al, '2' ; Add ASCII '2' to get '3', '4' or '5'
int 0x29 ; Undocumented MS-DOS print function.
mov al, ' ' ; Print a space too for better readability.
int 0x29 ; Print the character.
loop print ; Print the whole course.
ret ; Return to the beginning of the PSP where a INT 0x20 happen to be.
Compile with nasm 18h.asm -o 18h.com
and run under MS-DOS (or Dosbox), or NTVDM from a 32-bit Windows version.
Sample output:
4 5 4 5 4 5 3 4 3 4 3 4 4 5 4 3 5 3
3Please confirm, 44152809 solutions? – baby-rabbit – 2012-09-26T00:38:22.857
1I too am curious on the exact number of solutions, however I think it should be more than 44 million... (I am no mathmetician, however: | 1(5)/1(3) = 306 possibilities (1718) | 2(5)/2(3) = 69360 poss (17171615) | 3(5)/3(3) = 11182080 poss (1616161514*13) | does that look right? – NRGdallas – 2012-09-26T15:51:31.830
11@baby-rabbit: I can confirm 44,152,809 solutions by brute force enumeration. Also, it can be directly calculated this way: since the average is exactly
4
, and the only possibilities are3
,4
, or5
, the possible solution classes are {no 3's or 5's
,one 3 and one 5
,two 3's and two 5's
, ...,nine 3's and nine 5's
}. This can be calculated bynCr(18,0)*nCr(18,0) + nCr(18,1)*nCr(17,1) + nCr(18,2)*nCr(16,2) + ... + nCr(18,9)*nCr(9,9) = 44,152,809
. This means approximately11.4%
of all possible combinations are valid solutions(44,152,809 / 3^18)
. – mellamokb – 2012-09-26T22:02:18.1372
sum(factorial(18)/factorial(x)/factorial(y)/factorial(z) for x in range(25) for y in range(25) for z in range(25) if 3*x+4*y+5*z == 72 and x+y+z == 18)
gives44152809L
– Sanjeev Murty – 2014-01-05T07:05:43.160