AdventOfCode/2024/02/solution.py

89 lines
2.2 KiB
Python
Raw Normal View History

2024-12-02 09:54:52 +01:00
#!/bin/python3
import sys,re
from pprint import pprint
sys.path.insert(0, '../../')
from fred import list2int,get_re,nprint,lprint
input_f = 'input'
part = 2
#########################################
# #
# Part 1 #
# #
#########################################
def checkInc(line:list):
safe = False
for i in range(0,len(line)-1):
if line[i] < line[i+1]:
safe = True
else:
return False
return safe
def checkDesc(line:list):
safe = False
for i in range(0,len(line)-1):
if line[i] > line[i+1]:
safe = True
else:
return False
return safe
def checkJump(line:list):
safe = False
for i in range(0,len(line)-1):
if abs(line[i] - line[i+1]) in [1,2,3]:
safe = True
else:
return False
return safe
if part == 1:
reports = []
count = 0
with open(input_f) as file:
for line in file:
reports.append(list2int(line.rstrip().split()))
for r in reports:
if (checkDesc(r) or checkInc(r)) and checkJump(r):
count += 1
print(count)
#########################################
# #
# Part 2 #
# #
#########################################
if part == 2:
reports = []
count = 0
j = 0
with open(input_f) as file:
for line in file:
reports.append(list2int(line.rstrip().split()))
for rdx,r in enumerate(reports):
if (checkDesc(r) or checkInc(r)) and checkJump(r):
count += 1
else:
secondSafe = False
while not secondSafe:
tmp = r[:j] + r[j + 1:]
if (checkDesc(tmp) or checkInc(tmp)) and checkJump(tmp):
count += 1
secondSafe = True
if not secondSafe and j > len(tmp)-1:
break
j += 1
j = 0
secondSafe = False
print(count)