1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
using Match
using Test
plays = open("input") do f
split.(eachsplit(read(f, String), "\n", keepempty = false))
end
translate(play) = @match play begin
"A" || "X" => :rock
"B" || "Y" => :paper
"C" || "Z" => :scissors
end
weigth(play) = @match play begin
:rock => 1
:paper => 2
:scissors => 3
end
function trumps(play)
@match play begin
:rock => :scissors
:scissors => :paper
:paper => :rock
_ => error("Invalid play")
end
end
function looses(play)
@match play begin
:rock => :paper
:scissors => :rock
:paper => :scissors
_ => error("Invalid play")
end
end
function points(my, other)
if trumps(my) == other
return 6
elseif my == other
return 3
else
return 0
end
end
function fixed_play(oponent, my)
oponent_hand = translate(oponent)
my_hand = translate(my)
points(my_hand, oponent_hand) + weigth(my_hand)
end
function reactive_play(oponent, my)
oponent_hand = translate(oponent)
my_hand = @match my begin
"X" => trumps(oponent_hand) # I loose
"Y" => oponent_hand
"Z" => looses(oponent_hand) # I win
_ => error("Invalid input")
end
points(my_hand, oponent_hand) + weigth(my_hand)
end
@testset "solutions" begin
@test map(((op, my),) -> fixed_play(op, my), plays) |> sum == 12535
@test map(((op, my),) -> reactive_play(op, my), plays) |> sum == 15457
end
|