Solved day 2

main
s00ner 2022-12-02 15:48:06 -05:00
parent 9bf6b898db
commit 68cfd1d819
3 changed files with 2605 additions and 0 deletions

2500
day02/input Normal file

File diff suppressed because it is too large Load Diff

3
day02/input.test Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

102
day02/solution.rb Normal file
View File

@ -0,0 +1,102 @@
#!/bin/env ruby
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
outcome = 6 if opponent == :rock and me == :paper
outcome = 6 if opponent == :paper and me == :scissors
outcome = 6 if opponent == :scissors and me == :rock
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}"