Skip to content

Commit 9cab37a

Browse files
author
Robb Kidd
committed
remember Mario's solution
1 parent 5556512 commit 9cab37a

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

2022/ruby/day02-short.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
### Not My Solution ###
2+
#
3+
# much shorter and simpler because there are only
4+
# 9 combinations of possible outcomes for a round
5+
class Day02
6+
def self.go
7+
day = new
8+
puts "Part 1: #{day.part1}"
9+
puts "Part 2: #{day.part2}"
10+
end
11+
12+
def initialize(input=nil)
13+
@input = input || real_input
14+
end
15+
16+
# @example
17+
# day.part1 #=> 15
18+
def part1
19+
@input
20+
.split("\n")
21+
.map { |line|
22+
PART1_MAP.fetch(line)
23+
}
24+
.reduce(&:+)
25+
end
26+
27+
# map lifted wholesale from https://github.com/mfinelli/advent-of-code/blob/af64b2d7082b3616029b8ec680cbcc60e9219403/src/y22d02.rs
28+
PART1_MAP = {
29+
"A X" => 4, # rock v. rock (1) / draw (3)
30+
"A Y" => 8, # rock v. paper (2) / win (6)
31+
"A Z" => 3, # rock v. scissors (3) / lose (0)
32+
"B X" => 1, # paper v. rock (1) / lose (0)
33+
"B Y" => 5, # paper v. paper (2) / draw (3)
34+
"B Z" => 9, # paper v. scissors (3) / win (6)
35+
"C X" => 7, # scissors v. rock (1) / win (6)
36+
"C Y" => 2, # scissors v. paper (2) / lose (0)
37+
"C Z" => 6, # scissors v. scissors (3) / draw (3)
38+
}
39+
40+
# @example
41+
# day.part2 #=> 12
42+
def part2
43+
@input
44+
.split("\n")
45+
.map { |line|
46+
PART2_MAP.fetch(line)
47+
}
48+
.reduce(&:+)
49+
end
50+
51+
# map lifted wholesale from https://github.com/mfinelli/advent-of-code/blob/af64b2d7082b3616029b8ec680cbcc60e9219403/src/y22d02.rs
52+
PART2_MAP = {
53+
"A X" => 3, # rock v. lose (scissors): 0 + 3 = 3
54+
"A Y" => 4, # rock v. draw (rock): 3 + 1 = 4
55+
"A Z" => 8, # rock v. win (paper): 6 + 2 = 8
56+
"B X" => 1, # paper v. lose (rock): 0 + 1 = 1
57+
"B Y" => 5, # paper v. draw (paper): 3 + 2 = 5
58+
"B Z" => 9, # paper v. win (scissors): 6 + 3 = 9
59+
"C X" => 2, # scissors v. lose (paper): 0 + 2 = 2
60+
"C Y" => 6, # scissors v. draw (scissors): 3 + 3 = 6
61+
"C Z" => 7, # scissors v. win (rock): 6 + 1 = 7
62+
}
63+
64+
def real_input
65+
File.read('../inputs/day02-input.txt')
66+
end
67+
68+
EXAMPLE_INPUT = <<~INPUT
69+
A Y
70+
B X
71+
C Z
72+
INPUT
73+
end

0 commit comments

Comments
 (0)