aoc
/
2022
1
0
Fork 0

optimise day 2 ruby, add day 2 python

This commit is contained in:
Sijmen 2022-12-04 11:12:00 +01:00
parent 4229618c64
commit ba2a83de0c
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
2 changed files with 17 additions and 20 deletions

12
day02.py Normal file
View File

@ -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)

View File

@ -1,27 +1,12 @@
lines = STDIN.read.split("\n") lines = ARGF.read.split("\n")
part1 = part2 = 0
part1 = 0 lines.each do |line|
part2 = 0
input = lines.map do |line|
theirs = line[0].ord - 'A'.ord theirs = line[0].ord - 'A'.ord
ours = line[2].ord - 'X'.ord ours = line[2].ord - 'X'.ord
part1 += ours + 1 part1 += ours + [4, 7, 1][(ours - theirs) % 3]
part2 += [1, 4, 7][ours] + (theirs + ours - 1) % 3
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
end end
puts part1, part2 puts part1, part2