From b9a86c612c999bd8b252e2e6a86d765cbb1e46be Mon Sep 17 00:00:00 2001 From: Logan G Date: Wed, 6 Dec 2023 00:56:52 -0700 Subject: [PATCH] Added Day 5 Python versions --- day5/d5p1.py | 115 ++++++++++++++++++++++++++++++++++++++++++ day5/d5p2.py | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 255 insertions(+) create mode 100644 day5/d5p1.py create mode 100644 day5/d5p2.py diff --git a/day5/d5p1.py b/day5/d5p1.py new file mode 100644 index 0000000..92bc5b2 --- /dev/null +++ b/day5/d5p1.py @@ -0,0 +1,115 @@ +#!/usr/bin/python3 +from enum import IntEnum +import re +import sys + +class Maps(IntEnum): + seeds = 0 + seedToSoil = 1 + soilToFertilizer = 2 + fertilizerToWater = 3 + waterToLight = 4 + lightToTemp = 5 + temptoHumidity = 6 + humidityToLocation = 7 + +class Column(IntEnum): + DESTINATION = 0 + ORIGIN = 1 + LENGTH = 2 + +def file_load(path): + file = FileAccess.open(path, FileAccess.READ) + content = file.get_as_text() + return content + +def strarr_to_arr(data): + new = [] + for i in range(0, len(data)): + new.append(data[i]) + + return new + +def input_clean(data): + output = [] + for i in range(len(data)): + regex = re.compile(r"^(.*-to-.* map:\n)|^(seeds: )") + output.append(regex.sub("", data[i])) + + return output + +# bad variable names :( +def input_format(data): + new = [] + for i in range(0, len(data)): + temp = strarr_to_arr(data[i].split("\n")) + temp4 = [] + + for j in range(0, len(temp)): + temp2 = strarr_to_arr(temp[j].split(" ")) + temp3 = [] + + for k in range(0, len(temp2)): + if not len(temp2[k]): + continue + temp3.append(int(temp2[k])) + + temp4.append(temp3) + + new.append(temp4) + + return new + +def is_mapped(thing, data): + for v in thing: + if data >= v[Column.ORIGIN] and data <= v[Column.ORIGIN]+v[Column.LENGTH]: + return v[Column.DESTINATION] + (data-v[Column.ORIGIN]) + + return data + +def is_revmapped(thing, data): + for v in thing: + if data >= v[Column.DESTINATION] and data <= v[Column.DESTINATION]+v[Column.LENGTH]: + return v[Column.ORIGIN] + (data-v[Column.DESTINATION]) + + return data + +def map_input(data, seed): + temp = seed + for map in range(Maps.seedToSoil, Maps.humidityToLocation+1): + temp = is_mapped(data[map], temp) + #print("Output: %s %s" % [map, temp]) + + return temp + +def revmap_input(data, location): + temp = location + for map in range(Maps.humidityToLocation, Maps.seedToSoil-1, -1): + temp = is_revmapped(data[map], temp) + #print("Output: %s %s" % [map, temp]) + + return temp + +def __main__(): + print("Hello!") + + temp = "" + with open(sys.argv[1]) as f: + temp = f.read().split("\n\n") + + temp = input_clean(temp) + data = input_format(temp) + + seeds = data[0][0] + print(seeds) + + minval = 2**32 + for seed in seeds: + location = map_input(data, seed) + + if location < minval: + minval = location + + print("Answer: %s" % minval) + +__main__() diff --git a/day5/d5p2.py b/day5/d5p2.py new file mode 100644 index 0000000..84f6e05 --- /dev/null +++ b/day5/d5p2.py @@ -0,0 +1,140 @@ +#!/usr/bin/python3 +from enum import IntEnum +import re +import sys + +class Maps(IntEnum): + seeds = 0 + seedToSoil = 1 + soilToFertilizer = 2 + fertilizerToWater = 3 + waterToLight = 4 + lightToTemp = 5 + temptoHumidity = 6 + humidityToLocation = 7 + +class Column(IntEnum): + DESTINATION = 0 + ORIGIN = 1 + LENGTH = 2 + +def file_load(path): + file = FileAccess.open(path, FileAccess.READ) + content = file.get_as_text() + return content + +def strarr_to_arr(data): + new = [] + for i in range(0, len(data)): + new.append(data[i]) + + return new + +def input_clean(data): + output = [] + for i in range(len(data)): + regex = re.compile(r"^(.*-to-.* map:\n)|^(seeds: )") + output.append(regex.sub("", data[i])) + + return output + +# bad variable names :( +def input_format(data): + new = [] + for i in range(0, len(data)): + temp = strarr_to_arr(data[i].split("\n")) + temp4 = [] + + for j in range(0, len(temp)): + temp2 = strarr_to_arr(temp[j].split(" ")) + temp3 = [] + + for k in range(0, len(temp2)): + if not len(temp2[k]): + continue + temp3.append(int(temp2[k])) + + temp4.append(temp3) + + new.append(temp4) + + return new + +def is_mapped(thing, data): + for v in thing: + if data >= v[Column.ORIGIN] and data <= v[Column.ORIGIN]+v[Column.LENGTH]: + return v[Column.DESTINATION] + (data-v[Column.ORIGIN]) + + return data + +def is_revmapped(thing, data): + for v in thing: + if data >= v[Column.DESTINATION] and data <= v[Column.DESTINATION]+v[Column.LENGTH]: + return v[Column.ORIGIN] + (data-v[Column.DESTINATION]) + + return data + +def map_input(data, seed): + temp = seed + for map in range(Maps.seedToSoil, Maps.humidityToLocation+1): + temp = is_mapped(data[map], temp) + #print("Output: %s %s" % [map, temp]) + + return temp + +def revmap_input(data, location): + temp = location + for map in range(Maps.humidityToLocation, Maps.seedToSoil-1, -1): + temp = is_revmapped(data[map], temp) + #print("Output: %s %s" % [map, temp]) + + return temp + +def __main__(): + print("Hello!") + + temp = "" + with open(sys.argv[1]) as f: + temp = f.read().split("\n\n") + + temp = input_clean(temp) + data = input_format(temp) + #print(input[Maps.seeds]) + #print(input[Maps.seedToSoil]) + + seeds = data[0][0] + print(seeds) + + """ + minval = 2**32 + for i in range(0, len(seeds) // 2): + start = seeds[i*2] + end = seeds[i*2+1] + + print(f"Testing seeds: {start}-{start+end}") + + for j in range(0, end): + if not j % 1000000: + print(f"Test: {start+j}") + + location = map_input(data, start+j) + if location < minval: + minval = location + + print(f"Answer 2: {minval}") + """ + + testlocation = 0 + while True: + if testlocation % 100000 == 0: + print(testlocation) + for i in range(0, len(seeds) // 2): + seed = revmap_input(data, testlocation) + if seed >= seeds[i*2] and seed <= seeds[i*2]+seeds[i*2+1]: + print(f"Answer 2: {testlocation}") + print(f"Value is between seed {seeds[i*2]} and {seeds[i*2]+seeds[i*2+1]}") + return + + testlocation+=1 + +__main__()