2022/02/script.rb

70 lines
1.3 KiB
Ruby
Raw Normal View History

2022-12-06 11:59:50 +01:00
#!/usr/bin/env ruby
require_relative '../common'
class Day02 < Day
WIN = 6
DRAW = 3
LOSE = 0
class MovePart1
def self.game
{
"X" => new("A", "C", 1),
"Y" => new("B", "A", 2),
"Z" => new("C", "B", 3)
}
end
def self.score(input)
game[input[2]].score(input[0])
end
def initialize(opponent_letter, beat_letter, score)
@opponent_letter = opponent_letter
@beat_letter = beat_letter
@score = score
end
def score(played_letter)
match_result = if @opponent_letter == played_letter
Day02::DRAW
elsif played_letter == @beat_letter
Day02::WIN
else
Day02::LOSE
end
match_result + @score
end
end
class MovePart2
SCORES = {
"A" => 1,
"B" => 2,
"C" => 3
}
def self.score(input)
if input[2] == "X"
Day02::LOSE + SCORES[SCORES.keys[SCORES.keys.index(input[0]) - 1]]
elsif input[2] == "Y"
Day02::DRAW + SCORES[input[0]]
else
Day02::WIN + SCORES[SCORES.keys[(SCORES.keys.index(input[0]) + 1) % SCORES.keys.size]]
end
end
end
def part1
stdin.map { |i| MovePart1.score i }.sum
end
def part2
stdin.map { |i| MovePart2.score i }.sum
end
end
Day02.run