Update: added type assertions and [experimental] special do
statement syntax.
Not my finest work, but it’ll have to do for now:
# experimental `do` statement syntax
open("./input/2.txt") do {
::IO # new optional arg type assertion
# setup
oppmoves = (rock='A', paper='B', scissors='C')
mymoves = (rock='X', paper='Y', scissors='Z')
outcome(oppmove, mymove) = begin
(oppmove, mymove) ∈ ((:rock, :paper), (:paper, :scissors), (:scissors, :rock)) && return 1
(oppmove, mymove) ∈ ((:paper, :rock), (:scissors, :paper), (:rock, :scissors)) && return -1
0
end
# process
read(it, String)
split(it, "\n")
map(it) do {
try
oppmove = findfirst(==(it[1]), oppmoves)
mymove = findfirst(==(it[3]), mymoves)
score = findfirst(==(it[3]), values(mymoves))
score += 3 + outcome(oppmove, mymove) * 3
catch; 0 end
} end
sum
} end
(my preference of course, would be for f(arg) do {stuff}
not to require an end
keyword, but I can’t do that without parser changes.)