mirror of
https://github.com/thib8956/advent-of-code.git
synced 2026-05-25 19:53:26 +00:00
2025 day 2 and 3
This commit is contained in:
@@ -10,7 +10,6 @@ def part1(lines):
|
|||||||
print("Part 1: ", cnt)
|
print("Part 1: ", cnt)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def part2(lines):
|
def part2(lines):
|
||||||
pos = 50
|
pos = 50
|
||||||
cnt = 0
|
cnt = 0
|
||||||
@@ -27,17 +26,17 @@ def part2(lines):
|
|||||||
if amount >= k0:
|
if amount >= k0:
|
||||||
cnt += 1 + (amount - k0) // 100
|
cnt += 1 + (amount - k0) // 100
|
||||||
pos = (pos + direction * amount) % 100
|
pos = (pos + direction * amount) % 100
|
||||||
print(rotation, pos, cnt)
|
# print(rotation, pos, cnt)
|
||||||
print("Part 2: ", cnt)
|
print("Part 2: ", cnt)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
infile = sys.argv[1]
|
infile = sys.argv[1]
|
||||||
|
|
||||||
with open(infile) as f:
|
with open(infile) as f:
|
||||||
lines = f.readlines()
|
lines = f.readlines()
|
||||||
lines = [x.rstrip() for x in lines]
|
lines = [x.rstrip() for x in lines]
|
||||||
part1(lines)
|
part1(lines)
|
||||||
part2(lines)
|
part2(lines)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
def generate_invalid_ids(max_half_len):
|
||||||
|
res = []
|
||||||
|
for n in range(1, max_half_len):
|
||||||
|
for prefix in range(10 ** (n - 1), 10**n):
|
||||||
|
res.append(prefix * (10**n + 1))
|
||||||
|
return sorted(res)
|
||||||
|
|
||||||
|
|
||||||
|
def generate_invalid_ids_part2():
|
||||||
|
invalids = set()
|
||||||
|
for n in range(1, 100000):
|
||||||
|
n_str = str(n)
|
||||||
|
length = len(n_str)
|
||||||
|
if 1 <= length <= 5:
|
||||||
|
max_k = 10 // length
|
||||||
|
for k in range(2, max_k + 1):
|
||||||
|
repeated = int(n_str * k)
|
||||||
|
invalids.add(repeated)
|
||||||
|
return sorted(invalids)
|
||||||
|
|
||||||
|
|
||||||
|
def main(content):
|
||||||
|
t0 = time.perf_counter_ns()
|
||||||
|
|
||||||
|
pairs = content.split(",")
|
||||||
|
pairs = [tuple(map(int, x.split("-"))) for x in pairs]
|
||||||
|
t1 = time.perf_counter_ns()
|
||||||
|
|
||||||
|
parsing_ms = (t1 - t0) / 1_000_000
|
||||||
|
print(f"Parsing took {parsing_ms:.4f} ms")
|
||||||
|
|
||||||
|
max_len = max(len(str(pair[1])) for pair in pairs)
|
||||||
|
invalid_ids = generate_invalid_ids(max_half_len=max_len // 2 + 1)
|
||||||
|
t2 = time.perf_counter_ns()
|
||||||
|
|
||||||
|
gen1_ms = (t2 - t1) / 1_000_000
|
||||||
|
print(f"Generating invalid ids took {gen1_ms:.3f} ms")
|
||||||
|
|
||||||
|
invalid_ids_part2 = generate_invalid_ids_part2()
|
||||||
|
t3 = time.perf_counter_ns()
|
||||||
|
|
||||||
|
gen2_ms = (t3 - t2) / 1_000_000
|
||||||
|
print(f"Generating invalid ids for part 2 took {gen2_ms:.3f} ms")
|
||||||
|
|
||||||
|
total = 0
|
||||||
|
total_part2 = 0
|
||||||
|
for pair in pairs:
|
||||||
|
start, end = pair
|
||||||
|
for n in invalid_ids:
|
||||||
|
if start <= n <= end:
|
||||||
|
total += n
|
||||||
|
if n > end:
|
||||||
|
break
|
||||||
|
for n in invalid_ids_part2:
|
||||||
|
if start <= n <= end:
|
||||||
|
total_part2 += n
|
||||||
|
if n > end:
|
||||||
|
break
|
||||||
|
t4 = time.perf_counter_ns()
|
||||||
|
|
||||||
|
loop_ms = (t4 - t3) / 1_000_000
|
||||||
|
print(f"Looping took {loop_ms:.3f} ms")
|
||||||
|
print("Part 1: ", total)
|
||||||
|
print("Part 2: ", total_part2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import sys
|
||||||
|
|
||||||
|
infile = sys.argv[1]
|
||||||
|
|
||||||
|
with open(infile) as f:
|
||||||
|
content = f.read().rstrip()
|
||||||
|
main(content)
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
import fileinput
|
||||||
|
|
||||||
|
|
||||||
|
def part1(inp):
|
||||||
|
"Simple brute force solution"
|
||||||
|
total = 0
|
||||||
|
for line in inp:
|
||||||
|
max_jolt = 0
|
||||||
|
for i, a in enumerate(line):
|
||||||
|
for b in line[i + 1 :]:
|
||||||
|
max_jolt = max(max_jolt, int(a + b))
|
||||||
|
total += max_jolt
|
||||||
|
print("Part 1:", total)
|
||||||
|
|
||||||
|
|
||||||
|
def find_max_joltage(inp):
|
||||||
|
"""
|
||||||
|
Greedy algorithm: pick 12 digits from the input to form the largest
|
||||||
|
possible number, keeping the original order.
|
||||||
|
"""
|
||||||
|
stack = []
|
||||||
|
for i, current in enumerate(inp):
|
||||||
|
while stack != [] and current > stack[-1]:
|
||||||
|
remaining_len = len(inp) - i
|
||||||
|
if len(stack) + remaining_len <= 12:
|
||||||
|
break
|
||||||
|
stack.pop()
|
||||||
|
stack.append(current)
|
||||||
|
return "".join(stack[:12])
|
||||||
|
|
||||||
|
|
||||||
|
def part2(inp):
|
||||||
|
total = 0
|
||||||
|
for line in inp:
|
||||||
|
res = find_max_joltage(line)
|
||||||
|
total += int(res)
|
||||||
|
print("Part 2:", total)
|
||||||
|
|
||||||
|
|
||||||
|
def main(inp):
|
||||||
|
part1(inp)
|
||||||
|
part2(inp)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
lines = [x.rstrip() for x in fileinput.input()]
|
||||||
|
main(lines)
|
||||||
Reference in New Issue
Block a user