From ba2a83de0c7a0bed8027fe2fb92fabee8f480b97 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Sun, 4 Dec 2022 11:12:00 +0100 Subject: [PATCH] optimise day 2 ruby, add day 2 python --- day02.py | 12 ++++++++++++ day02.rb | 25 +++++-------------------- 2 files changed, 17 insertions(+), 20 deletions(-) create mode 100644 day02.py diff --git a/day02.py b/day02.py new file mode 100644 index 0000000..08c91b8 --- /dev/null +++ b/day02.py @@ -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) diff --git a/day02.rb b/day02.rb index 1db6e1a..4a00ac9 100644 --- a/day02.rb +++ b/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