From 8e105b7451cfaf4abb48735a62264a56a47c033f Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Thu, 5 May 2022 09:47:54 +0000 Subject: [PATCH] finish extra/queue-to-do --- README.md | 3 +++ extra/queue-to-do/solution.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 extra/queue-to-do/solution.py diff --git a/README.md b/README.md index ba6252b..ca0aec3 100644 --- a/README.md +++ b/README.md @@ -76,3 +76,6 @@ CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQM ### fuel-injection-perfection * Completed in: 1 hr, 2 secs. + +### queue-to-do +* Completed in: 9 hrs, 13 mins, 42 secs. diff --git a/extra/queue-to-do/solution.py b/extra/queue-to-do/solution.py new file mode 100644 index 0000000..4bac795 --- /dev/null +++ b/extra/queue-to-do/solution.py @@ -0,0 +1,31 @@ +def solution(start, length): + checksum = 0 + for i in range(length): + base = start + length * i + checksum ^= calc_checksum_range(base, base + length - i) + return checksum + +# P = [01]* +# P^P = 0 +# P00^P01^P10^P11 = 0 +def calc_checksum_range(_from, _to): # _from included, _to is NOT included + checksum = 0 + remain = (-_from) % 4 + for i in range(_from, min(_from + remain, _to)): + checksum ^= i + checkpoint = _from + remain + + remain = _to % 4 + for i in range(max(_from, checkpoint, _to - remain), _to): + checksum ^= i + + return checksum + +tests = [ + ([0, 3], 2), + ([17, 4], 14) + ] + +for i, o in tests: + result = solution(*i) + print (i, result == o, result, o)