From a6b5ccc5f31706efef2fe5ba3add84f1728172b3 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Sun, 4 Dec 2022 11:17:00 +0100 Subject: [PATCH] day 2: quick maths --- day02.py | 10 +++++----- day02.rb | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/day02.py b/day02.py index 08c91b8..ca7927f 100644 --- a/day02.py +++ b/day02.py @@ -2,11 +2,11 @@ import fileinput part1, part2 = 0, 0 -for line in fileinput.input(): - theirs = ord(line[0]) - ord('A') - ours = ord(line[2]) - ord('X') +for line in fileinput.input(mode='rb'): + theirs = line[0] - ord('A') + ours = line[2] - ord('X') - part1 += ours + (4, 7, 1)[(ours - theirs) % 3] - part2 += (1, 4, 7)[ours] + (theirs + ours - 1) % 3 + part1 += ours + (ours - theirs + 1) % 3 * 3 + 1 + part2 += ours * 3 + (theirs + ours - 1) % 3 + 1 print(part1, part2) diff --git a/day02.rb b/day02.rb index 4a00ac9..f9a5ec4 100644 --- a/day02.rb +++ b/day02.rb @@ -5,8 +5,8 @@ lines.each do |line| theirs = line[0].ord - 'A'.ord ours = line[2].ord - 'X'.ord - part1 += ours + [4, 7, 1][(ours - theirs) % 3] - part2 += [1, 4, 7][ours] + (theirs + ours - 1) % 3 + part1 += ours + (ours - theirs + 1) % 3 * 3 + 1 + part2 += ours * 3 + (theirs + ours - 1) % 3 + 1 end puts part1, part2