|
311 | 311 | " num_cards[game_id + i + 1] += num_cards[game_id]\n",
|
312 | 312 | "sum(num_cards)"
|
313 | 313 | ]
|
| 314 | + }, |
| 315 | + { |
| 316 | + "cell_type": "markdown", |
| 317 | + "metadata": {}, |
| 318 | + "source": [ |
| 319 | + "## Day 5: If You Give A Seed A Fertilizer\n", |
| 320 | + "\n", |
| 321 | + "What is the lowest location number that corresponds to any of the initial seed numbers?" |
| 322 | + ] |
| 323 | + }, |
| 324 | + { |
| 325 | + "cell_type": "code", |
| 326 | + "execution_count": null, |
| 327 | + "metadata": {}, |
| 328 | + "outputs": [], |
| 329 | + "source": [ |
| 330 | + "from collections import namedtuple\n", |
| 331 | + "from numpy import interp\n", |
| 332 | + "\n", |
| 333 | + "lines = open('05_input.txt', 'r').readlines()\n", |
| 334 | + "lines = [line.strip() for line in lines]\n", |
| 335 | + "\n", |
| 336 | + "def parse_seeds(lines):\n", |
| 337 | + " return [int(seed) for seed in lines[0].removeprefix('seeds: ').split()]\n", |
| 338 | + "\n", |
| 339 | + "def parse_range(line):\n", |
| 340 | + " ConversionRange = namedtuple('conversion_range', 'source dest')\n", |
| 341 | + " dest_start, source_start, length = [int(number) for number in line.split()]\n", |
| 342 | + " return ConversionRange((source_start, source_start + length - 1), (dest_start, dest_start + length - 1))\n", |
| 343 | + "\n", |
| 344 | + "def parse_maps(lines):\n", |
| 345 | + " line_id = 3\n", |
| 346 | + " maps = []\n", |
| 347 | + " while line_id < len(lines):\n", |
| 348 | + " ranges = []\n", |
| 349 | + " while line_id < len(lines) and lines[line_id]:\n", |
| 350 | + " ranges.append(parse_range(lines[line_id]))\n", |
| 351 | + " line_id += 1\n", |
| 352 | + " maps.append(ranges)\n", |
| 353 | + " line_id += 2\n", |
| 354 | + " return maps\n", |
| 355 | + "\n", |
| 356 | + "def convert(number_to_convert, ranges):\n", |
| 357 | + " for range in ranges:\n", |
| 358 | + " if number_to_convert >= range.source[0] and number_to_convert <= range.source[1]:\n", |
| 359 | + " return int(interp(number_to_convert, range.source, range.dest))\n", |
| 360 | + " return number_to_convert\n", |
| 361 | + "\n", |
| 362 | + "def get_location(seed, maps):\n", |
| 363 | + " location = seed\n", |
| 364 | + " for map in maps:\n", |
| 365 | + " location = convert(location, map)\n", |
| 366 | + " return location\n", |
| 367 | + "\n", |
| 368 | + "seeds = parse_seeds(lines)\n", |
| 369 | + "maps = parse_maps(lines)\n", |
| 370 | + "locations = [get_location(seed, maps) for seed in seeds]\n", |
| 371 | + "min(locations)" |
| 372 | + ] |
| 373 | + }, |
| 374 | + { |
| 375 | + "cell_type": "markdown", |
| 376 | + "metadata": {}, |
| 377 | + "source": [ |
| 378 | + "What is the lowest location number that corresponds to any of the initial seed numbers?" |
| 379 | + ] |
| 380 | + }, |
| 381 | + { |
| 382 | + "cell_type": "code", |
| 383 | + "execution_count": null, |
| 384 | + "metadata": {}, |
| 385 | + "outputs": [], |
| 386 | + "source": [ |
| 387 | + "# TODO" |
| 388 | + ] |
314 | 389 | }
|
315 | 390 | ],
|
316 | 391 | "metadata": {
|
|
0 commit comments