123456789101112131415161718192021222324252627282930313233343536373839404142 |
- input = File.read("2.input").split("\n").map {|line| line.split(" ")}
- @shape_score = {"A" => 1, "B" => 2, "C" => 3}
- @result_score = {"X" => 0, "Y" => 3, "Z" => 6}
- @win_map = {"A" => "C", "B" => "A", "C" => "B"}
- @lose_map = @win_map.invert
- def result_to_shape(opponent, result)
- if result == "X" then
- return @win_map[opponent]
- elsif result == "Y" then
- return opponent
- else
- return @lose_map[opponent]
- end
- end
- def shapes_to_result(me, opponent)
- if me == opponent then
- return "Y"
- elsif @win_map[me] == opponent then
- return "Z"
- else
- return "X"
- end
- end
- def task1(opponent, me)
- shape_map = {"X" => "A", "Y" => "B", "Z" => "C"}
- me = shape_map[me]
- return @result_score[shapes_to_result(me, opponent)] + @shape_score[me]
- end
- def task2(opponent, result)
- return @result_score[result] + @shape_score[result_to_shape(opponent, result)]
- end
- puts input.map {|line| task1(line[0], line[1])}.sum()
- puts input.map {|line| task2(line[0], line[1])}.sum()
|