Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamatti committed Dec 6, 2023
1 parent 7c2bfed commit dcfcdc6
Showing 1 changed file with 19 additions and 23 deletions.
42 changes: 19 additions & 23 deletions src/day_6.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "e28d36d4",
"id": "6e1ee4e6",
"metadata": {},
"source": [
"> # Day 6: Wait For It\n",
Expand Down Expand Up @@ -54,7 +54,7 @@
},
{
"cell_type": "markdown",
"id": "0433d5d3",
"id": "5bbb30a3",
"metadata": {},
"source": [
"## Read input\n",
Expand All @@ -67,7 +67,7 @@
{
"cell_type": "code",
"execution_count": 1,
"id": "cdea1bfa",
"id": "ebe6d36b",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -82,7 +82,7 @@
},
{
"cell_type": "markdown",
"id": "75e0f224",
"id": "eba5f04e",
"metadata": {},
"source": [
"My first solution to this was to loop through every potential millisecond from 0 to `time` and then see how many of them are winning."
Expand All @@ -91,7 +91,7 @@
{
"cell_type": "code",
"execution_count": 2,
"id": "9c5d6b96",
"id": "2e0ca511",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -106,7 +106,7 @@
},
{
"cell_type": "markdown",
"id": "20875f24",
"id": "06175401",
"metadata": {},
"source": [
"The slight issue with this though is that when the numbers get large (as they did in part 2), there's a lot of unnecessary looping. The reason for that is that all the failing cases happen at the beginning and at the end.\n",
Expand All @@ -117,7 +117,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "0d955b88",
"id": "dacb917c",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -140,7 +140,7 @@
},
{
"cell_type": "markdown",
"id": "ef7a530d",
"id": "ab32a811",
"metadata": {},
"source": [
"To get the final result, we combine the times and records with [zip](https://docs.python.org/3.3/library/functions.html#zip) that is a very handy function. It takes any number of iterables like lists or tuples and on each iteration, takes the nth item from each iterable.\n",
Expand All @@ -158,7 +158,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "c53b2699",
"id": "4c857c09",
"metadata": {},
"outputs": [
{
Expand All @@ -179,7 +179,7 @@
},
{
"cell_type": "markdown",
"id": "c5ac29dc",
"id": "11552a7a",
"metadata": {},
"source": [
"## Part 2\n",
Expand All @@ -204,7 +204,7 @@
},
{
"cell_type": "markdown",
"id": "ff5aac58",
"id": "1575408f",
"metadata": {},
"source": [
"Them pesky elves with their incoherent writing again. All we need to do here is to fix the input by combining the numbers into a single string and then calling the function once more.\n",
Expand All @@ -215,33 +215,29 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "be9ca40f",
"id": "e8d65b5b",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'distances' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[5], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m new_time \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;28mstr\u001b[39m(t) \u001b[38;5;28;01mfor\u001b[39;00m t \u001b[38;5;129;01min\u001b[39;00m times))\n\u001b[0;32m----> 2\u001b[0m new_distance \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mint\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;241m.\u001b[39mjoin(\u001b[38;5;28mstr\u001b[39m(d) \u001b[38;5;28;01mfor\u001b[39;00m d \u001b[38;5;129;01min\u001b[39;00m \u001b[43mdistances\u001b[49m))\n\u001b[1;32m 4\u001b[0m part_2 \u001b[38;5;241m=\u001b[39m find_winning_strategies(new_time, new_distance)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mSolution: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mpart_2\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n",
"\u001b[0;31mNameError\u001b[0m: name 'distances' is not defined"
"name": "stdout",
"output_type": "stream",
"text": [
"Solution: 40087680\n"
]
}
],
"source": [
"new_time = int(''.join(str(t) for t in times))\n",
"new_distance = int(''.join(str(d) for d in distances))\n",
"new_record = int(''.join(str(d) for d in records))\n",
"\n",
"part_2 = find_winning_strategies(new_time, new_distance)\n",
"part_2 = find_winning_strategies(new_time, new_record)\n",
"print(f'Solution: {part_2}')\n",
"assert part_2 == 40087680"
]
},
{
"cell_type": "markdown",
"id": "274190ab",
"id": "b83f9d2a",
"metadata": {},
"source": [
"## Two stars!\n",
Expand Down

0 comments on commit dcfcdc6

Please sign in to comment.