advent_of_code/2022_2.rb
Code:
# Advent of Code 2022, Day 2: Rock Paper Scissors
# https://adventofcode.com/2022/day/2
require_relative './helpers.rb'
def part_one(games)
games.map { |i, j| (j - i + 1) % 3 * 3 + j + 1 }.sum
end
def part_two(games)
games.map { |i, j| (i + j + 2) % 3 + 1 + j * 3 }.sum
end
def solve(dataset)
games = dataset.map { |s| s.split(' ').map { |c| %w(A B C X Y Z).index(c) % 3 } }
return part_one(games), part_two(games)
end
puts solve(get_dataset(year: 2022, day: 2, type: 'example'))
puts solve(get_dataset(year: 2022, day: 2, type: 'input'))
Output:
15
12
12855
13726