diff --git a/Book_1.ipynb b/Book_1.ipynb index a217904..e4e7bc0 100644 --- a/Book_1.ipynb +++ b/Book_1.ipynb @@ -18,7 +18,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "1e6ac084-5bf9-43f7-a7a6-8c8e20066159", "metadata": {}, "outputs": [], @@ -661,6 +661,121 @@ " 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" + ] } ], "metadata": { diff --git a/pretty_read.md b/pretty_read.md index 4a6174d..f212a6a 100644 --- a/pretty_read.md +++ b/pretty_read.md @@ -429,3 +429,68 @@ min([seed['start'] for seed in seeds]) 2254686 + +## Puzzel 6 + +## Deel 1 + + +```python +data = open('data/puzzle_6.txt', 'r').readlines() +times = [int(time) for time in re.findall(r'(\d+)', data[0])] +distances = [int(distance) for distance in re.findall(r'(\d+)', data[1])] +``` + + [358, 1054, 1807, 1080] [46, 68, 98, 66] + + + +```python +total_wins = [] +for time, distance in zip(times, distances): + wins = 0 + for time_pressed in range(0, time+1): + distance_travelled = time_pressed * (time-time_pressed) + if distance_travelled > distance: + wins += 1 + total_wins.append(wins) +score = 1 +for win in total_wins: + score = score*win +score + +``` + + + + + 138915 + + + +## Deel 2 + + +```python +time = [time for time in re.findall(r'(\d+)', data[0])] +time = int(''.join(time)) +distance = [distance for distance in re.findall(r'(\d+)', data[1])] +distance = int(''.join(distance)) + + +discriminant = (time**2) - (4 * distance) +x0 = ((-time) + np.sqrt(discriminant))/-2 +x1 = ((-time) - np.sqrt(discriminant))/-2 +print(x0, x1) +round(x1) - round(x0)-1 +``` + + 9674509.498754364 37015356.50124563 + + + + + + 27340847 + +