Last active
December 2, 2022 15:52
-
-
Save fxn/bf89ba74bd5defdaae639a48c8cb502d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/os | |
type | |
Shape = enum Rock, Paper, Scissors | |
GameResult = range[-1..1] | |
func toShape(shapeChr: char): Shape = | |
case shapeChr: | |
of 'A', 'X': Rock | |
of 'B', 'Y': Paper | |
of 'C', 'Z': Scissors | |
else: raise | |
func gameResult(a: Shape, b: Shape): GameResult = | |
if a == b: | |
return 0 | |
case a: | |
of Rock: | |
if b == Scissors: -1 else: 1 | |
of Paper: | |
if b == Rock: -1 else: 1 | |
of Scissors: | |
if b == Paper: -1 else: 1 | |
func shapeScore(shape: Shape): int = | |
1 + ord(shape) | |
func myGameScore(a: Shape, b: Shape): int = | |
[0, 3, 6][1 + gameResult(a, b)] | |
func myScore(a: Shape, b: Shape): int = | |
shapeScore(b) + myGameScore(a, b) | |
var score = 0 | |
for line in paramStr(1).lines: | |
let a = line[0].toShape | |
let b = line[2].toShape | |
inc score, myScore(a, b) | |
echo score |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std/os | |
type | |
Shape = enum Rock, Paper, Scissors | |
GameResult = range[-1..1] | |
func toShape(shapeChr: char): Shape = | |
case shapeChr: | |
of 'A', 'X': Rock | |
of 'B', 'Y': Paper | |
of 'C', 'Z': Scissors | |
else: raise | |
func gameResult(a: Shape, b: Shape): GameResult = | |
if a == b: | |
return 0 | |
case a: | |
of Rock: | |
if b == Scissors: -1 else: 1 | |
of Paper: | |
if b == Rock: -1 else: 1 | |
of Scissors: | |
if b == Paper: -1 else: 1 | |
func shapeScore(shape: Shape): int = | |
1 + ord(shape) | |
func myGameScore(a: Shape, b: Shape): int = | |
[0, 3, 6][1 + gameResult(a, b)] | |
func myScore(a: Shape, b: Shape): int = | |
shapeScore(b) + myGameScore(a, b) | |
func myShapeFor(a: Shape, r: char): Shape = | |
if r == 'Y': | |
return a | |
case a: | |
of Rock: | |
if r == 'X': Scissors else: Paper | |
of Paper: | |
if r == 'X': Rock else: Scissors | |
of Scissors: | |
if r == 'X': Paper else: Rock | |
var score = 0 | |
for line in lines(paramStr(1)): | |
let a = line[0].toShape | |
let b = myShapeFor(a, line[2]) | |
inc score, myScore(a, b) | |
echo score |
@Yardanico I've removed the unnecessary return
s from the gists, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Yardanico Ah! Awesome! Was not aware you could do that in Nim (reading docs like crazy these days :). That is how you do it in Ruby, but I was into a Pythonic-mindset here I guess. Don't know yet what is an expression in Nim just yet too.
So that is way better for my taste, prefer the conciseness of the expression without explicit returns.