optimise day 2 ruby, add day 2 python
This commit is contained in:
parent
4229618c64
commit
ba2a83de0c
|
@ -0,0 +1,12 @@
|
|||
import fileinput
|
||||
|
||||
part1, part2 = 0, 0
|
||||
|
||||
for line in fileinput.input():
|
||||
theirs = ord(line[0]) - ord('A')
|
||||
ours = ord(line[2]) - ord('X')
|
||||
|
||||
part1 += ours + (4, 7, 1)[(ours - theirs) % 3]
|
||||
part2 += (1, 4, 7)[ours] + (theirs + ours - 1) % 3
|
||||
|
||||
print(part1, part2)
|
25
day02.rb
25
day02.rb
|
@ -1,27 +1,12 @@
|
|||
lines = STDIN.read.split("\n")
|
||||
lines = ARGF.read.split("\n")
|
||||
part1 = part2 = 0
|
||||
|
||||
part1 = 0
|
||||
part2 = 0
|
||||
|
||||
input = lines.map do |line|
|
||||
lines.each do |line|
|
||||
theirs = line[0].ord - 'A'.ord
|
||||
ours = line[2].ord - 'X'.ord
|
||||
|
||||
part1 += ours + 1
|
||||
|
||||
if ours == theirs then
|
||||
part1 += 3
|
||||
elsif (theirs + 1) % 3 == ours then
|
||||
part1 += 6
|
||||
end
|
||||
|
||||
if ours == 0 then
|
||||
part2 += (theirs - 1) % 3 + 1
|
||||
elsif ours == 1 then
|
||||
part2 += theirs + 3 + 1
|
||||
else
|
||||
part2 += (theirs + 1) % 3 + 6 + 1
|
||||
end
|
||||
part1 += ours + [4, 7, 1][(ours - theirs) % 3]
|
||||
part2 += [1, 4, 7][ours] + (theirs + ours - 1) % 3
|
||||
end
|
||||
|
||||
puts part1, part2
|
||||
|
|
Loading…
Reference in New Issue