Added 2025 day3

This commit is contained in:
2025-12-03 21:44:08 +01:00
parent 536c2349a8
commit 28c877459d
2 changed files with 91 additions and 1 deletions
+89
View File
@@ -0,0 +1,89 @@
#!/bin/python3
import sys,time,re,os
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint,loadFile
start_time = time.time()
if sys.argv[1] == 'test':
input_f = 'test'
elif sys.argv[1] == 'input':
input_f = 'input'
else:
print('No argv provided')
exit()
#########################################
# #
# Part 1 #
# #
#########################################
def part1():
score = 0
nums = loadFile(input_f)
for n in nums:
max_nums = 0
max_idx = 0
for i in range(0,len(n)-1):
if int(n[i]) > max_nums:
max_nums = int(n[i])
max_idx = i
max2_nums = 0
if max_idx+2 == len(n):
score+= int(str(max_nums)+str(n[max_idx+1]))
else:
for i in range(max_idx+1,len(n)):
if int(n[i]) > max2_nums:
max2_nums = int(n[i])
score += int(str(max_nums)+str(max2_nums))
return score
start_time = time.time()
p1 = part1()
print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms')
#########################################
# #
# Part 2 #
# #
#########################################
def part2():
score = 0
nums = loadFile(input_f)
for n in nums:
n=list2int(list(n))
pos = 0
filled = 0
left = len(n)
can_skip = left - 12
number = ''
while filled < 12:
current = n[pos]
window = n[pos:pos+can_skip+1]
if current >= max(window):
number += str(current)
pos += 1
filled += 1
else:
pos += 1
can_skip -= 1
score += int(number)
return score
start_time = time.time()
p2 = part2()
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')