From a10dff108345a7c0de8430d7f1f5402f5c4bbd55 Mon Sep 17 00:00:00 2001 From: Fedor Lyanguzov Date: Tue, 2 Dec 2025 12:44:27 +0300 Subject: [PATCH] 01, 02 --- .gitignore | 3 +++ 01/01.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 02/02.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 01/01.py create mode 100644 02/02.py diff --git a/.gitignore b/.gitignore index b263cd1..58412ce 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +input +test + /_build /cover /deps diff --git a/01/01.py b/01/01.py new file mode 100644 index 0000000..8a2f0d7 --- /dev/null +++ b/01/01.py @@ -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)) diff --git a/02/02.py b/02/02.py new file mode 100644 index 0000000..022f6e6 --- /dev/null +++ b/02/02.py @@ -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))