Skip to content

Instantly share code, notes, and snippets.

@giesse
Created December 11, 2018 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giesse/877f315af0cbf70fb143fcd50bf146a5 to your computer and use it in GitHub Desktop.
Save giesse/877f315af0cbf70fb143fcd50bf146a5 to your computer and use it in GitHub Desktop.
Red []
; [ counter-clockwise marble-number clockwise ]
counter-clockwise: 1
marble-number: 2
clockwise: 3
make-circle: function [marble [integer!]] [
circle: reduce [none marble none]
circle/:counter-clockwise: circle
circle/:clockwise: circle
circle
]
skip-circle: function [
"Skip to a marble in the circle"
circle [block!]
steps [integer!] "Positive for clockwise, negative for counter-clockwise"
] [
direction: either positive? steps [clockwise] [
steps: negate steps
counter-clockwise
]
loop steps [
circle: circle/:direction
]
circle
]
print-circle: function [circle [block!]] [
start: circle
until [
prin circle/:marble-number
prin " "
circle: circle/:clockwise
same? circle start
]
print ""
]
insert-circle: function [
"Insert marble before the current position in the circle"
circle [block!]
marble [integer!]
] [
new-circle: reduce [none marble none]
new-circle/:clockwise: circle
new-circle/:counter-clockwise: circle/:counter-clockwise
circle/:counter-clockwise: new-circle
new-circle/:counter-clockwise/:clockwise: new-circle
new-circle
]
remove-circle: function [
"Remove marble at the current position"
circle [block!]
] [
circle/:clockwise/:counter-clockwise: circle/:counter-clockwise
circle/:counter-clockwise/:clockwise: circle/:clockwise
circle/:clockwise
]
normal-game-step: function [circle [block!] marble [integer!]] [
insert-circle skip-circle circle 2 marble
]
special-game-step: function [circle [block!] scores [block!] player [integer!] marble [integer!]] [
circle: skip-circle circle -7
prev-score: pick scores player
poke scores player prev-score + circle/:marble-number + marble
remove-circle circle
]
run-game: function [players [integer!] final-marble [integer!]] [
scores: make block! players
insert/dup scores 0 players
circle: make-circle 0
repeat marble final-marble [
player: marble - 1 % players + 1
circle: either marble % 23 = 0 [special-game-step circle scores player marble] [normal-game-step circle marble]
;prin [#"[" player "] "]
;print-circle circle
]
;print ["Scores: " scores]
print last sort scores
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment