|
2314 | 2314 | "Distance: 9 40 200\n",
|
2315 | 2315 | "```\n",
|
2316 | 2316 | "\n",
|
2317 |
| - "Our boat starts at 0mm/ms. Charing of 1ms increases speed by 1mm/ms.\n", |
| 2317 | + "Our boat starts at 0mm/ms. Charging of 1ms increases speed by 1mm/ms.\n", |
2318 | 2318 | "\n",
|
2319 | 2319 | "**Determine the number of ways you could beat the record in each race. What do you get if you multiply these numbers together?**\n",
|
2320 | 2320 | "\n",
|
|
2335 | 2335 | "outputs": [],
|
2336 | 2336 | "source": [
|
2337 | 2337 | "def solve_part1(data):\n",
|
2338 |
| - " _, durations_part = data[0].split(\":\")\n", |
2339 |
| - " _, distances_part = data[1].split(\":\")\n", |
2340 |
| - " durations = [int(x) for x in durations_part.split()]\n", |
2341 |
| - " distances = [int(x) for x in distances_part.split()]\n", |
2342 |
| - " \n", |
| 2338 | + " durations = [int(x) for x in data[0].split(\":\")[1].split()]\n", |
| 2339 | + " distances = [int(x) for x in data[1].split(\":\")[1].split()]\n", |
2343 | 2340 | " logger.debug(f\"{durations=}\")\n",
|
2344 | 2341 | " logger.debug(f\"{distances=}\")\n",
|
2345 | 2342 | " \n",
|
2346 | 2343 | " wins = defaultdict(dict) # { race_num: { hold_time: distance, hold_time: distance, ...} }\n",
|
2347 | 2344 | " for i, duration in enumerate(durations): # e.g. 0, 7\n",
|
2348 | 2345 | " for hold_time in range(1, duration): # e.g. 0-7\n",
|
| 2346 | + " # d = (t - h) * h\n", |
2349 | 2347 | " d = (duration - hold_time) * hold_time\n",
|
2350 | 2348 | " if d > distances[i]: # did we win?\n",
|
2351 | 2349 | " wins[i][hold_time] = d\n",
|
|
2385 | 2383 | "source": [
|
2386 | 2384 | "### Day 6 Part 2\n",
|
2387 | 2385 | "\n",
|
2388 |
| - "Uh oh, there's only one race and the spaces should be ignored. E.g.\n", |
| 2386 | + "Uh oh, there's only one race and the spaces should be ignored. So we reinterpret the input like this:\n", |
2389 | 2387 | "\n",
|
2390 | 2388 | "```text\n",
|
2391 | 2389 | "Time: 71530\n",
|
|
2420 | 2418 | "Remember the quadratic formula?\n",
|
2421 | 2419 | "\n",
|
2422 | 2420 | "$$\n",
|
2423 |
| - "x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n", |
| 2421 | + "\\begin{align}\n", |
| 2422 | + "\\text{For: } ah^2 + bh + c &= 0 \\\\\n", |
| 2423 | + "\\notag \\\\\n", |
| 2424 | + "h &= \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}\n", |
| 2425 | + "\\end{align}\n", |
2424 | 2426 | "$$\n",
|
2425 | 2427 | "\n",
|
2426 |
| - "So we can use this to obtain the two roots, i.e. the hold times where where our distance is equal to the record distance. Using the sample data we were given:\n", |
| 2428 | + "So we can use this to obtain the two roots, i.e. the hold times where where our distance is equal to the record distance. We're given distance $d$ and one duration $t$ in our input. (And we know $a$ is `1`.) Using the sample data we were given:\n", |
2427 | 2429 | "\n",
|
2428 | 2430 | "$$\n",
|
2429 | 2431 | "h^2 - 71530h + 940200 = 0 \\\\\n",
|
|
9873 | 9875 | "toc_visible": true
|
9874 | 9876 | },
|
9875 | 9877 | "kernelspec": {
|
9876 |
| - "display_name": "aca_aoc", |
| 9878 | + "display_name": "ana-aoc", |
9877 | 9879 | "language": "python",
|
9878 |
| - "name": "aca_aoc" |
| 9880 | + "name": "python3" |
9879 | 9881 | },
|
9880 | 9882 | "language_info": {
|
9881 | 9883 | "codemirror_mode": {
|
|
0 commit comments