advent_of_code/2024/day2/main.py

77 lines
2.0 KiB
Python
Raw Permalink Normal View History

2024-12-02 06:53:16 -05:00
import sys
import os
sys.path.insert(1, os.path.abspath('../../'))
from python_tools.aoc_utils import *
levels = []
def part_one(input):
safe_levels=0
for i in input:
levels.append(list(map(int,i.split())))
for i in levels:
increasing = (i[1]-i[0]) > 0
maybe_safe = True
for j in range(1,len(i)):
diff = i[j] - i[j-1]
inc = diff > 0
if ((inc != increasing) or (abs(diff) > 3) or (diff==0)):
maybe_safe = False
break
if maybe_safe:
safe_levels+=1
else:
continue
print("Part_One: ", safe_levels)
def test_level(level, remove_one):
increasing = (level[1]-level[0]) > 0
maybe_safe = True
index = 1
while(index < len(level)):
diff = level[index] - level[index-1]
inc = diff > 0
if ((inc != increasing) or (abs(diff) > 3) or (diff==0)):
if remove_one:
remove_one = False
#if its less then 2 any of the first 3 numbers
#could be the one causing issues.
if index <= 2:
l0 = level.copy()
l2 = level.copy()
del level[1]
del l0[0]
del l2[2]
maybe_safe = test_level(level, False) or test_level(l0, False) or test_level(l2, False)
elif index == len(level)-1:
maybe_safe = True
else:
del level[index]
maybe_safe = test_level(level, False)
else:
maybe_safe = False
break
index += 1
return maybe_safe
def part_two(input):
safe_levels=0
for i in levels:
if test_level(i, True):
safe_levels += 1
print("Part_Two: ", safe_levels)
def main():
contents = file2list("input.txt")
part_one(contents)
part_two(contents)
if __name__ == "__main__":
main()