diff --git a/day_5/answer2 b/day_5/answer2 new file mode 100644 index 0000000..9e87dc7 --- /dev/null +++ b/day_5/answer2 @@ -0,0 +1 @@ +31161857 diff --git a/day_5/hint2 b/day_5/hint2 new file mode 100644 index 0000000..f756727 --- /dev/null +++ b/day_5/hint2 @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 diff --git a/day_5/meme b/day_5/meme new file mode 100644 index 0000000..8a8b924 --- /dev/null +++ b/day_5/meme @@ -0,0 +1,406 @@ +[Range(start=79, end=92), Range(start=55, end=67)] +[Remap(dest_start=50, dest_end=51, src_start=98, src_end=99), Remap(dest_start=52, dest_end=99, src_start=50, src_end=97)] 5 +[Remap(dest_start=0, dest_end=36, src_start=15, src_end=51), Remap(dest_start=37, dest_end=38, src_start=52, src_end=53), Remap(dest_start=39, dest_end=53, src_start=0, src_end=14)] 10 +[Remap(dest_start=49, dest_end=56, src_start=53, src_end=60), Remap(dest_start=0, dest_end=41, src_start=11, src_end=52), Remap(dest_start=42, dest_end=48, src_start=0, src_end=6), Remap(dest_start=57, dest_end=60, src_start=7, src_end=10)] 16 +[Remap(dest_start=88, dest_end=94, src_start=18, src_end=24), Remap(dest_start=18, dest_end=87, src_start=25, src_end=94)] 20 +[Remap(dest_start=45, dest_end=67, src_start=77, src_end=99), Remap(dest_start=81, dest_end=99, src_start=45, src_end=63), Remap(dest_start=68, dest_end=80, src_start=64, src_end=76)] 25 +[Remap(dest_start=0, dest_end=0, src_start=69, src_end=69), Remap(dest_start=1, dest_end=69, src_start=0, src_end=68)] 29 +[Remap(dest_start=60, dest_end=96, src_start=56, src_end=92), Remap(dest_start=56, dest_end=59, src_start=93, src_end=96)] 33 +Seed to Soil +[ ############# ############## ] +[ 11111111111111111111111111111111111111111111111100] +[ 00111111111111111111111111111111111111111111111111] +--- +[ ############## ] +We do remap now +[ 00] +[ 00 ] +New ranges are +[ ############## ] +We do remap now +[ 111111111111111111111111111111111111111111111111 ] +[ 111111111111111111111111111111111111111111111111] +Case 2 +[ ############## ] +New ranges are +[ ] +[ ############## ] + +[ ############# ] +We do remap now +[ 00] +[ 00 ] +New ranges are +[ ############# ] +We do remap now +[ 111111111111111111111111111111111111111111111111 ] +[ 111111111111111111111111111111111111111111111111] +Case 2 +[ ############# ] +New ranges are +[ ] +[ ############# ] + +--- + +Soil to Fertilizer +[ ############# ############## ] +[222222222222222000000000000000000000000000000000000011 ] +[000000000000000000000000000000000000011222222222222222 ] +--- +[ ############## ] +We do remap now +[ 0000000000000000000000000000000000000 ] +[0000000000000000000000000000000000000 ] +New ranges are +[ ############## ] +We do remap now +[ 11 ] +[ 11 ] +New ranges are +[ ############## ] +We do remap now +[222222222222222 ] +[ 222222222222222 ] +New ranges are +[ ############## ] +[ ############## ] + +[ ############# ] +We do remap now +[ 0000000000000000000000000000000000000 ] +[0000000000000000000000000000000000000 ] +New ranges are +[ ############# ] +We do remap now +[ 11 ] +[ 11 ] +New ranges are +[ ############# ] +We do remap now +[222222222222222 ] +[ 222222222222222 ] +New ranges are +[ ############# ] +[ ############# ] + +--- + +Fertilizer to Water +[ ############# ############## ] +[2222222333311111111111111111111111111111111111111111100000000 ] +[1111111111111111111111111111111111111111112222222000000003333 ] +--- +[ ############## ] +We do remap now +[ 00000000 ] +[ 00000000 ] +New ranges are +[ ############## ] +We do remap now +[ 111111111111111111111111111111111111111111 ] +[111111111111111111111111111111111111111111 ] +New ranges are +[ ############## ] +We do remap now +[2222222 ] +[ 2222222 ] +New ranges are +[ ############## ] +We do remap now +[ 3333 ] +[ 3333 ] +New ranges are +[ ############## ] +[ ############## ] + +[ ############# ] +We do remap now +[ 00000000 ] +[ 00000000 ] +Case 2 +[ #### ] +Case 3 +[ ######### ] +New ranges are +[ ######### ] +We do remap now +[ 111111111111111111111111111111111111111111 ] +[111111111111111111111111111111111111111111 ] +New ranges are +[ ######### ] +We do remap now +[2222222 ] +[ 2222222 ] +New ranges are +[ ######### ] +We do remap now +[ 3333 ] +[ 3333 ] +New ranges are +[ ######### ] +[ #### ######### ] + +--- + +Water to Light +[ #### ######### ############## ] +[ 00000001111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 11111111111111111111111111111111111111111111111111111111111111111111110000000 ] +--- +[ ############## ] +We do remap now +[ 0000000 ] +[ 0000000 ] +New ranges are +[ ############## ] +We do remap now +[ 1111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 1111111111111111111111111111111111111111111111111111111111111111111111 ] +Case 2 +[ ############## ] +New ranges are +[ ] +[ ############## ] + +[ ######### ] +We do remap now +[ 0000000 ] +[ 0000000 ] +New ranges are +[ ######### ] +We do remap now +[ 1111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 1111111111111111111111111111111111111111111111111111111111111111111111 ] +Case 2 +[ ######### ] +New ranges are +[ ] +[ ######### ] + +[ #### ] +We do remap now +[ 0000000 ] +[ 0000000 ] +New ranges are +[ #### ] +We do remap now +[ 1111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 1111111111111111111111111111111111111111111111111111111111111111111111 ] +Case 2 +[ #### ] +New ranges are +[ ] +[ #### ] + +--- + +[Range(start=74, end=87), Range(start=54, end=62), Range(start=46, end=49)] [Remap(dest_start=45, dest_end=67, src_start=77, src_end=99), Remap(dest_start=81, dest_end=99, src_start=45, src_end=63), Remap(dest_start=68, dest_end=80, src_start=64, src_end=76)] +Light to Temperatures +[ #### ######### ############## ] +[ 1111111111111111111222222222222200000000000000000000000] +[ 0000000000000000000000022222222222221111111111111111111] +--- +[ ############## ] +We do remap now +[ 00000000000000000000000] +[ 00000000000000000000000 ] +Case 1 +[ ### ] +Case 2 +[ ########### ] +New ranges are +[ ### ] +We do remap now +[ 1111111111111111111 ] +[ 1111111111111111111] +New ranges are +[ ### ] +We do remap now +[ 2222222222222 ] +[ 2222222222222 ] +Case 2 +[ ########### ### ] +New ranges are +[ ] +[ ########### ### ] + +[ ######### ] +We do remap now +[ 00000000000000000000000] +[ 00000000000000000000000 ] +New ranges are +[ ######### ] +We do remap now +[ 1111111111111111111 ] +[ 1111111111111111111] +Case 2 +[ ######### ] +New ranges are +[ ] +We do remap now +[ 2222222222222 ] +[ 2222222222222 ] +New ranges are +[ ] +[ ######### ] + +[ #### ] +We do remap now +[ 00000000000000000000000] +[ 00000000000000000000000 ] +New ranges are +[ #### ] +We do remap now +[ 1111111111111111111 ] +[ 1111111111111111111] +Case 2 +[ #### ] +New ranges are +[ ] +We do remap now +[ 2222222222222 ] +[ 2222222222222 ] +New ranges are +[ ] +[ #### ] + +--- + +Temperatures to Humidity +[ ########### ### #### ######### ] +[1111111111111111111111111111111111111111111111111111111111111111111110 ] +[0111111111111111111111111111111111111111111111111111111111111111111111 ] +--- +[ ########### ] +We do remap now +[ 0 ] +[0 ] +New ranges are +[ ########### ] +We do remap now +[111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 111111111111111111111111111111111111111111111111111111111111111111111 ] +Case 2 +[ ########### ] +New ranges are +[ ] +[ ########### ] + +[ ### ] +We do remap now +[ 0 ] +[0 ] +New ranges are +[ ### ] +We do remap now +[111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 111111111111111111111111111111111111111111111111111111111111111111111 ] +New ranges are +[ ### ] +[ ### ] + +[ ######### ] +We do remap now +[ 0 ] +[0 ] +New ranges are +[ ######### ] +We do remap now +[111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 111111111111111111111111111111111111111111111111111111111111111111111 ] +New ranges are +[ ######### ] +[ ######### ] + +[ #### ] +We do remap now +[ 0 ] +[0 ] +New ranges are +[ #### ] +We do remap now +[111111111111111111111111111111111111111111111111111111111111111111111 ] +[ 111111111111111111111111111111111111111111111111111111111111111111111 ] +New ranges are +[ #### ] +[ #### ] + +--- + +Humidity to Location +[ ########### ### #### ######### ] +[ 00000000000000000000000000000000000001111 ] +[ 11110000000000000000000000000000000000000 ] +--- +[ ########### ] +We do remap now +[ 0000000000000000000000000000000000000 ] +[ 0000000000000000000000000000000000000 ] +Case 1 +[ ########## ] +Case 2 +[ # ] +New ranges are +[ ########## ] +We do remap now +[ 1111 ] +[ 1111 ] +New ranges are +[ ########## ] +[ ########## # ] + +[ ### ] +We do remap now +[ 0000000000000000000000000000000000000 ] +[ 0000000000000000000000000000000000000 ] +Case 2 +[ ### ] +New ranges are +[ ] +We do remap now +[ 1111 ] +[ 1111 ] +New ranges are +[ ] +[ ### ] + +[ ######### ] +We do remap now +[ 0000000000000000000000000000000000000 ] +[ 0000000000000000000000000000000000000 ] +Case 2 +[ ### ] +Case 3 +[ ###### ] +New ranges are +[ ###### ] +We do remap now +[ 1111 ] +[ 1111 ] +Case 2 +[ #### ### ] +Case 3 +[ ## ] +New ranges are +[ ## ] +[ #### ##### ] + +[ #### ] +We do remap now +[ 0000000000000000000000000000000000000 ] +[ 0000000000000000000000000000000000000 ] +Case 2 +[ #### ] +New ranges are +[ ] +We do remap now +[ 1111 ] +[ 1111 ] +New ranges are +[ ] +[ #### ] + +--- + +[Range(start=46, end=55), Range(start=60, end=60), Range(start=82, end=84), Range(start=97, end=98), Range(start=94, end=96), Range(start=56, end=59), Range(start=86, end=89)] diff --git a/day_5/part2.py b/day_5/part2.py new file mode 100644 index 0000000..5dc0716 --- /dev/null +++ b/day_5/part2.py @@ -0,0 +1,230 @@ +from dataclasses import dataclass + +@dataclass +class Range: + start: int # Inclusive + end: int # Inclusive + +@dataclass +class Remap: + dest_start: int = 0 # Inclusive + dest_end: int = 0 # Inclusive + src_start: int = 0 # Inclusive + src_end: int = 0 # Inclusive + + +def draw_ranges(ranges: list[Range]): + return + lol = [' '] * 100 + for range_ in ranges: + for i in range(range_.start, range_.end + 1): + lol[i] = '#' + s_lol = ''.join(lol) + print(f'[{s_lol}]') + +def draw_remap(remap: Remap, chr): + return + src_lol = [' '] * 100 + dst_lol = [' '] * 100 + + src_lol[remap.src_start] = '[' + src_lol[remap.src_end] = ']' + for j in range(remap.src_start, remap.src_end + 1): + src_lol[j] = chr + dst_lol[remap.dest_start] = '[' + dst_lol[remap.dest_end] = ']' + for j in range(remap.dest_start, remap.dest_end + 1): + dst_lol[j] = chr + + s_lol = ''.join(src_lol) + print(f'[{s_lol}]') + s_lol = ''.join(dst_lol) + print(f'[{s_lol}]') + +def draw_remaps(remaps: list[Remap]): + return + src_lol = [' '] * 100 + dst_lol = [' '] * 100 + + for i, remap in enumerate(remaps): + src_lol[remap.src_start] = '[' + src_lol[remap.src_end] = ']' + for j in range(remap.src_start, remap.src_end + 1): + src_lol[j] = str(i) + dst_lol[remap.dest_start] = '[' + dst_lol[remap.dest_end] = ']' + for j in range(remap.dest_start, remap.dest_end + 1): + dst_lol[j] = str(i) + + s_lol = ''.join(src_lol) + print(f'[{s_lol}]') + s_lol = ''.join(dst_lol) + print(f'[{s_lol}]') + + + +def remap_range(map: list[Remap], initial_range: Range): + unchopped: list[Range] = [initial_range] + chopped: list[Range] = [] + + for i, remap in enumerate(map): + print('We do remap now') + draw_remap(remap, str(i)) + + new_ranges: list[Range] = [] + for range_ in unchopped: + # This remap does not apply to us, just move along + if not(range_.start <= remap.src_end and remap.src_start <= range_.end): + new_ranges.append(range_) + continue + + # Case 1: + # ####### + # [ ] + # ### + if range_.start < remap.src_start: + new_ranges.append(Range(start=range_.start, end=remap.src_start - 1)) + print('Case 1') + draw_ranges(new_ranges) + + # Case 2: + # #### + # [ ] + # #### + # Actually do remap and append to chopped list + bump = remap.dest_start - remap.src_start + chopped.append(Range(max(range_.start, remap.src_start) + bump, min(range_.end, remap.src_end) + bump)) + print('Case 2') + draw_ranges(chopped) + + # Case 2: + # ###### + # [ ] + # ## + if range_.end > remap.src_end: + new_ranges.append(Range(start=remap.src_end + 1, end=range_.end)) + print('Case 3') + draw_ranges(new_ranges) + + print('New ranges are') + draw_ranges(new_ranges) + unchopped = new_ranges.copy() + + return unchopped + chopped + + +def read_map(lines, i): + remaps: list[Remap] = [] + while i < len(lines) and lines[i] != '': + s_dest_start, s_src_start, s_length = lines[i].split(' ') + length = int(s_length) + + remap = Remap() + remap.dest_start = int(s_dest_start) + remap.src_start = int(s_src_start) + + remap.dest_end = remap.dest_start + length - 1 + remap.src_end = remap.src_start + length - 1 + + remaps.append(remap) + + i += 1 + + return remaps, i + + +def do_thing(n_from: str, n_to: str, in_ranges: list[Range], map: list[Remap]) -> list[Range]: + print(f'{n_from} to {n_to}') + draw_ranges(in_ranges) + draw_remaps(map) + print('---') + out_ranges = [] + for seed in in_ranges: + draw_ranges([seed]) + remapped = remap_range(map, seed) + out_ranges += remapped + draw_ranges(remapped) + print() + print('---') + + return out_ranges + + +def get_data(): + with open('input.txt', 'r') as f: + lines = f.readlines() + lines = [line.strip('\n') for line in lines] + + seeds: list[Range] = [] + s_seeds = lines[0].split(' ')[1:] + + for i in range(len(s_seeds) // 2): + start = int(s_seeds[i * 2 + 0]) + length = int(s_seeds[i * 2 + 1]) + end = start + length - 1 + + seeds.append(Range(start=start, end=end)) + + print(seeds) + + idx = 3 + seed_to_soil, idx = read_map(lines, idx) + print(seed_to_soil, idx) + + idx += 2 + soil_to_fertilizer, idx = read_map(lines, idx) + print(soil_to_fertilizer, idx) + + idx += 2 + fertilizer_to_water, idx = read_map(lines, idx) + print(fertilizer_to_water, idx) + + idx += 2 + water_to_light, idx = read_map(lines, idx) + print(water_to_light, idx) + + idx += 2 + light_to_temperature, idx = read_map(lines, idx) + print(light_to_temperature, idx) + + idx += 2 + temperature_to_humidity, idx = read_map(lines, idx) + print(temperature_to_humidity, idx) + + idx += 2 + humidity_to_location, idx = read_map(lines, idx) + print(humidity_to_location, idx) + + + soils = do_thing('Seed', 'Soil', seeds, seed_to_soil) + print() + + fetilizers = do_thing('Soil', 'Fertilizer', soils, soil_to_fertilizer) + print() + + waters = do_thing('Fertilizer', 'Water', fetilizers, fertilizer_to_water) + print() + + lights = do_thing('Water', 'Light', waters, water_to_light) + print() + + print(lights, light_to_temperature) + temperatures = do_thing('Light', 'Temperatures', lights, light_to_temperature) + print() + + humidities = do_thing('Temperatures', 'Humidity', temperatures, temperature_to_humidity) + print() + + locations = do_thing('Humidity', 'Location', humidities, humidity_to_location) + print() + + print(locations) + lowest = locations[0].start + for loc in locations: + lowest = min(lowest, loc.start) + + print(lowest) + + +if __name__ == '__main__': + get_data() diff --git a/day_5/project.godot b/day_5/project.godot index e36cc8a..d609afa 100644 --- a/day_5/project.godot +++ b/day_5/project.godot @@ -12,7 +12,7 @@ config_version=5 config/name="Day 5" run/main_scene="res://control.tscn" -config/features=PackedStringArray("4.1", "Forward Plus") +config/features=PackedStringArray("4.2", "Forward Plus") config/icon="res://icon.svg" [network]