瀏覽代碼

2020/13

master
Roderic Day 4 年之前
父節點
當前提交
cdb56f033c
共有 2 個檔案被更改,包括 31 行新增0 行删除
  1. +5
    -0
      toolkit.py
  2. +26
    -0
      y2020/p13.py

+ 5
- 0
toolkit.py 查看文件

@@ -1,6 +1,7 @@
import collections
import hashlib
import itertools
import math
import os
import re
import sys
@@ -9,6 +10,10 @@ from pathlib import Path
import requests


def lcm(a, b):
return abs(a * b) // math.gcd(a, b)


def render(grid, brush=None):
if brush is None:
brush = {v: v for v in grid.values()}

+ 26
- 0
y2020/p13.py 查看文件

@@ -0,0 +1,26 @@
import sys
from itertools import count

import toolkit


text = sys.stdin.read()


tid, string = text.split()
tid = int(tid)
found = [
next((tid + i, val) for i in count() if (tid + i) % val == 0)
for val in [int(v) for v in string.split(',') if v != 'x']
]
a, b = min(found)
print((a - tid) * b)


args = [(i, int(val)) for i, val in enumerate(string.split(',')) if val != 'x']
(last, step), *tail = args
for offset, value in tail:
i = next(i for i in count() if (last + i * step + offset) % value == 0)
last += i * step
step = toolkit.lcm(step, value)
print(last)

Loading…
取消
儲存