From a4ac698f7a91c98d9fdd9948e31dc59db024da67 Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Mon, 2 Dec 2024 06:53:16 -0500 Subject: [PATCH] day 2. messy, might clean up later --- 2024/day2/main.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 2024/day2/main.py diff --git a/2024/day2/main.py b/2024/day2/main.py new file mode 100644 index 0000000..7e6fc14 --- /dev/null +++ b/2024/day2/main.py @@ -0,0 +1,76 @@ +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() +