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