AoC2022/day02/solution.rb

105 lines
1.6 KiB
Ruby
Raw Normal View History

2022-12-02 20:48:06 +00:00
#!/bin/env ruby
2022-12-02 21:15:20 +00:00
# frozen_string_literal: true
2022-12-02 20:48:06 +00:00
require 'pry'
def score(opponent, me)
base = case me
when :rock
1
when :paper
2
when :scissors
3
end
return 3 + base if opponent == me
outcome = 0
2022-12-02 21:15:20 +00:00
outcome = 6 if (opponent == :rock) && (me == :paper)
outcome = 6 if (opponent == :paper) && (me == :scissors)
outcome = 6 if (opponent == :scissors) && (me == :rock)
2022-12-02 20:48:06 +00:00
outcome + base
end
input = File.readlines('./input').map(&:strip)
guide = input.map do |pair|
pair.split.map do |letter|
case letter
when 'A', 'X'
:rock
when 'B', 'Y'
:paper
when 'C', 'Z'
:scissors
end
end
end
scores = guide.map do |pair|
score(pair[0], pair[1])
end
puts "Part 1 Solution: #{scores.sum}"
## Part 2
def wins_to?(throw)
case throw
when :rock
:paper
when :paper
:scissors
when :scissors
:rock
end
end
def loses_to?(throw)
case throw
when :rock
:scissors
when :paper
:rock
when :scissors
:paper
end
end
guide2 = input.map do |pair|
pair.split.map do |letter|
case letter
when 'A'
:rock
when 'B'
:paper
when 'C'
:scissors
when 'X'
:lose
when 'Y'
:draw
when 'Z'
:win
end
end
end
guide2.map! do |opponent, outcome|
throw = case outcome
when :draw
opponent
when :win
wins_to?(opponent)
when :lose
loses_to?(opponent)
end
[opponent, throw]
end
scores2 = guide2.map do |pair|
score(pair[0], pair[1])
end
puts "Part 2 Solution: #{scores2.sum}"