01, 02
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
input
|
||||
test
|
||||
|
||||
/_build
|
||||
/cover
|
||||
/deps
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from itertools import groupby
|
||||
|
||||
data = open("input").read().strip().split("\n")
|
||||
data = [(a[0], int(a[1:])) for a in data]
|
||||
|
||||
fs = {"R": lambda x, y: x + y, "L": lambda x, y: x - y}
|
||||
|
||||
|
||||
def part1(data):
|
||||
s = 50
|
||||
c = 0
|
||||
for a, b in data:
|
||||
f = fs[a]
|
||||
while not 0 <= f(s, b) <= 99:
|
||||
b -= 100
|
||||
s = f(s, b)
|
||||
if s == 0:
|
||||
c += 1
|
||||
return c
|
||||
|
||||
|
||||
def part2(data):
|
||||
s = 50
|
||||
c = 0
|
||||
for a, b in groupby(data, key=lambda x: x[0]):
|
||||
x = sum(y[1] for y in b)
|
||||
f = fs[a]
|
||||
while not 0 <= x <= 99:
|
||||
x -= 100
|
||||
c += 1
|
||||
while x > 0:
|
||||
s = f(s, 1)
|
||||
if s == 0:
|
||||
c += 1
|
||||
elif s == -1:
|
||||
s = 99
|
||||
elif s == 100:
|
||||
s = 0
|
||||
c += 1
|
||||
x -= 1
|
||||
return c
|
||||
|
||||
|
||||
print(part1(data))
|
||||
print(part2(data))
|
||||
@@ -0,0 +1,44 @@
|
||||
F = "input"
|
||||
|
||||
data = open(F).read().strip().split(",")
|
||||
data = [(int(y[0]), int(y[1])) for x in data if (y := x.split("-"))]
|
||||
|
||||
data = sorted(data)
|
||||
m = max(data, key=lambda x: x[1])[1]
|
||||
|
||||
|
||||
def part1(data):
|
||||
c = 0
|
||||
for i in range(1, int("9" * (len(str(m)) // 2)) + 1):
|
||||
x = int(str(i) * 2)
|
||||
for a, b in data:
|
||||
if x in range(a, b + 1):
|
||||
c += x
|
||||
return c
|
||||
|
||||
|
||||
def part2(data):
|
||||
nums = []
|
||||
for i in range(2, len(str(m)) + 1):
|
||||
j = 1
|
||||
n = int(str(j) * i)
|
||||
while n <= m:
|
||||
nums.append(n)
|
||||
j += 1
|
||||
n = int(str(j) * i)
|
||||
nums = sorted(nums)
|
||||
i = 0
|
||||
c = set()
|
||||
for n in nums:
|
||||
while n > data[i][1]:
|
||||
i += 1
|
||||
j = i
|
||||
while j < len(data) and n <= data[j][1]:
|
||||
if data[j][0] <= n <= data[j][1]:
|
||||
c.add(n)
|
||||
j += 1
|
||||
return sum(c)
|
||||
|
||||
|
||||
print(part1(data))
|
||||
print(part2(data))
|
||||
Reference in New Issue
Block a user