Day 5 part1 completed
Also, deleted an example_input.txt, modified .gitignore added a script to get the current day's input
This commit is contained in:
46
day5/data.py
Normal file
46
day5/data.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Dict, List, Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class AlmanacMap:
|
||||
destination: int
|
||||
source: int
|
||||
length: int
|
||||
|
||||
def is_mapped(self, source: int) -> bool:
|
||||
return self.source <= source < self.source + self.length
|
||||
|
||||
def mapped_value(self, source: int) -> Optional[int]:
|
||||
if not self.is_mapped(source):
|
||||
return None
|
||||
return self.destination + (source - self.source)
|
||||
|
||||
|
||||
def extract(input_file: str) -> Tuple[List[int], Dict[str, List[AlmanacMap]]]:
|
||||
seeds = []
|
||||
maps = {
|
||||
"seed-to-soil": [],
|
||||
"soil-to-fertilizer": [],
|
||||
"fertilizer-to-water": [],
|
||||
"water-to-light": [],
|
||||
"light-to-temperature": [],
|
||||
"temperature-to-humidity": [],
|
||||
"humidity-to-location": [],
|
||||
}
|
||||
|
||||
with open("input.txt") as input:
|
||||
current_map = {}
|
||||
while line := input.readline():
|
||||
if match := re.match("seeds: (.*)", line):
|
||||
for seed in match.group(1).split(" "):
|
||||
seeds.append(int(seed))
|
||||
continue
|
||||
if match := re.match("([a-z-]+) map:", line):
|
||||
current_map = maps[match.group(1)]
|
||||
continue
|
||||
if match := re.match("([\d ]+)", line):
|
||||
destination, source, length = match.group(1).split(" ")
|
||||
current_map.append(AlmanacMap(destination=int(destination), source=int(source), length=int(length)))
|
||||
return seeds, maps
|
||||
30
day5/part1.py
Executable file
30
day5/part1.py
Executable file
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3.11
|
||||
# Is killed on WSL2 after vmmem increase to 16GB
|
||||
from data import extract
|
||||
|
||||
seeds, maps = extract("input.txt")
|
||||
lowest_location = None
|
||||
|
||||
for seed in seeds:
|
||||
|
||||
soil_maps = list(filter(lambda soil_map: soil_map.is_mapped(seed), maps["seed-to-soil"]))
|
||||
soil = seed if soil_maps == [] else soil_maps[0].mapped_value(seed)
|
||||
fertilizer_maps = list(filter(lambda fertilizer_map: fertilizer_map.is_mapped(soil), maps["soil-to-fertilizer"]))
|
||||
print(fertilizer_maps)
|
||||
fertilizer = soil if fertilizer_maps == [] else fertilizer_maps[0].mapped_value(soil)
|
||||
water_maps = list(filter(lambda water_map: water_map.is_mapped(fertilizer), maps["fertilizer-to-water"]))
|
||||
water = fertilizer if water_maps == [] else water_maps[0].mapped_value(fertilizer)
|
||||
light_maps = list(filter(lambda light_map: light_map.is_mapped(water), maps["water-to-light"]))
|
||||
light = water if light_maps == [] else light_maps[0].mapped_value(water)
|
||||
temperature_maps = list(filter(lambda temperature_map: temperature_map.is_mapped(light), maps["light-to-temperature"]))
|
||||
temperature = light if temperature_maps == [] else temperature_maps[0].mapped_value(light)
|
||||
humidity_maps = list(filter(lambda humidity_map: humidity_map.is_mapped(temperature), maps["temperature-to-humidity"]))
|
||||
humidity = temperature if humidity_maps == [] else humidity_maps[0].mapped_value(temperature)
|
||||
location_maps = list(filter(lambda location_map: location_map.is_mapped(humidity), maps["humidity-to-location"]))
|
||||
location = humidity if location_maps == [] else location_maps[0].mapped_value(humidity)
|
||||
if lowest_location is not None:
|
||||
lowest_location = min(lowest_location, location)
|
||||
else:
|
||||
lowest_location = location
|
||||
|
||||
print(lowest_location)
|
||||
52
day5/part1_memory_hog.py
Executable file
52
day5/part1_memory_hog.py
Executable file
@@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env python3.11
|
||||
# Is killed on WSL2 after vmmem increase to 16GB
|
||||
import re
|
||||
|
||||
seeds = []
|
||||
|
||||
maps = {
|
||||
"seed-to-soil": {},
|
||||
"soil-to-fertilizer": {},
|
||||
"fertilizer-to-water": {},
|
||||
"water-to-light": {},
|
||||
"light-to-temperature": {},
|
||||
"temperature-to-humidity": {},
|
||||
"humidity-to-location": {},
|
||||
}
|
||||
input_file = "example_input.txt"
|
||||
with open(input_file) as input:
|
||||
current_map = {}
|
||||
while line := input.readline():
|
||||
if match := re.match("seeds: (.*)", line):
|
||||
for seed in match.group(1).split(" "):
|
||||
seeds.append(int(seed))
|
||||
continue
|
||||
if match := re.match("([a-z-]+) map:", line):
|
||||
current_map = maps[match.group(1)]
|
||||
continue
|
||||
if match := re.match("([\d ]+)", line):
|
||||
destination, source, length = match.group(1).split(" ")
|
||||
for idx in range(int(length)):
|
||||
current_map[int(source) + idx] = int(destination) + idx
|
||||
|
||||
lowest_location = None
|
||||
|
||||
for seed in seeds:
|
||||
soil = fertilizer = water = light = temperature = humidity = location = -1
|
||||
|
||||
soil = maps["seed-to-soil"][seed] if seed in maps["seed-to-soil"] else seed
|
||||
fertilizer = maps["soil-to-fertilizer"][soil] if soil in maps["soil-to-fertilizer"] else soil
|
||||
water = maps["fertilizer-to-water"][fertilizer] if fertilizer in maps["fertilizer-to-water"] else fertilizer
|
||||
light = maps["water-to-light"][water] if water in maps["water-to-light"] else water
|
||||
temperature = maps["light-to-temperature"][light] if light in maps["light-to-temperature"] else light
|
||||
humidity = (
|
||||
maps["temperature-to-humidity"][temperature] if temperature in maps["temperature-to-humidity"] else temperature
|
||||
)
|
||||
location = maps["humidity-to-location"][humidity] if humidity in maps["humidity-to-location"] else humidity
|
||||
|
||||
if lowest_location is not None:
|
||||
lowest_location = min(lowest_location, location)
|
||||
else:
|
||||
lowest_location = location
|
||||
|
||||
print(lowest_location)
|
||||
Reference in New Issue
Block a user