{ "cells": [ { "cell_type": "markdown", "id": "d0b07b88-208c-4408-b9c4-ac9d42b5bad0", "metadata": {}, "source": [ "# Vet mooi notebook van Bas" ] }, { "cell_type": "markdown", "id": "5016996d-7e00-44a1-9553-8dd95b864662", "metadata": {}, "source": [ "Laten we beginnen met de imports" ] }, { "cell_type": "code", "execution_count": 1, "id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159", "metadata": {}, "outputs": [], "source": [ "import re \n", "import numpy as np\n", "from functools import reduce\n", "from math import factorial" ] }, { "cell_type": "markdown", "id": "8f3db4f3-92c2-4e2c-91a2-fe59b1109673", "metadata": {}, "source": [ "## Puzzel 1" ] }, { "cell_type": "markdown", "id": "e76a322a-85e3-40b4-aa04-6f4dda2b7f40", "metadata": {}, "source": [ "### Deel 1" ] }, { "cell_type": "code", "execution_count": 2, "id": "98412978-4434-4461-8255-fde982948f26", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "55834" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = open('data/puzzle_1.txt', 'r')\n", "total = 0\n", "for line in f:\n", " digit_1 = re.search('(\\d)', line).group()\n", " digit_2 = re.search('(?:\\d)(?!.*\\d)', line).group()\n", " total += int(digit_1+digit_2)\n", "total" ] }, { "cell_type": "markdown", "id": "00cbc4f3-d337-4702-bf25-d04717a1787b", "metadata": {}, "source": [ "### Deel 2" ] }, { "cell_type": "code", "execution_count": 3, "id": "66632939-dc5e-44ba-b740-868c20cabe8c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "53221" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']\n", "digits = '|'+'|'.join(l)\n", "f = open('data/puzzle_1.txt', 'r')\n", "total = 0\n", "for line in f:\n", " string_digits = re.findall('(?=(\\d'+digits+'))', line)\n", " string_digits = [digit if (digit not in l) else str(l.index(digit)+1) for digit in string_digits]\n", " total += int(string_digits[0] + string_digits[-1])\n", "total" ] }, { "cell_type": "markdown", "id": "455fa9e3-535d-4708-b0fe-1179ad095f4c", "metadata": {}, "source": [ "## Puzzel 2" ] }, { "cell_type": "code", "execution_count": 13, "id": "9c3695c6-20de-4e20-b13a-45c10eb26d0c", "metadata": {}, "outputs": [], "source": [ "data = open('data/puzzle_2.txt', 'r').readlines()" ] }, { "cell_type": "markdown", "id": "a1afaf93-e716-4347-83cb-8a5d3efd8601", "metadata": {}, "source": [ "### Deel 1" ] }, { "cell_type": "code", "execution_count": 14, "id": "eb4c93ee-5b08-47c7-b020-16b2c545b7c1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3035" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "color_dict = {\n", " 'red': 12,\n", " 'green': 13,\n", " 'blue': 14\n", "}\n", "total = 0\n", "for line in data:\n", " possible = True\n", " for amount, color in re.findall('(\\d+)\\W*(red|green|blue)', line):\n", " if (color_dict[color] < int(amount)):\n", " possible = False\n", " if possible:\n", " total += int(re.findall('(\\d+)', line)[0])\n", "total" ] }, { "cell_type": "markdown", "id": "810e3c1a-af84-45e1-9508-a00c40523f7b", "metadata": {}, "source": [ "### Deel 2" ] }, { "cell_type": "code", "execution_count": 15, "id": "21a60b17-619f-426f-8617-5ff4352cca0c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "66027" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total = 0\n", "for line in data:\n", " color_dict = {'red': [], 'green': [], 'blue': []}\n", " \n", " for pair in re.findall('(\\d+)\\W*(red|green|blue)', line):\n", " color_dict[pair[1]].append(int(pair[0]))\n", " total += max(color_dict['red']) * max(color_dict['green']) * max(color_dict['blue'])\n", "total" ] }, { "cell_type": "markdown", "id": "95831680-6f23-404b-97e6-0411c59f6bdf", "metadata": {}, "source": [ "## Puzzel 3" ] }, { "cell_type": "code", "execution_count": 104, "id": "3f70dc72-b4e4-4f65-87ac-d1222a01c8aa", "metadata": {}, "outputs": [], "source": [ "data = open('data/puzzle_3.txt', 'r').readlines()" ] }, { "cell_type": "code", "execution_count": 113, "id": "91a632d1-6262-4c28-bb17-45352c60c16e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "517021" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_list = [re.sub('\\n', '', line) for line in data]\n", "data_list = [re.sub('[^\\d\\.]', 'X', line) for line in data_list]\n", "total = 0\n", "for line_number, line in enumerate(data_list):\n", " matches = re.findall('\\d+', line)\n", " for number in matches:\n", " match = re.search(number, line)\n", " surrounding_string = \"\"\n", " start, end = match.span()\n", " if start > 0:\n", " start -= 1\n", " if end <= len(line):\n", " end += 1\n", " if line_number != 0:\n", " surrounding_string += data_list[line_number-1][start:end]\n", " surrounding_string += line[start:end]\n", " if line_number != len(data_list)-1:\n", " surrounding_string += data_list[line_number+1][start:end]\n", " if 'X' in surrounding_string:\n", " total += int(match.group())\n", " line = re.sub(match.group(), len(match.group())*'.', line, 1)\n", " match = re.search('\\d+', line) \n", "total" ] }, { "cell_type": "code", "execution_count": 138, "id": "4b239c25-3a43-459e-9669-aa9834571733", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "81296995" ] }, "execution_count": 138, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data_list = [re.sub('\\n', '', line) for line in data]\n", "asterisks = []\n", "numbers = []\n", "for line_number, line in enumerate(data_list):\n", " match = re.search('(\\*)|(\\d+)', line)\n", " while match is not None:\n", " x, y = match.span()\n", " if match.group() == '*':\n", " asterisks.append({'x_pos': x, 'line_number': line_number})\n", " else:\n", " numbers.append({'x_start':x, 'x_end': y, 'line_number': line_number, 'number': int(match.group())})\n", " line = re.sub('(\\*)|(\\d+)', len(match.group())*'.', line, 1)\n", " match = re.search('(\\*)|(\\d+)', line)\n", " \n", "for asterisk in asterisks:\n", " asterisk_numbers = []\n", " for number in numbers:\n", " if number['line_number'] == asterisk['line_number']:\n", " if number['x_end'] == asterisk['x_pos']:\n", " asterisk_numbers.append(number['number'])\n", " continue\n", " if number['x_start']-1 == asterisk['x_pos']:\n", " asterisk_numbers.append(number['number'])\n", " continue\n", " \n", " if (number['line_number']+1 == asterisk['line_number']) or (number['line_number']-1 == asterisk['line_number']):\n", " if asterisk['x_pos'] in [num for num in range(number['x_start']-1, number['x_end']+1)]:\n", " asterisk_numbers.append(number['number'])\n", "\n", " asterisk['numbers'] = asterisk_numbers\n", "total = 0\n", "for asterisk in asterisks:\n", " if len(asterisk['numbers']) == 2:\n", " total+= asterisk['numbers'][0] * asterisk['numbers'][1]\n", "total" ] }, { "cell_type": "markdown", "id": "db726345-15cd-4692-9d98-a8d3a28adfeb", "metadata": {}, "source": [ "## Puzzel 4" ] }, { "cell_type": "markdown", "id": "42c8bc93-34b9-4722-850a-bc4c15212fd8", "metadata": {}, "source": [ "### Deel 1" ] }, { "cell_type": "code", "execution_count": 2, "id": "2cbabb08-e696-4466-8963-844ec7d8fc15", "metadata": {}, "outputs": [], "source": [ "data = open('data/puzzle_4.txt', 'r').readlines()" ] }, { "cell_type": "code", "execution_count": 19, "id": "813183a3-31df-4cb2-a4f4-cbb0db517cb2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "21568\n" ] } ], "source": [ "# initiating output variable\n", "total = 0\n", "for line in data:\n", " # Cleaning identifier as it is not necessary\n", " line_number_part = line.split(':')[1]\n", "\n", " # Removing the \\n \n", " line_number_part = re.sub('\\n', '',line_number_part)\n", "\n", " # Creating a set with winning numbers and with owned numbers\n", " winning_numbers, owned_numbers = line_number_part.split('|')\n", "\n", " # Getting the seperate numbers\n", " winning_numbers = re.findall('(\\d+)', winning_numbers)\n", " owned_numbers = re.findall('(\\d+)', owned_numbers)\n", "\n", " # Converting to a set to prepare for intersect\n", " winning_set = set(winning_numbers)\n", " owned_set = set(owned_numbers)\n", "\n", " # Only numbers that are both owned and winning will be left\n", " winning_numbers_owned = winning_set.intersection(owned_set)\n", "\n", " # Get the number of matches\n", " matched_number_count = len(winning_numbers_owned)\n", "\n", " # If there are no matches, we add nothing to total, so we check for zero matches\n", " if matched_number_count > 0:\n", " # We then add to the total 2 to the power of the total number of matches minus one, as we start at 1 instead of 2 and 2^0 is 1 \n", " total += 2**(matched_number_count-1)\n", "print(total)" ] }, { "cell_type": "markdown", "id": "9d818873-9540-4de8-881a-620c02ddefca", "metadata": {}, "source": [ "### Deel 2" ] }, { "cell_type": "code", "execution_count": 23, "id": "64ac3677-aa84-4059-981a-857632b827c8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11827296\n" ] } ], "source": [ "# initiating output variable\n", "total = 0\n", "winning_numbers_on_card = []\n", "card_numbers = []\n", "copies_of_card = []\n", "for line in data:\n", " # Cleaning identifier\n", " card_identifier_part, line_number_part = line.split(':')\n", " identifier = int(re.search('\\d+', card_identifier_part).group())\n", "\n", " # adding to the list\n", " card_numbers.append(identifier)\n", "\n", " # Also adding a 1 to the owned cards list\n", " copies_of_card.append(1)\n", "\n", " # Removing the \\n \n", " line_number_part = re.sub('\\n', '',line_number_part)\n", "\n", " # Creating a set with winning numbers and with owned numbers\n", " winning_numbers, owned_numbers = line_number_part.split('|')\n", "\n", " # Getting the seperate numbers\n", " winning_numbers = re.findall('(\\d+)', winning_numbers)\n", " owned_numbers = re.findall('(\\d+)', owned_numbers)\n", "\n", " # Converting to a set to prepare for intersect\n", " winning_set = set(winning_numbers)\n", " owned_set = set(owned_numbers)\n", "\n", " # Only numbers that are both owned and winning will be left\n", " winning_numbers_owned = winning_set.intersection(owned_set)\n", "\n", " # Get the number of matches\n", " matched_number_count = len(winning_numbers_owned)\n", "\n", " # Add to card info list\n", " winning_numbers_on_card.append(matched_number_count)\n", "\n", "\n", "# Just simulating the rounds, if a card has winining numbers, loop over a range and add that loop variable to the card number\n", "# if the card number exists, we simply add the number of copies of the current card to that card\n", "# etc. \n", "for i, card_id in enumerate(card_numbers):\n", " winning_numbers = winning_numbers_on_card[i]\n", " for number in range(1, winning_numbers+1):\n", " if (card_id+number) in card_numbers:\n", " id_to_add_to = card_numbers.index(card_id+number)\n", " copies_of_card[id_to_add_to] += copies_of_card[i]\n", "\n", "# just sum the total cards at the end\n", "print(sum(copies_of_card))\n", " \n", " \n", " " ] }, { "cell_type": "markdown", "id": "bdaf4568-5996-493b-9d9b-398b5e16f522", "metadata": {}, "source": [ "# AANSCHOUW!!!!!\n", "Het wonder, het fantastischste ding ooit, oneliners" ] }, { "cell_type": "code", "execution_count": 302, "id": "091f006e-838e-418b-8282-610cc6fadccc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "21568" ] }, "execution_count": 302, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reduce(lambda total, multipliers: (total + 2**(multipliers-1) if (multipliers > 0) else total) ,map(lambda numbers: len(numbers)-len(set(numbers)), [re.findall('(?:(\\d+)\\s|\\n)', line) for line in open('data/puzzle_4.txt')]), 0)" ] }, { "cell_type": "code", "execution_count": 400, "id": "cdbdddfb-c0e4-4f9b-8b6c-4dd561c6236a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11827296" ] }, "execution_count": 400, "metadata": {}, "output_type": "execute_result" } ], "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])" ] }, { "cell_type": "markdown", "id": "30d277d2-13e8-45d6-87da-ef02fa3d9779", "metadata": {}, "source": [ "## Puzzel 6" ] }, { "cell_type": "markdown", "id": "68cefad3-f476-4a7e-8ea2-93affd97daf6", "metadata": {}, "source": [ "## Deel 1" ] }, { "cell_type": "code", "execution_count": 7, "id": "75e6d50f-7c27-4aed-b948-f819d9347ab1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[358, 1054, 1807, 1080] [46, 68, 98, 66]\n" ] } ], "source": [ "data = open('data/puzzle_6.txt', 'r').readlines()\n", "times = [int(time) for time in re.findall(r'(\\d+)', data[0])]\n", "distances = [int(distance) for distance in re.findall(r'(\\d+)', data[1])]" ] }, { "cell_type": "code", "execution_count": 9, "id": "7213a72f-3421-43b2-afff-3b959f1d8380", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "138915" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "total_wins = []\n", "for time, distance in zip(times, distances):\n", " wins = 0\n", " for time_pressed in range(0, time+1):\n", " distance_travelled = time_pressed * (time-time_pressed)\n", " if distance_travelled > distance:\n", " wins += 1\n", " total_wins.append(wins)\n", "score = 1\n", "for win in total_wins:\n", " score = score*win\n", "score\n", " " ] }, { "cell_type": "markdown", "id": "efa4bdfd-636d-4d55-a4a1-9a4a4d300ab2", "metadata": {}, "source": [ "## Deel 2" ] }, { "cell_type": "code", "execution_count": 25, "id": "bf365352-52df-4495-a685-27595ff908fe", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9674509.498754364 37015356.50124563\n" ] }, { "data": { "text/plain": [ "27340847" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "time = [time for time in re.findall(r'(\\d+)', data[0])]\n", "time = int(''.join(time))\n", "distance = [distance for distance in re.findall(r'(\\d+)', data[1])]\n", "distance = int(''.join(distance))\n", "\n", "\n", "discriminant = (time**2) - (4 * distance)\n", "x0 = ((-time) + np.sqrt(discriminant))/-2\n", "x1 = ((-time) - np.sqrt(discriminant))/-2\n", "print(x0, x1)\n", "round(x1) - round(x0)-1" ] }, { "cell_type": "code", "execution_count": 34, "id": "11bcd698-4d34-4a9f-8ae8-be65fe7f8707", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "27340847" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[round(number[0] - number[1]) for number in [[(-1 * numbers[1] + np.sqrt(numbers[0]))/2, ((-1 * numbers[1]) - np.sqrt(numbers[0]))/2] for numbers in [[((numbers[0]**2) - (4 * numbers[1]))]+numbers for numbers in [[int(''.join(re.findall('(\\d+)', line))) for line in open('data/puzzle_6.txt')]]]]][0]" ] }, { "cell_type": "markdown", "id": "4ff4bff7-b7fa-4c44-89f4-cff78d0e7dd3", "metadata": {}, "source": [ "## Puzzel 7" ] }, { "cell_type": "markdown", "id": "38709034-8443-4db3-8c4f-0952dd498fec", "metadata": {}, "source": [ "## Deel 1" ] }, { "cell_type": "code", "execution_count": 49, "id": "8369bc66-021e-44e3-a44d-a2403e4ee11f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "250120186" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = re.findall(r'(\\w+)\\s(\\d+)',open('data/puzzle_7.txt', 'r').read(), flags=re.MULTILINE)\n", "values = \"AKQJT98765432\"\n", "hands = [{'cards': x, 'bid': int(y)} for x,y in data]\n", "\n", "fivek = []\n", "fourk = []\n", "fh = []\n", "threek = []\n", "twop = []\n", "onep = []\n", "hc = []\n", "for hand in hands:\n", " hand['values'] = np.array([hand['cards'].count(letter) for letter in values])\n", " max_card = np.argmax(hand['values'])\n", "\n", " if max(hand['values']) == 5:\n", " fivek.append(hand)\n", " elif max(hand['values']) == 4:\n", " fourk.append(hand)\n", " elif max(hand['values']) == 3:\n", " if np.any(hand['values'] == 2):\n", " fh.append(hand)\n", " else:\n", " threek.append(hand)\n", " elif max(hand['values']) == 2:\n", " if sum(hand['values'] > 1) == 2:\n", " twop.append(hand)\n", " else:\n", " onep.append(hand)\n", " else:\n", " hc.append(hand)\n", "combos = [fivek, fourk, fh, threek, twop, onep, hc]\n", "for combo in combos:\n", " combo.sort(key=lambda hand: ''.join([chr(values.index(letter)+96) for letter in hand['cards']]))\n", "combos = [val for sublist in combos for val in sublist] \n", "combos = combos[::-1]\n", "sum([(i+1)*hand['bid'] for i, hand in enumerate(combos)])" ] }, { "cell_type": "markdown", "id": "b36323f9-e331-4cb3-b683-0c34e0012466", "metadata": {}, "source": [ "### Deel 2" ] }, { "cell_type": "code", "execution_count": 25, "id": "c12b66ca-a0e7-43e9-a55a-919cd027ab9b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "250665248" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = re.findall(r'(\\w+)\\s(\\d+)',open('data/puzzle_7.txt', 'r').read(), flags=re.MULTILINE)\n", "values = \"AKQT98765432J\"\n", "hands = [{'cards': x, 'bid': int(y)} for x,y in data]\n", "\n", "fivek = []\n", "fourk = []\n", "fh = []\n", "threek = []\n", "twop = []\n", "onep = []\n", "hc = []\n", "for hand in hands:\n", " hand['values'] = np.array([hand['cards'].count(letter) for letter in values if letter != 'J'])\n", " max_card = np.argmax(hand['values'])\n", " hand['values'][max_card] += hand['cards'].count('J')\n", "\n", " if max(hand['values']) == 5:\n", " fivek.append(hand)\n", " elif max(hand['values']) == 4:\n", " fourk.append(hand)\n", " elif max(hand['values']) == 3:\n", " if np.any(hand['values'] == 2):\n", " fh.append(hand)\n", " else:\n", " threek.append(hand)\n", " elif max(hand['values']) == 2:\n", " if sum(hand['values'] > 1) == 2:\n", " twop.append(hand)\n", " else:\n", " onep.append(hand)\n", " else:\n", " hc.append(hand)\n", "combos = [fivek, fourk, fh, threek, twop, onep, hc]\n", "for combo in combos:\n", " combo.sort(key=lambda hand: ''.join([chr(values.index(letter)+96) for letter in hand['cards']]))\n", "combos = [val for sublist in combos for val in sublist] \n", "combos = combos[::-1]\n", "sum([(i+1)*hand['bid'] for i, hand in enumerate(combos)])" ] }, { "cell_type": "markdown", "id": "c3c2728a-0b18-4fef-ba87-f8b8144050bc", "metadata": {}, "source": [ "## Puzzel 8" ] }, { "cell_type": "markdown", "id": "d4513f0d-2fd2-4c84-9de0-ce92978742b6", "metadata": {}, "source": [ "### Deel 1" ] }, { "cell_type": "code", "execution_count": 3, "id": "52c6a5ba-3a6a-45fe-a60c-873e10a81745", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20777\n" ] } ], "source": [ "data = open('data/puzzle_8.txt', 'r').read()\n", "directions = re.search(r'(\\w+)', data).group()\n", "nodes = {x: {'L': y, 'R': z} for x,y,z in re.findall(r'(\\w{3}).*\\((\\w{3}).*(\\w{3})', data, flags=re.MULTILINE)}\n", "steps = 0\n", "current_node = 'AAA'\n", "\n", "while current_node != 'ZZZ':\n", " for direction in directions:\n", " current_node = nodes[current_node][direction]\n", " steps += 1\n", " if current_node == 'ZZZ':\n", " break\n", "print(steps)" ] }, { "cell_type": "markdown", "id": "5c0033da-34cb-4eff-b99c-0e45986577ce", "metadata": {}, "source": [ "### Deel 2" ] }, { "cell_type": "code", "execution_count": 47, "id": "e2e32f65-6df2-4cc7-b0ee-96df14f6b083", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "13289612809129" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "current_nodes = re.findall('(\\w{2}A)\\s', data, flags=re.MULTILINE)\n", "all_matches = []\n", "for node in current_nodes:\n", " matches = []\n", " steps = 0\n", " while len(matches) < 10:\n", " for letter in directions:\n", " node = nodes[node][letter]\n", " steps += 1\n", " if node[2] == 'Z':\n", " matches.append(steps)\n", " all_matches.append(matches)\n", "lowest = [matches[0] for matches in all_matches]\n", "\n", "val = np.gcd.reduce(lowest)\n", "total = 1\n", "for x in [x/val for x in lowest]:\n", " total *= x\n", "int(total * val)\n", " \n", " \n", " \n", " " ] }, { "cell_type": "markdown", "id": "72e0ecc7-3104-45ea-8660-3354d09fbeee", "metadata": {}, "source": [ "## Puzzel 10!!!" ] }, { "cell_type": "markdown", "id": "0897158c-8c18-467a-8706-23237c32c7e5", "metadata": {}, "source": [ "### Deel 1" ] }, { "cell_type": "code", "execution_count": 113, "id": "464d8ac2-9511-4fed-bdaa-1ae234c0c1e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(42, 8)" ] }, "execution_count": 113, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data = open('data/puzzle_10.txt', 'r').readlines()\n", "data = [re.sub('\\n', '', line) for line in data]\n", "start_position = [(i, line.index('S')) for i, line in enumerate(data) if 'S' in line][0]\n", "start_position" ] }, { "cell_type": "code", "execution_count": 43, "id": "0d3097b8-579b-4ae1-9c70-8dde061e9491", "metadata": {}, "outputs": [], "source": [ "def get_next_direction(current_direction, pipe):\n", " if pipe in 'FJ':\n", " current_direction = (-current_direction[1], -current_direction[0])\n", " if pipe in '7L':\n", " current_direction = (current_direction[1], current_direction[0])\n", " return current_direction" ] }, { "cell_type": "code", "execution_count": 91, "id": "ea659734-4403-4dd5-9bef-af4716f36674", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13676\n", "6\n", "13676\n", "60\n" ] } ], "source": [ "current_position = start_position\n", "for current_direction in [(1,0),(-1,0),(0,1),(0,-1)]:\n", " current_position = start_position\n", " steps = 0\n", " while data[current_position[0]][current_position[1]] != 'S' or steps == 0:\n", " current_position = (current_position[0] + current_direction[0], current_position[1] + current_direction[1])\n", " steps += 1\n", " current_direction = get_next_direction(current_direction, data[current_position[0]][current_position[1]])\n", " if data[current_position[0]][current_position[1]] == '.':\n", " break\n", " print(steps)" ] }, { "cell_type": "code", "execution_count": 159, "id": "8ff1ec45-cec0-4a8a-9a7a-0ce278ee064d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "451\n" ] }, { "data": { "text/plain": [ "[' ',\n", " ' ',\n", " ' ',\n", " ' F7 ',\n", " ' || ',\n", " ' |L7 F7 F--7 F7 ',\n", " ' L7L7F7|| L-7L7||F7 ',\n", " ' F-7F7 F-7L7||||L7 L7||LJL7 ',\n", " ' L7LJ| F7 L7| |LJLJFJ F-7FJ||F--J ',\n", " ' F-JF-J |L7 F7 F-7F-JL7L-7F-J F7 L7LJFJ||F7F7 F7 ',\n", " ' L-7| F-7|FJ |L7 L7|L--7L7FJL---7F-7 F-JL7F7F7 F--7F7L-7L7|||LJ| F7 FJL7 ',\n", " ' F--J|FJFJ|| FJFJ F-JL7F7|FJL-7F--J|FJ F7 L--7LJ||L-7|F-J||F7|FJ|||F-J FJL-7|F-J ',\n", " ' L--7||FJ ||F7L7|F7L--7||LJL7F-JL-7FJL7FJL--7 F7F7L7FJ|F-J||F-J||||L-J|||F7F7L-7FJ|| F7F-7 ',\n", " ' F7 FJLJ|F7||||FJ|||F7FJ|L--7|L7F--JL7FJ|F-7FJF7FJLJL7|L-JL7 ||L-7|||L-7FJ|||||| FJL-J|FJ|L7| ',\n", " ' |L7L--7||||LJ|L7LJ|||L7L7F7|L7|L-7F7||FJL7||FJ|L-7F-J|F--7L-JL-7||||F-J|FJLJLJL7L---7LJFJFJL-7 ',\n", " ' F7 |FJF--JLJLJF7| L-7|||FJFJ||L7|L-7LJLJLJF-JLJL7| FJL7 LJF-JF7F-7||LJ|L-7|L7F----JF7F-JF-JFJF7FJ ',\n", " ' ||FJ| L-7F7F--JLJF7FJ|||L7| || ||F7L-7F--7L-7 F7|L-JF-JF7 L--J|| |||F-JF7|| ||F7F7FJ|L-7|F-JFJLJ ',\n", " ' |||FJF7 LJ||F--7FJ|L7LJL-JL7|L7|LJ|F7||F7L--JFJ||F-7|F7||F-7F7LJFJLJ|F7|||L-JLJLJ|L7L7 ||L7FJ ',\n", " ' F7||||FJ|F--J|L7FJL7L7|F-----J|FJL7FJ||||||F7F7L7|LJ |||||||FJ|L7FJF-7||||||F--7F-7|FJFJFJL7||F7 F-7 ',\n", " ' |LJ|||L7|L--7| |L-7L7||| F-7F-J|F7|L7|||LJLJLJL7|L7F-J||LJ||L7L7|L7|FJ||||||L-7LJ LJL7|FJF-J|LJL7 L7| ',\n", " ' FJF7LJL7|| F-JL-JF-JFJ||L7|FJL-7|||L7LJLJF------J|FJL-7||F-JL7| |L7LJL7||||||F-J F7 F7|||FJF-JF--J ||F7 ',\n", " ' L-JL7F7LJL7L----7| FJFJL7LJL7F7|LJL7L-7F-JF---7F7|L7F7||||F7 |L7L7L---JLJ|||||F-7|L7|||||| |F-JF7 FJLJ| ',\n", " ' F--J|L-7FJF----JL7L7L7FJF--J||L7F-J FJL-7L7F7||LJFJ|||LJLJL7L7L7L------7LJ||||FJ|FJ|||||L-J|F7||F7F-7FJF--J ',\n", " ' F7 L--7|F-JL7L----7FJ |FJL7L7F7|| ||F7FJF--JFJ|LJL-7L7||L--7F-JFJFJF-----7L-7LJ||L-JL7|||||F7FJ|||||||FJL7L7 ',\n", " ' || F7LJL--7| F7F7||F-J|F7L7|||||FJ||||FJ F7L7|F7F-JFJ|L7F7|L-7L7L7L--7F7L--JF7LJF--7|||||||LJFJLJLJLJL7FJFJ ',\n", " ' F-J| F7F-JL-7F--JL7|LJ||LJF-J|| |LJLJ|L7|||||F7|L7|LJ|L-7L7L7||LJF-J |FJF-7LJL----J|F-JF7LJ|||||L-7|F7F-----J|FJ ',\n", " ' L-7|F7 ||L--7FJL---7|L-7|L-7|F-JL7L---7L7|LJLJLJ|L7||F-JF7L7L-J|L-7L7F7|L7|FJ F7 F7F7|L--JL7FJ||LJF-JLJ||F7F7 FJL-7 ',\n", " ' F7 ||||FJL7F7|L7F---J|F-JL7FJLJF--JF7F7L7|L7F7F--JFJLJL7FJL-JF--JF7|FJ|LJFJ|L7FJL-J|||L7F---JL7||F-JF7 FJLJLJL-JF--JF7 ',\n", " ' FJL-JLJ||F-J||L7LJF--7|L--7||F-7L--7|LJ|FJL-J||L7F7L7F--JL---7|F7FJ||L7|F7L7L7||F7F7LJ|FJL-7F7FJ||L-7||FJF-7F---7L---J| ',\n", " ' |F7F7F7|||F7|| L7FJF-JL7F7|||| L---JL7FJL7F--JL-J|| |L7F7F--7|LJ|L7||FJLJL7|FJ|LJLJL-7||F7FJ||L7||F-J||L7L7|L--7|F----J ',\n", " ' ||LJLJ|LJ||LJ|F7LJ L--7||||LJL--7F7 FJL7FJ|F7 F7||FJFJ|LJF-JL-7|FJ||L7F7 ||L7| F7F7FJ|LJ|L-JL7|LJL-7|L-JFJL---J|L---7 ',\n", " ' LJF7 L-7|L7FJ||F7 F7FJLJ|L-7F--J||FJF-JL7||L7FJ||||FJ L-7| F--J||FJ|FJ|L7|L7|L7|LJ|L7L7FJF---J|F---JL7F7L----7 L----J ',\n", " ' || F--JL7|L7|||L-J|L--7L7FJL-7FJ|L7|F7FJ||FJ|FJ|||L-7 FJL7L--7LJL7|| L7||FJ|FJL-7|FJFJL7| F7 ||F7F7FJ||F7F7FJF7F7F7 F7 ',\n", " ' FJL7L---7|L7|||L--7|F--JFJL-7FJL7L-J|||L7LJL7||FJ||F-JFJF-JF-7L--7||L7FJ||L7|L7F-JLJFJ FJ|FJL7|||||LJFJLJLJLJFJLJLJL-7|| ',\n", " ' L-7|F---JL-J|||F7FJ|L--7|F7 ||F7L-7FJ|| L-7FJ||L7|||F7|FJF7L7| F7|||FJL7||FJL7|L7F--JF7L7|L7FJ|LJ||F-J F7 F7 L7F----7LJL7 ',\n", " ' F-J|L------7LJ|||L7L-7 |LJ|FJ|||F7||FJ|F7FJL7|L7|||LJ||L7||FJL7||||||F7|||L7FJL7|L7F7||FJL7||FJF-J|| F-JL-JL7FJL---7L---J ',\n", " ' L-7L-------JF7LJL-JF7|FJF7||FJ|||||LJFJ|||F7||FJ|||F7|L7LJ|L7FJ|||||||||||FJL7O||FJ|||LJF7LJLJOL--JL-JF--7F-J|F----J F-7 ',\n", " ' F-7 L7F-7F7F--JL--7F7|||L-J||||FJLJLJF-JFJ|LJ|||L7||LJ||FJF-JFJL-J||LJ||LJLJ|F-JFJ|L7|LJOFJL-------7F7F-JF7LJF-J|F7F7F7|FJ ',\n", " ' L7| LJ LJLJ F-7F-J|LJ|L---JLJ|L-7F--JF7|FJF-JLJO||L7FJ|L7L-7|F---J|F-JL---7|L7OL7L7|L7F7L-7F-7F--7||||F-JL-7|F-J|||||||| ',\n", " ' |L7F-7F7F7 FJFJL--J FJF7F---7L-7||F7FJ||L7L----7||O|L7L7|F-J|L7F-7|L7F7F7FJ|FJF7|FJ|FJ|L--JL7LJOFJLJLJL7F--J||F7|||||LJL-7 ',\n", " ' |FJL7LJLJL7L7L-----7L-JLJ F7L7O||LJ|L7|L7L7F--7|LJFJFJFJ|L7FJFJ|O||OLJ|||L7|L-J||L7LJOL--7F-JF--J F7 F7||F7 |LJLJLJLJF---J ',\n", " ' F7F-J|F7|F---7L-JF----7L7 F-7FJL-JFJL-7L7|L-JFJL-7||F7L7|OL7|FJ|FJFJFJL--7||L7|L7F7|L7L-7OF7FJ|F7L----JL7|||||L-JF7F---7FJF-7 ',\n", " ' |LJF7|||LJF7 L---JF7F7L7L7|FJL--7OL7F-JFJL7F-JF7FJ|LJ|FJL7FJ|L7|L7|FJF-7FJ|L7LJO||LJO|F7L7||L7||L------7LJLJLJF7FJ|L-7 |L-JFJ ',\n", " ' L--J|LJ|F7||F---7FJLJ| L7LJ|F7F-JF7LJF-JF7||F7||L7|F-J|F7|L7|O||FJ|L7L7|L7L7|F--J|OF7LJL-J||FJ||F-7F7F7L-7F-7FJLJFJF7|FJF7FJ ',\n", " ' F-7FJF7LJLJ|L-7FJL--7L-7L-7LJLJF-JL7OL-7|LJ||||L7||L-7LJ|L7|L7LJL7|FJFJL7L7LJL7F7L-JL----7||L-JLJOLJLJL-7|L7|L--7L-JLJ|FJ|L7 ',\n", " ' F7L7|L-JL---7|F7|| F--JF7L-7L----JF-7L7F7LJOFJ||L7||L7FJF-JFJL7|F--J||FJF7|FJOF7||L7F---7F7||L--7F7F------JL7||F--JF7F7 || L-J ',\n", " 'F--J|FJ|F------JLJLJL-JF7FJ|F7L------JOL7LJL--7L7||FJ|L7||OL-7L-7LJL--7||L-J|||F-JLJL7||F--J|LJL--7|||L------7 LJLJF--JLJL7|L-7F7 ',\n", " 'L--7|L7LJF------7F7F-7FJ|| LJL---------7|F---7|FJ|LJO|FJLJF--JF7L-7F--J|L7F-JLJ|F7F7FJLJL7F7L-7F--JLJL------7L--7F-JF7F---JL-7LJL-7 ',\n", " 'F--JL-JF-JF7F--7LJLJ LJ LJF----7F------JLJF7FJ|L7L-7FJL--7L7F7||F7|L-7FJFJL-7F7|||||L---7LJ|F-JL-----7F----7L---J|F-JLJF----7|F7F-J ',\n", " 'L7F7F--JF-J||F7L7 F7 F7 F7L---7LJF----7OF7||L-JOL7FJL7F7FJO||||||LJF-JL7L7F7LJ|||LJ|F7F7|F-J|F-------J|F---JF--7 || F7 L7F7FJ||LJ ',\n", " ' LJ|L7F-JF7LJ|L7L-JL-JL-JL-7 FJF-JF--7L-JLJ|OF7OO|L-7||LJF7|||||L-7L-7FJO|||F-JLJOFJ|||LJL7FJL7F7F7F-7|L-7F7L7FJFJL-JL--J|LJ LJ ',\n", " ' L-JL7FJL-7|FJF7F-7F-7F-7L-JFJF7L7OL7F7F7L-JL7FJF7||L7FJLJ|LJL--JOFJ|F-J||L-7OF7L7||L-7FJL7FJ|||||OLJF7LJL-JL-JF7F7F--7L7F7F----7 ',\n", " ' ||F--J|L-J|L7||FJ| L-7FJFJL-JF7LJLJL-7F-JL-J|||FJL7F7L---7F--JFJL-7||F7|FJL-J|L7FJL7FJL-JLJLJF--JL------7FJLJ|L7 L7LJ||F---J ',\n", " ' ||L--7| F7L7|LJL7|F--J| L----JL-7F7F7LJF-7F7||LJF-J||F7F7|L7F-JF--J|LJ||L7F-7|O||F-JL7OF7F7F7L---------7LJ F7L7L-7|F7LJL-7 ',\n", " ' LJ LJFJ| LJF--J|L-7FJF---7F7F7LJLJL-7L7LJ||L-7L7FJ||LJLJOLJF7L7F7L-7||FJ|FJL7|||F7FJFJ||LJ|F7F---7F--J F-J| L7FJ|||F7F-J ',\n", " ' F7L7|F7 L--7|F-J| L7F7LJLJL7F---7L-JF-JL-7|FJL7|L7OOOF--JL-J||F-JLJL-JL--JLJLJLJOL7|L-7|||L--7|L----JF7|F7LJ LJLJLJ ',\n", " ' FJL-JLJ|F7F7LJL--JF7LJ|F---7||F7OL7F7L7F7OLJ|F-JL7L7F7L-7F7F-J|L---7F------7F7F-7F-JL--JLJL---J|F-----J|LJ|F--7F7F-7 ',\n", " ' F-JF7F--7LJLJL------JL-7LJF-7|LJ|L-7|||FJ||F7O|L7OFJFJ|L--J||L7OL7F-7||F7F7F7LJLJFJL7F7F-7F---7F7LJF7 F-7|F7LJF7LJLJFJ ',\n", " ' L--JLJF7L------------7FJ FJOLJOFJF7||||L-J||L7|FJFJFJOL7F7FJ|FJF-JL7||||LJLJL---7L-7LJLJOLJF7OLJ|F-JL7|FJLJ|F-JL-7F7L7 ',\n", " ' F-----JL7F7 F-7 F7F7FJL-7L--7F7L7|LJLJL7F-J|FJLJOL7L--7LJ|L7||FJF7FJLJLJOF7F7OF-JF7L7F-7F7FJL--7LJF-7LJL-7 LJF7F7LJL7| ',\n", " ' L7F----7LJL7L7|FJLJ|L7F7|F--J|L-JL----7LJOFJ|F7F7OL7F7|OOL-J||L7||L-7F---JLJL-JF7|L7|L7|||L---7L--JFJF7F-JF7FJLJL7F7LJ ',\n", " ' LJ F7 L--7|FJLJF-7| ||LJL---JF-------JOF7|FJ|LJL7OLJLJOOOOOLJOLJL7FJ|F--------JLJFJL-JLJL--7OL---7L-J|L--JLJF--7LJL---7 ',\n", " ' F7FJL----JLJF-7|FJ|FJL------7L--7F7F7OFJLJL-JF7FJOOOOOOOOOOOOOOOO|L7||F---------7L--------7|F----JF-7|F7F---JF7L------J ',\n", " ' FJLJF-------7L7LJL7LJF7F----7|F7FJ|||L-JF-7F--JLJOOOOOOOOOOOOOOOOOL-JLJL7F-7F-7F7L---------JLJF---7|FJLJLJF---JL7 F--7 ',\n", " ' L---JF------JFJ F7L7FJLJF7F-JLJ|L7|||F--JFJL---7OOOOOOOOOOOOOOOOOOOOOOF7LJFJL7||L7F-7F7F7F7F7FJF--J|L-7 F7|F-7F7L-JF7L-7 ',\n", " ' F--7L---7F7FJ FJL7LJF--JLJF7F7L-J|LJL--7L7F7F7|F-7OOOOOOOOOOOOOOOOOOFJ|F7L--J||OLJOLJLJLJLJLJOL--7|F7L7|LJL7LJL---JL-7| ',\n", " ' L-7L-7F-J|||F-JF7L--JF7F--JLJL7F7|F-7F7L7LJLJ|LJFJOOOOOOOOOOOOOOOOOOL7|||OF7OLJF-7F-7F7F-7F7F-7F7LJ|L-J|F--JF----7 F7LJ ',\n", " ' F7L-7|L--JLJL--JL--7FJ||F-----J||||O|||FJOF7O|F-JOOOOOOOOOOOOOOOOOOOFJLJL-JL7F7L7|L7LJ|L7|||L7LJL7FJF7 |L---JF--7L-JL7 ',\n", " ' F7FJL--J|F7F7F7F7F----J|FJ|L------JLJL7LJ|L--JL7LJF7F7OOOOOOOOOOOOOOOF-JF7F7F-7LJL-JL7L-7|FJLJL7L--7||FJL7|F----JF7L---7| ',\n", " ' |LJF---7LJLJLJLJLJF----JL-J F7F7F7F7F7L-7L7F7F7|OO||||OF7OOOOOOOOOOOOL--JLJ||FJF7F-7FJF7|LJF--7L7F-JLJ|F-J||F----JL7F-7LJ ',\n", " ' L-7|F7 L-7F-------JF7F7 F7F-JLJLJLJ|||F7L7LJLJLJOFJ||L-JL7OOOOOOOOOOOOF-7OOLJL7|LJFJ|FJLJF-JF-JFJL----JL--JLJF-----J|FJ ',\n", " ' LJ|L---J| F---7F-JLJL-JLJF-----7OLJLJL-JF7F-7F7L7||F-7FJOOOOOOOOOF-7L7|OF7F7LJF7L7LJF-7|F-JF7L7OF7F-7F7F-7FJF7F--7|L7 ',\n", " ' L7F---JFJF-7|L--7F--7F-JF7F--JF7F----7|||FJ||FJLJ|OLJOOOOOOOOOOL7|FJL-JLJ|F7||FJF7L7LJL7FJL7L-JLJ LJLJ |L-J||F-J|FJ ',\n", " ' LJ F--JFJFJL7F7LJF7||F-JLJF7FJLJF7F7LJ||L-JLJF7FJF7OOOOOOOOOOOO||L-----7|||||L7||FJOF7LJF7L--------7F7|F7FJ|L--JL-7 ',\n", " ' F---JF--J |F7LJ|F7||LJL----JLJF--JLJ|F7LJF-7F7|||O|L-7OOOOOOOOOO|L------JLJLJL7LJ||F-JL--JL---------J||LJ|L-JF7F7F7|F7F7 ',\n", " ' F-JF---JF---J||F7LJLJL7F7F------JF7F-7LJL--JOLJLJLJFJF-JOOOOOOOOOFJF7F---------7L--J|L----7F7F7F-------JL-7L7F7|LJLJLJ|||| ',\n", " ' L7FJ F-7L----JLJ|F7F-7LJ|L-7F7F--JLJFJOF-----7F7F--JFJOOOOOOOOOOOL-J||F-------7|F7F7|F----J|LJLJF7F------7L7LJLJF7F7F7|LJL---7 ',\n", " ' ||F-JFJF-------J|LJ |F7L--J|LJF----JOFJF----J|LJF7FJOOOOOOOOOOOOOOOLJL------7LJ|||||L--7F7L7F--JLJF7F--7L7L--7FJLJLJ|L-7F---J ',\n", " ' LJL-7L7L----7F-7|F--J|L----JF7L-----7L7|F-7F7|F-JLJOOOOOOOOOOOOOOOOOOF------JF7|LJLJF-7LJ|FJL---7 |LJF-J L7F-J|F7F7FJF-JL-7 ',\n", " ' F7L7L----7|L7LJL7F7L7F----JL----7FJO|LJFJ|||L7F7F7OOOOOOOOOOOOOOOOOL---7F7FJ|L7F7OL7|F7LJF7F--JFJF7|F7F7|L7FJ|LJLJFJF---J ',\n", " ' || L-7F-7LJFJ F7||L-JL--------7OLJOFJF7|FJLJFJ|LJL7OOOOOOOOOOOOOOOOOF--J|LJOL-J||F7||||F7||L---JFJLJ|LJ||FJL7L7F-7L7L-----7 ',\n", " ' |L---J| L-7|F7|LJL7F----------JF7F7L7|LJ|F-7L-JF7FJF7OOOOOOOOOOOOOOFJF7FJF7F--7|||||LJLJLJL7F---JF7 |F-JLJF-JFJL7L-JF-----J ',\n", " ' F7L-----JF7 LJ|LJF7FJL-7F-------7|LJ|FJL--JL7L7F7|LJFJL7OOOOOOOOF---7L-J||FJLJF7LJLJ||F-7F--7|L----JL7|L--7 L-7L--JF--JF7 ',\n", " ' |L-------JL---JF7|||F7FJ|F------JL-7LJF7F-7FJFJ|||F7L7FJF7F7F7F7L--7|OOOLJL-7FJL---7LJL7LJF7|L--7F7F7||F--JF7FJF---JF-7|L7 ',\n", " ' L----------7F-7|||LJ||L-J|F----7OF7|F-J|L7|L7L7|LJ|L7||FJ|||||||OF-JL7OF---7|L7OF7FJF--JF-JLJF-7LJLJLJ||F7 ||L7| F7 L7||FJ ',\n", " ' F--7F------7LJ ||||F-JL---J|F---JFJLJL-7L-JL7L7LJF-JFJ|||FJ||||||OL--7L7L--7|L-JFJLJFJOF-JF7F7|FJF7 F7FJLJL-JL-JL7||F-JLJL-7F-7 ',\n", " ' L-7|L-----7L---J|||L-------JL----JF---7L--7O|FJF7L-7L-J||L-J||LJ|F---JFJF-7|L7OFJF7FJF7L7FJ||LJL-J|FJLJF7F7F----7LJ||F-----J|FJ ',\n", " ' F-JL7 F7F-JF7F7FJ||F---7F---7F-7F7L-7FJF--JFJ|O||F7L-7FJL7F-J|F-JL-7F7|FJFJ|FJFJFJ||FJ|OLJOLJF---7LJF-7|LJLJF-7FJF7LJL----7FJL7 ',\n", " ' L7F7|FJLJF7|LJ|| ||L--7|L--7|L7|||F-JL-JF7OL-JFJ||L--J|F7||F7|L7F7OLJ|||FJFJL7L7L7LJL7L--7OF7L--7L-7L7|L7F7 |FJL7||F-7F--7LJF-J ',\n", " ' FJ||LJF--JLJF-JL7LJF--JL---JL7|LJLJOF7OFJL-7F7L7|L-7F7LJ||LJLJFJ|L7OFJLJL7L-7L7L-JF-7L7F7L-JL---JF7|FJL7LJL-JL-7||||FJL-7L--J ',\n", " ' L-JL-7L7F---JF-7L7FJF7F-----7LJF7F7FJL-JF--J||FJL7OLJ|F-JL-7F-JOL7L7L7F--JF7L7L7OFJFJFJ|L7F7F7F-7||LJ L----7F7|LJLJL-7FJF-7 ',\n", " ' FJFJL---7| L-JL-J||F----JF7|LJLJF--7L--7|||F-JF7FJL7F7FJ|F7F7L7L7|L--7|L-JFJFJFJOL-JOLJLJLJOLJL-7 F7F7F7LJLJ F7F-7|L-JFJ ',\n", " ' F7F7L7L-7F-7LJF7F7F--J|L-----JLJF7F7|F7L---J||||F7||L7FJ||L7|||||FJFJ|F--JL7F-JOL7|F7F7F7F7F-------7L7|||LJL-7F--JLJFJ|F-7L7 ',\n", " ' F7|||| L--JL7|F7||||L---J F-------JLJLJ|L7F7F7||||||||FJ|FJL-JLJ||||FJFJL7F7FJL7F-7|||||||||||F7F---7L7LJLJF7F-JL7F---JFJ|FJFJ ',\n", " ' |LJLJL-7F-7FJLJ|||||F7F7F7L----7OF7F-7FJFJ|||||||||LJLJFJL-----7||LJL7L7FJ||L7FJL7|||||||||||LJLJF--JFJF7F7||L-7FJL---7L7|L-J ',\n", " ' L-----7||FJL7F7LJLJLJLJ|||F7F-7L-JLJFJL7|FJ||||LJ|L-7F-JF-7F--7||L-7FJO|L7|L7||OFJLJ||||LJ||L7OF7L--7L-JLJLJL-7LJF----J |L7F7 ',\n", " ' F7FJLJ|F7LJ|F------7LJLJ|L7|F7F7FJF-JLJFJ|||F-JF7||OFJFJL-7||L-7||F7L7||FJ|L7L-7FJ|||F7LJFJFJL---J F------7|F7L----7 L7||| F7 ',\n", " ' F7FJ|L-7FJ||F7LJF7F7F-JF7F7L-J||LJLJFJF---JFJ|||F7|LJL7L7|F--JLJF-J|LJ|O|LJL7L7L--J|O||LJL7FJOL-------JF7F--7|||L-----JF7LJ|L-JL7 ',\n", " ' FJ|L7|F-J| |||| FJLJ|L--JLJL--7LJF7F7L7|F-7O|FJ||||L--7L-J|L----7L-7L7FJFJF--JOL--7FJFJL--7|L-7F-----7F7|||F7LJLJ F----7|L--JF---J ',\n", " ' L7L-J|L-7L-JLJL7L--7L7F7F7 F--JOFJ||L7|||FJFJ|FJ|||OF-JF--JOF--7L-7L7|L7L7|F7F7F7O|L7L7F--JL7FJL----7||LJLJ|| F7F7|F---J|F---J ',\n", " ' L--7|F7L-7F7F7L-7 L7LJLJL7L-7F-JFJ|FJ||||O|FJL7LJL7L-7L---7L-7|F7L7||FJFJLJ|||||FJFJFJL7OF7||F7OF7FJLJ F-7|L7|||LJL7F--JL----7 ',\n", " ' F7F--J|||F-J|LJL-7|F-JF-7F7L--JL-7|FJL7||||FJL7FJF--JF7|F---JF-JLJL7||||OL7F7LJLJ|L7L7L-7L7||||||FJLJF7F7|FJ|FJ|||F--J|F--7F---J ',\n", " ' F-JLJF-7|||L-7|F7F-J|L-7L7LJL7F-7OFJ||F-J|LJ|L-7|L7|F7O||||OF7OL----7||LJL7FJ|L7F--JOL7|F7|FJ|||LJ|L---JLJLJL7||FJLJL---JL-7|L---7 ',\n", " ' L-7F7|FJLJL7 LJ|LJF7L--JFJF--J| L7L7LJL7FJF-JOFJL7|||L7|||L7|L-7F--7|||F--JL-JFJL7F7F7||||||FJ|L-7L7F7F--7F-7LJLJF7F-7F----J|F---J ',\n", " ' LJLJL-7F-JF--JF7|L----J |F7FJF-JOL-7FJL7|OF7L7FJ||L7||||FJL7FJL7FJ||||F7F7F7L7FJ||||||||||L7|F7|FJ||L-7|L7|F7F-JLJFJL----7|L--7 ',\n", " ' F7F7F7 F-JL--JF7FJ||F-----7LJLJFJF7F7FJL7FJL7||FJ|FJL7|||||L7FJL7FJ|FJ||||||||L7|L7|||LJ|||||FJ|||||FJ|F7|L7|LJ|L---7L7F7F--JL---J ',\n", " ' |LJLJL-JF7F-7FJ|| ||L----7| F--JFJ||LJF-JL-7||LJ LJ FJ|||||FJ|F7||FJL7LJ||LJ||FJL7|||| FJ|LJ||FJ|||LJ LJ|L7LJF7L7F--JFJ||L------7 ',\n", " ' L--7F7F7|LJFJL7||FJ|F--7FJL-JF7FJ |L-7L7F7FJ|L--7 F7L7|LJ||L7LJ|LJL-7L7FJL-7||L7FJ||||FJFJF-J|L7||L---7 L-JF-J|FJL--7L7||F7F7F7FJ ',\n", " ' F7FJ|LJ|L7 L--JLJL7||F7LJF-7FJ|L-7|F7|FJ||L7|F--JFJL-JL-7LJ |F7L-7F7|FJL7F7||L7||FJ||||FJFJF7|FJ|L-7F-JF---JF-JL7F-7L7||LJLJLJLJ ',\n", " 'F-JLJFJ FJFJF----7 FJ|LJL--J || L-7|||||L7||FJ|L-7 L7F7F7FJ F-J||F7||LJL-7||LJL7|||L7||||L7|FJ|||FJF-JL--JF---JF7FJL7|FJ|L----7 ',\n", " 'L7F--J |FJFJF7F7L-JFJ F-----JL--7LJ||LJ |||| |F-JF-J||||L7FJF7|LJLJL7F--J|L--7|||L7LJLJ|FJ||FJ||L7L7F-7F7L----J|L7FJLJFJF7F--J ',\n", " ' || F7FJL7L7||||F7FJF7L-7F7F7F7FJ FJ| F-J||L7LJF-JF7||||FJ|FJ|L--7F-JL-7FJF7FJ||L7|F---JL7||L7||FJ || LJL7F7F7FJ |L-7 L-JLJ ',\n", " ' LJF-JLJF7|FJ|LJLJLJFJL-7LJ|||||L-7L7|FJF7|L7L-7|F7||||||L7|L7L7F-J|F7F7||FJLJFJL7|||F--7FJLJFJ|LJ FJL7F7FJ|LJLJF7|F7L----7 ',\n", " ' L---7|LJL-J F7F--JF-7L--J||||F-J LJ|FJ|| |F-J||||LJ|||FJ|FJFJL-7LJ|||||L--7L--J||||F-JL-7 L7L-7FJF7LJ|L7L-7F7||LJL7F-7FJ ',\n", " ' F--JL7 F---JLJF-7L7L7F7FJ||LJ F---J| LJFJL-7LJ|L7FJ|||FJ| |F-7L-7||||L7F-JF-7 LJ||L----JF7|F7|L-J|F-J |F7LJLJL--7LJ |L---7 ',\n", " ' |F7F7L7|F-7F7FJFJFJFJ|||FJL--7L-7F7L-7FJF--JF7|FJL7|||L7L7|L7|F7|||LJFJL--JFJF7 LJF-----JLJ||L-7FJL--7LJ|F--7F--JF7FJF7F-J ',\n", " ' LJLJ|FJ||FJ|||FJFJ L7|||L7F-7L7FJ||F7|L7L-7FJLJ|F7|||| L7||FJ||LJLJF-JF7F7FJ |L--7L--7F7F7FJ|F-JL7F--JF7|L-7|L---J|L7|LJ ',\n", " ' F--J| LJL7|LJL-J LJ|| LJFJFJL7||||L7|F7|L7F7||LJ|LJF-J||L7|L---7|F7|LJ|L--JF7FJ F7LJ|||L7|L7F7|L---J||F-J|F7F7FJ LJ ',\n", " ' |F--J FJ|F-7 F7 F--JL--7L-JF-J|||L-J||LJ ||LJL-7|F7L7FJ|FJ|F--7|||LJ FJF7F-J||F-JL--J||FJ|FJ|LJF7F-7||L-7||LJ|L-7 ',\n", " ' LJ |FJL7L-JL7L-7F7F7L7F-JF7||L-7 LJ F-JL7F--J||L-JL7|L7|L-7LJLJF--JFJ|L-7||L-7F-7FJ|L7LJ L-7|LJ LJ|F7||L-7|F7| ',\n", " ' || L7F-7L--J|LJ|FJL7FJ|||F7L-7F7L--7|L7F7|L7F--JL7|L7FJF--7L7F7| |F-J||F-JL7||FJFJF----JL---7FJ|LJL7FJLJLJ ',\n", " ' LJ FJ|FJF7F7L-7|L7 || LJLJL7FJ|| F-JL7LJ||FJL---7LJ ||FJF7|FJ||L7|L7FJ|L7F7|LJL7L7|F---7F-7FJL7L--7|L-7 ',\n", " ' |FJL7|LJL7FJL-JFJL---7F7|L7|L-JF-7|F-J|L-7F7FJF--JLJFJLJL7|L7||FJL7|FJ|LJ F-JFJLJ F-JL7LJF7L7F-J|F7L-7 ',\n", " ' LJ || F7|L-7F-JF7F--J||L-J|F-7L7||L-7L7FJ||L7L7F7F7L7 F7|L7|||L-7LJL7L--7L--JF7F7L7F7L--J|FJL7FJ||F-J ',\n", " ' FJ|FJLJF7||F7||L---JL-7FJ| |FJLJF7|FJL-J|FJ LJ||L7L7||L7|||L7FJ F-JF--J F7FJLJL-J||F--7|L7FJL7||| ',\n", " ' L7||F-7|LJ|||||F-7F7F-J|FJFJL-7FJLJL---7|L7F--JL7L7LJ|FJ|LJ ||F-JF-JF---JLJF--7F7||L-7LJFJL-7|||| ',\n", " ' LJ||FJL-7LJ|||L7||LJF-JL7L7F7|L--7F--7|L7||F7F7|FJF-JL7L7 LJL7FJ L-7F7F7|F-J||||F-JF7L---J||LJ ',\n", " ' ||L-7FJF-J||FJ|L-7L-7FJ ||||F--JL-7LJFJ|||||||L7L--7L7L7 F-JL7 F7LJLJLJL7FJ|||L--JL---7 LJ ',\n", " ' LJF-J|FJF7|LJ |F-J || |||||F7F7FJF7L7|LJ||||FJF--J L7L7 L-7FJF-JL-7 F---J|FJ||F--7F7F7| ',\n", " ' L--JL-J|L7 || FJL7 LJLJ|||||L-JL7LJF-J|LJL7L-7 L7L7 LJ L-7F7L7L---7|L-J||F7LJLJLJ ',\n", " ' FJFJ LJ L7FJ LJLJ|F7F-J |F-JF--JF-J L-J ||L7L----J|F--JLJL------7 ',\n", " ' F-JFJ LJ F-J||| |L7 |F-7L7 LJ L-7F--7|L---7F-------J ',\n", " ' L--J |F7|LJ L-J LJ L7| ||F-J|F---JL--7F7 F7 ',\n", " ' ||LJ FJ| |||F-JL-7F7F7FJ|L-J| ',\n", " ' F-JL7 L-J LJ||F---J||||L-JF7FJ ',\n", " ' L---J LJL7F7FJ|||F--J|L7 ',\n", " ' ||LJFJ||L--7L7L7 ',\n", " ' LJF-JFJ|F7FJ L-J ',\n", " ' L7FJ |||L--7 ',\n", " ' LJ LJL---J ']" ] }, "execution_count": 159, "metadata": {}, "output_type": "execute_result" } ], "source": [ "current_position = start_position\n", "loop = [[{'direction': (0,0), 'letter': '.'}] * len(line) for line in data]\n", "current_direction = (1, 0)\n", "steps = 0\n", "while data[current_position[0]][current_position[1]] != 'S' or steps == 0:\n", " current_position = (current_position[0] + current_direction[0], current_position[1] + current_direction[1])\n", " current_direction = get_next_direction(current_direction, data[current_position[0]][current_position[1]])\n", " letter = data[current_position[0]][current_position[1]]\n", " loop[current_position[0]][current_position[1]] = {'letter': letter}\n", " steps += 1\n", "\n", "loop[42][8] = {'direction': (1, 0), 'letter': 'F'}\n", "grid = []\n", "total = 0\n", "for line in loop:\n", " row = \"\"\n", " in_loop = False\n", " last = ''\n", " for item in line:\n", " if item['letter'] in 'FL':\n", " last = item['letter']\n", " elif item['letter'] in '||':\n", " in_loop = not in_loop\n", " elif item['letter'] == '7' and last == 'L':\n", " in_loop = not in_loop\n", " last = item['letter']\n", " elif item['letter'] == 'J' and last == 'F':\n", " in_loop = not in_loop\n", " last = item['letter']\n", "\n", " if item['letter'] == '.' and in_loop:\n", " row += 'O'\n", " total += 1\n", " elif item['letter'] != '.':\n", " row += item['letter']\n", " else:\n", " row += ' '\n", " grid.append(row)\n", "\n", "print(total)\n", "grid\n", "\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }