diff --git a/.gitignore b/.gitignore index 3207605..68fa3b0 100644 --- a/.gitignore +++ b/.gitignore @@ -160,4 +160,5 @@ cython_debug/ #.idea/ **/*Zone.Identifier -**/*input* \ No newline at end of file +**/*input* +cookies.txt \ No newline at end of file diff --git a/day4/example_input.txt b/day4/example_input.txt deleted file mode 100644 index 71f208a..0000000 --- a/day4/example_input.txt +++ /dev/null @@ -1,6 +0,0 @@ -Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 -Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19 -Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1 -Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83 -Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36 -Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11 \ No newline at end of file diff --git a/day5/data.py b/day5/data.py new file mode 100644 index 0000000..8fcbb4d --- /dev/null +++ b/day5/data.py @@ -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 diff --git a/day5/part1.py b/day5/part1.py new file mode 100755 index 0000000..82172b1 --- /dev/null +++ b/day5/part1.py @@ -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) diff --git a/day5/part1_memory_hog.py b/day5/part1_memory_hog.py new file mode 100755 index 0000000..5c91f51 --- /dev/null +++ b/day5/part1_memory_hog.py @@ -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) diff --git a/get_today_puzzle.sh b/get_today_puzzle.sh new file mode 100755 index 0000000..d0a2a20 --- /dev/null +++ b/get_today_puzzle.sh @@ -0,0 +1,5 @@ +#!/bin/bash +DAY=`date "+%-e"` +mkdir day${DAY} +# You have to get your cookie header in text format (Cookie: session=xxxx) +wget --verbose --header "`cat cookies.txt`" "https://adventofcode.com/2023/day/${DAY}/input" -O day${DAY}/input.txt \ No newline at end of file