diff --git a/Book_1.ipynb b/Book_1.ipynb index d3d9e62..a217904 100644 --- a/Book_1.ipynb +++ b/Book_1.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 240, + "execution_count": 2, "id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159", "metadata": {}, "outputs": [], @@ -525,6 +525,142 @@ "source": [ "sum([y for (x,y) in reduce(lambda total, cards: total + [(cards[0], 1)] + [(card, 1+sum([total_card[1] for total_card in total if total_card[0] == cards[0]])) for card in cards[1]], enumerate([j+1+i for j in range(len(re.findall(r'(?:\\b(\\d+)\\s)(?=.*\\s\\1\\b)', line)))] for i, line in enumerate(open('data/puzzle_4.txt'))), [])])" ] + }, + { + "cell_type": "markdown", + "id": "5c0477b9-70f7-4cab-abee-ff843ede5b9c", + "metadata": {}, + "source": [ + "## Puzzel 5" + ] + }, + { + "cell_type": "markdown", + "id": "72c6c580-c46f-40da-90f7-f9bb4a3ba5e7", + "metadata": {}, + "source": [ + "### Deel 1" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "b638e651-ce5f-47af-baa2-b6baa71ccccf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3871578677, 1724133724, 199602917, 2982314302, 595680226, 692431340, 2899305373, 2926286942, 4220409748, 2324727144, 2504054106, 2942258417, 1481150454, 1479468889, 2022824054, 4001340211, 3089202785]\n" + ] + }, + { + "data": { + "text/plain": [ + "199602917" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = open('data/puzzle_5.txt','r').readlines()\n", + "seeds = [int(seed) for seed in re.findall(r'\\d+', data[0])]\n", + "\n", + "seed_maps = []\n", + "map_names = []\n", + "mapping = []\n", + "for line in data[1:]:\n", + " if 'map' in line:\n", + " mapping = []\n", + " map_names.append(line)\n", + " if line == '\\n':\n", + " seed_maps.append(mapping)\n", + " ints = [int(value) for value in re.findall(r'\\b(\\d+)', line)]\n", + " if len(ints) > 0:\n", + " mapping.append(ints)\n", + "seed_maps.append(mapping)\n", + "\n", + "for mapping in seed_maps:\n", + " if len(mapping) < 1:\n", + " continue\n", + " new_seeds = []\n", + " for map_row in mapping:\n", + " for seed in seeds:\n", + " if (map_row[1]+map_row[2]) > seed >= map_row[1]:\n", + " new_seeds.append(seed+map_row[0]-map_row[1])\n", + " seeds = new_seeds\n", + "print(seeds)\n", + "min(seeds)" + ] + }, + { + "cell_type": "markdown", + "id": "58230f2e-b277-408b-8c47-91297109e125", + "metadata": {}, + "source": [ + "## Deel 2" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "3d07272a-6d4e-476d-b321-0639b8433bae", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2254686" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "data = open('data/puzzle_5.txt','r').readlines()\n", + "seeds = [{'start': int(x), 'end': int(x) + int(y)} for (x,y) in re.findall(r'(?:(\\d+)\\s(\\d+))', data[0])]\n", + "seed_maps = []\n", + "map_names = []\n", + "mapping = []\n", + "for line in data[1:]:\n", + " if 'map' in line:\n", + " mapping = []\n", + " map_names.append(line)\n", + " if line == '\\n':\n", + " if len(mapping) > 0:\n", + " seed_maps.append(mapping)\n", + " ints = [int(value) for value in re.findall(r'\\b(\\d+)', line)]\n", + " if len(ints) > 0:\n", + " mapping.append({'start': ints[1], 'end': ints[1]+ints[2], 'change': ints[0] - ints[1]})\n", + "seed_maps.append(mapping)\n", + "for seed_map in seed_maps:\n", + " new_seeds = []\n", + " for seed in seeds:\n", + " bounds = [(seed['start'], 0), (seed['end'], 0)]\n", + " for row in seed_map:\n", + " if (row['start'] >= seed['end']) or (row['end'] <= seed['start']):\n", + " continue\n", + " if row['start'] <= seed['start']:\n", + " bounds.append((seed['start'], row['change']))\n", + " else:\n", + " bounds.append((row['start'], row['change']))\n", + " if row['end'] < seed['end']:\n", + " bounds.append((row['end'], 0))\n", + " bounds.sort(key=lambda x: x[0])\n", + " for i in range(len(bounds)-1):\n", + " lower_bound = bounds[i]\n", + " upper_bound = bounds[i+1]\n", + " if lower_bound[0] != lower_bound[1]:\n", + " new_seeds.append({'start': lower_bound[0]+lower_bound[1], 'end': upper_bound[0]+lower_bound[1]})\n", + " seeds = new_seeds\n", + "min([seed['start'] for seed in seeds])" + ] } ], "metadata": { diff --git a/pretty_read.md b/pretty_read.md index 2b5cb7d..4a6174d 100644 --- a/pretty_read.md +++ b/pretty_read.md @@ -333,3 +333,99 @@ sum([y for (x,y) in reduce(lambda total, cards: total + [(cards[0], 1)] + [(card 11827296 + +## Puzzel 5 + +### Deel 1 + + +```python +data = open('data/puzzle_5.txt','r').readlines() +seeds = [int(seed) for seed in re.findall(r'\d+', data[0])] + +seed_maps = [] +map_names = [] +mapping = [] +for line in data[1:]: + if 'map' in line: + mapping = [] + map_names.append(line) + if line == '\n': + seed_maps.append(mapping) + ints = [int(value) for value in re.findall(r'\b(\d+)', line)] + if len(ints) > 0: + mapping.append(ints) +seed_maps.append(mapping) + +for mapping in seed_maps: + if len(mapping) < 1: + continue + new_seeds = [] + for map_row in mapping: + for seed in seeds: + if (map_row[1]+map_row[2]) > seed >= map_row[1]: + new_seeds.append(seed+map_row[0]-map_row[1]) + seeds = new_seeds +print(seeds) +min(seeds) +``` + + [3871578677, 1724133724, 199602917, 2982314302, 595680226, 692431340, 2899305373, 2926286942, 4220409748, 2324727144, 2504054106, 2942258417, 1481150454, 1479468889, 2022824054, 4001340211, 3089202785] + + + + + + 199602917 + + + +## Deel 2 + + +```python +data = open('data/puzzle_5.txt','r').readlines() +seeds = [{'start': int(x), 'end': int(x) + int(y)} for (x,y) in re.findall(r'(?:(\d+)\s(\d+))', data[0])] +seed_maps = [] +map_names = [] +mapping = [] +for line in data[1:]: + if 'map' in line: + mapping = [] + map_names.append(line) + if line == '\n': + if len(mapping) > 0: + seed_maps.append(mapping) + ints = [int(value) for value in re.findall(r'\b(\d+)', line)] + if len(ints) > 0: + mapping.append({'start': ints[1], 'end': ints[1]+ints[2], 'change': ints[0] - ints[1]}) +seed_maps.append(mapping) +for seed_map in seed_maps: + new_seeds = [] + for seed in seeds: + bounds = [(seed['start'], 0), (seed['end'], 0)] + for row in seed_map: + if (row['start'] >= seed['end']) or (row['end'] <= seed['start']): + continue + if row['start'] <= seed['start']: + bounds.append((seed['start'], row['change'])) + else: + bounds.append((row['start'], row['change'])) + if row['end'] < seed['end']: + bounds.append((row['end'], 0)) + bounds.sort(key=lambda x: x[0]) + for i in range(len(bounds)-1): + lower_bound = bounds[i] + upper_bound = bounds[i+1] + if lower_bound[0] != lower_bound[1]: + new_seeds.append({'start': lower_bound[0]+lower_bound[1], 'end': upper_bound[0]+lower_bound[1]}) + seeds = new_seeds +min([seed['start'] for seed in seeds]) +``` + + + + + 2254686 + +