2.rb 990 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. input = File.read("2.input").split("\n").map {|line| line.split(" ")}
  2. @shape_score = {"A" => 1, "B" => 2, "C" => 3}
  3. @result_score = {"X" => 0, "Y" => 3, "Z" => 6}
  4. @win_map = {"A" => "C", "B" => "A", "C" => "B"}
  5. @lose_map = @win_map.invert
  6. def result_to_shape(opponent, result)
  7. if result == "X" then
  8. return @win_map[opponent]
  9. elsif result == "Y" then
  10. return opponent
  11. else
  12. return @lose_map[opponent]
  13. end
  14. end
  15. def shapes_to_result(me, opponent)
  16. if me == opponent then
  17. return "Y"
  18. elsif @win_map[me] == opponent then
  19. return "Z"
  20. else
  21. return "X"
  22. end
  23. end
  24. def task1(opponent, me)
  25. shape_map = {"X" => "A", "Y" => "B", "Z" => "C"}
  26. me = shape_map[me]
  27. return @result_score[shapes_to_result(me, opponent)] + @shape_score[me]
  28. end
  29. def task2(opponent, result)
  30. return @result_score[result] + @shape_score[result_to_shape(opponent, result)]
  31. end
  32. puts input.map {|line| task1(line[0], line[1])}.sum()
  33. puts input.map {|line| task2(line[0], line[1])}.sum()