Compare commits
2 commits
383dfa9e7a
...
bebfa59762
| Author | SHA1 | Date | |
|---|---|---|---|
| bebfa59762 | |||
| 8bf07308ff |
2 changed files with 66 additions and 0 deletions
35
2025-python/output/day_02.py
Normal file
35
2025-python/output/day_02.py
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
from output import ints
|
||||||
|
|
||||||
|
|
||||||
|
def solve(data):
|
||||||
|
p1 = set()
|
||||||
|
p2 = set()
|
||||||
|
R = re.compile(r"^(\w+)\1+$")
|
||||||
|
for line in data.split(","):
|
||||||
|
a, b = ints(line)
|
||||||
|
for n in range(a, b + 1):
|
||||||
|
s = str(n)
|
||||||
|
ls = len(s)
|
||||||
|
for seq in re.findall(R, s):
|
||||||
|
sqrts = [i for i in range(1, ls + 1) if ls % i == 0]
|
||||||
|
for t in sqrts:
|
||||||
|
if "".join([seq] * t) == s:
|
||||||
|
if t == 2:
|
||||||
|
p1.add(n)
|
||||||
|
p2.add(n)
|
||||||
|
return sum(p1), sum(p2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/02.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
|
|
||||||
|
assert p1 == 38437576669
|
||||||
|
assert p2 == 49046150754
|
||||||
31
2025-python/output/day_03.py
Normal file
31
2025-python/output/day_03.py
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
def solve(data):
|
||||||
|
p1 = 0
|
||||||
|
p2 = 0
|
||||||
|
for line in data.splitlines():
|
||||||
|
p1 += _maxj(line, 2)
|
||||||
|
p2 += _maxj(line, 12)
|
||||||
|
return p1, p2
|
||||||
|
|
||||||
|
|
||||||
|
def _maxj(line, C):
|
||||||
|
toexcl = len(line) - C
|
||||||
|
batt = []
|
||||||
|
for c in line:
|
||||||
|
while toexcl and batt and batt[-1] < c:
|
||||||
|
toexcl -= 1
|
||||||
|
batt.pop()
|
||||||
|
batt.append(c)
|
||||||
|
return sum(10**x * int(y) for x, y in zip(range(C - 1, -1, -1), batt))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
with open("./input/03.txt", "r") as f:
|
||||||
|
inp = f.read().strip()
|
||||||
|
|
||||||
|
p1, p2 = solve(inp)
|
||||||
|
|
||||||
|
print(p1)
|
||||||
|
print(p2)
|
||||||
|
|
||||||
|
assert p1 == 17430
|
||||||
|
assert p2 == 171975854269367
|
||||||
Loading…
Add table
Reference in a new issue