Added 2025 day6
This commit is contained in:
@@ -0,0 +1,102 @@
|
|||||||
|
#!/bin/python3
|
||||||
|
import sys,time,re,os,copy
|
||||||
|
from pprint import pprint
|
||||||
|
sys.path.insert(0, '../../')
|
||||||
|
from fred import *
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
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
|
||||||
|
grid = []
|
||||||
|
lines = loadFile(input_f)
|
||||||
|
|
||||||
|
pattern = re.compile(r'\d*[^\s]|\*|\+')
|
||||||
|
|
||||||
|
for l in lines:
|
||||||
|
p = pattern.findall(l)
|
||||||
|
try: #i want my numbers to be integers unless its the last row of operations.
|
||||||
|
grid.append(list2int(p))
|
||||||
|
except:
|
||||||
|
grid.append(p)
|
||||||
|
|
||||||
|
numbers = list(map(list, zip(*grid[:-1])))
|
||||||
|
ops = grid[-1]
|
||||||
|
|
||||||
|
|
||||||
|
for x in range(len(ops)):
|
||||||
|
if ops[x] == '*':
|
||||||
|
|
||||||
|
score += math.prod(numbers[x])
|
||||||
|
else:
|
||||||
|
score += sum(numbers[x])
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
grid = []
|
||||||
|
lines = []
|
||||||
|
with open(input_f) as file:
|
||||||
|
for line in file:
|
||||||
|
lines.append(line.rstrip('\n')) #my loadFile helper stripped trailing spaces and newline. We just want to remove newline.
|
||||||
|
|
||||||
|
for l in lines:
|
||||||
|
p = list(l)
|
||||||
|
grid.append(p)
|
||||||
|
|
||||||
|
numbers = list(map(list, zip(*grid[:-1])))
|
||||||
|
ops = [x.strip(' ') for x in grid[-1] if x != ' ']
|
||||||
|
count = 0
|
||||||
|
tmp = 0
|
||||||
|
for x in range(len(numbers)):
|
||||||
|
if all( n == ' ' for n in numbers[x]):
|
||||||
|
score += tmp
|
||||||
|
tmp = 0
|
||||||
|
count += 1
|
||||||
|
continue
|
||||||
|
op = ops[count]
|
||||||
|
|
||||||
|
if op == '+':
|
||||||
|
tmp += int(''.join(numbers[x]))
|
||||||
|
else:
|
||||||
|
if tmp == 0:
|
||||||
|
tmp = 1
|
||||||
|
tmp *= int(''.join(numbers[x]))
|
||||||
|
score += tmp
|
||||||
|
|
||||||
|
return score
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
|
p2 = part2()
|
||||||
|
print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')
|
||||||
|
|
||||||
@@ -14,7 +14,8 @@
|
|||||||
__________________________ ..| \ '''''' // @@| 4 **
|
__________________________ ..| \ '''''' // @@| 4 **
|
||||||
|_ ___ | .--. () () |.' ..__[#]_@@__//_@@@|
|
|_ ___ | .--. () () |.' ..__[#]_@@__//_@@@|
|
||||||
|_\_|^|_]_|==|_T_T_T_T_T_...' 5 **
|
|_\_|^|_]_|==|_T_T_T_T_T_...' 5 **
|
||||||
|
|| . ____________
|
||||||
|
_||__/ \_ 6 **
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user