diff --git a/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb b/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb index 8817e2a..dfe7bd4 100644 --- a/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb +++ b/src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb @@ -64,7 +64,7 @@ }, { "cell_type": "code", - "execution_count": 110, + "execution_count": 18, "metadata": { "id": "p5Ki_HvOJUWk", "tags": [] @@ -258,7 +258,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 21, "metadata": { "id": "lwP0r3BAaxjt", "tags": [] @@ -331,7 +331,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 22, "metadata": { "id": "Y6nbd6WMryWi", "tags": [] @@ -359,7 +359,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 23, "metadata": { "id": "A8sU4Ez_bBKl", "tags": [] @@ -529,7 +529,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 24, "metadata": { "id": "DT5FSYliC9wp", "tags": [] @@ -8199,7 +8199,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -8310,10 +8310,12 @@ }, { "cell_type": "code", - "execution_count": 156, + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ + "### OLD ###\n", + "\n", "CHANGES_SEQ_LEN = 4\n", "SECRETS = 2000\n", "\n", @@ -8325,13 +8327,12 @@ " diffs_for_buyer: dict[int, list[int]] = {}\n", " all_change_sequences: set[tuple[int]] = set()\n", " \n", - " secret = 123\n", " # for buyer, secret in enumerate([123]): # Test buyer\n", " for buyer, secret in enumerate(initial_secrets):\n", " logger.debug(f\"{buyer=},{secret=}\")\n", " \n", " # list the first n prices for this buyer\n", - " for price in range(SECRETS+1):\n", + " for _ in range(SECRETS+1):\n", " prices_for_buyer[buyer].append(secret % 10) # Get units column\n", " secret = generate_next_secret(secret)\n", " \n", @@ -8341,16 +8342,10 @@ " diffs_for_buyer[buyer] = tuple(j-i for i, j in zip(prices_for_buyer[buyer], prices_for_buyer[buyer][1:]))\n", " assert len(diffs_for_buyer[buyer]) == SECRETS, f\"Should be {SECRETS} diffs\"\n", " \n", - " # logger.debug(f\"For {buyer=}, diffs_for_buyer: {diffs_for_buyer[buyer]}\")\n", - " # for price, diff in zip(prices_for_buyer[buyer], diffs_for_buyer[buyer]):\n", - " # logger.debug(f\"{price=}, {diff=}\")\n", - " \n", " # Add sequences into set\n", " all_change_sequences.update(diffs_for_buyer[buyer][i:i+CHANGES_SEQ_LEN] \n", " for i in range(len(diffs_for_buyer[buyer])-CHANGES_SEQ_LEN+1))\n", " \n", - " # logger.debug(all_change_sequences)\n", - " \n", " bananas_for_seq = defaultdict(int)\n", " # Check if / where this seq appears in changes for each buyer\n", " for seq in tqdm(all_change_sequences):\n", @@ -8377,6 +8372,95 @@ " return 0\n" ] }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [], + "source": [ + "### OPTIMISING ###\n", + "CHANGES_SEQ_LEN = 4\n", + "SECRETS = 2000\n", + "\n", + "# def solve_part2(data):\n", + "# initial_secrets = list(map(int, data))\n", + "# logger.debug(f\"{initial_secrets=}\")\n", + " \n", + "# prices_for_buyer: dict[int, list[int]] = defaultdict(list)\n", + "# diffs_for_buyer: dict[int, list[int]] = {}\n", + "# all_change_sequences: set[tuple[int]] = set()\n", + " \n", + "# # for buyer, secret in enumerate([123]): # Test buyer\n", + "# for buyer, secret in enumerate(initial_secrets):\n", + "# logger.debug(f\"{buyer=},{secret=}\")\n", + " \n", + "# # list the first n prices for this buyer\n", + "# for _ in range(SECRETS+1):\n", + "# prices_for_buyer[buyer].append(secret % 10) # Get units column\n", + "# secret = generate_next_secret(secret)\n", + " \n", + "# # logger.debug(f\"For {buyer=}, prices_for_buyer: {prices_for_buyer[buyer]}\")\n", + " \n", + "# # determine the diffs between each price for this buyer\n", + "# diffs_for_buyer[buyer] = tuple(j-i for i, j in zip(prices_for_buyer[buyer], prices_for_buyer[buyer][1:]))\n", + "# assert len(diffs_for_buyer[buyer]) == SECRETS, f\"Should be {SECRETS} diffs\"\n", + " \n", + "# # Add sequences into set\n", + "# all_change_sequences.update(diffs_for_buyer[buyer][i:i+CHANGES_SEQ_LEN] \n", + "# for i in range(len(diffs_for_buyer[buyer])-CHANGES_SEQ_LEN+1))\n", + " \n", + "# bananas_for_seq = defaultdict(int)\n", + "# # Check if / where this seq appears in changes for each buyer\n", + "# for seq in tqdm(all_change_sequences):\n", + "# for buyer, diffs in diffs_for_buyer.items():\n", + "# if chg_idx := index_seq_complete(seq, diffs):\n", + "# bananas_for_seq[seq] += prices_for_buyer[buyer][chg_idx+1]\n", + " \n", + "# # logger.debug(f\"{bananas_for_seq=}\")\n", + "# most_bananas = max(bananas_for_seq.items(), key=lambda x: x[1])\n", + "# logger.debug(f\"{most_bananas=}\")\n", + " \n", + "# return most_bananas[1]\n", + " \n", + "# def index_seq_complete(change_seq: Iterable[int], diffs_for_this_buyer):\n", + "# \"\"\" Determines if this sequence is found in the diffs list.\n", + "# If so, it returns the index where the index COMPLETES. (Which must be > 0.)\n", + " \n", + "# Return 0 if the sequence is not found.\n", + "# \"\"\"\n", + "# for i in range(SECRETS - CHANGES_SEQ_LEN + 1):\n", + "# if diffs_for_this_buyer[i:i+CHANGES_SEQ_LEN] == change_seq:\n", + "# return i+CHANGES_SEQ_LEN-1 # the index where this seq completed\n", + " \n", + "# return 0\n", + "\n", + "def solve_part2(data):\n", + " initial_secrets = list(map(int, data))\n", + " \n", + " # Store sequences and their banana contributions\n", + " bananas_for_seq = defaultdict(int)\n", + "\n", + " for buyer, secret in enumerate(initial_secrets):\n", + " prices = []\n", + " diffs = []\n", + "\n", + " # Generate prices and calculate diffs in a single loop\n", + " for _ in range(SECRETS + 1):\n", + " prices.append(secret % 10)\n", + " secret = generate_next_secret(secret)\n", + " \n", + " diffs = [j - i for i, j in zip(prices, prices[1:])]\n", + " \n", + " # Extract sequences and calculate bananas for each sequence\n", + " for i in range(len(diffs) - CHANGES_SEQ_LEN + 1):\n", + " seq = tuple(diffs[i:i+CHANGES_SEQ_LEN])\n", + " bananas_for_seq[seq] += prices[i + CHANGES_SEQ_LEN]\n", + "\n", + " # Find the sequence yielding the most bananas\n", + " best_seq = max(bananas_for_seq.items(), key=lambda x: x[1])\n", + " return best_seq[1]" + ] + }, { "cell_type": "code", "execution_count": null,