Skip to content

Commit

Permalink
Comment updates
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Dec 29, 2023
1 parent be86ea6 commit e5e228d
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/AoC_2023/Dazbo's_Advent_of_Code_2023.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1983,6 +1983,7 @@
" \"\"\" Add a range which contains src start, dest start, and the length of the range \"\"\"\n",
" self.src_ranges.append((src_start, range_length)) \n",
" self.dest_ranges.append((dest_start, range_length))\n",
" self._sort_ranges()\n",
" \n",
" def _sort_ranges(self):\n",
" \"\"\" Sort the range into ascending numeric, based on source range start values \"\"\"\n",
Expand All @@ -1994,17 +1995,13 @@
" self.src_ranges = [self.src_ranges[i] for i in index_order]\n",
" self.dest_ranges = [self.dest_ranges[i] for i in index_order]\n",
"\n",
" def finalise(self):\n",
" \"\"\" Sort the range into ascending numeric, based on source range start values \"\"\"\n",
" self._sort_ranges()\n",
" \n",
" def get_target(self, src_val: int):\n",
" \"\"\" Map a source value to a target value \"\"\"\n",
" target = src_val # if our source isn't in a range, then return the same value\n",
" \n",
" for i, curr_range in enumerate(self.src_ranges):\n",
" src_start = curr_range[0]\n",
" src_end = curr_range[0] + curr_range[1] # exclusive end\n",
" src_end = curr_range[0] + curr_range[1] # start+length; exclusive end\n",
" if src_start <= src_val < src_end: # if our source is in a range, then apply the shift\n",
" target = src_val - src_start + self.dest_ranges[i][0]\n",
" break # we've mapped the value, so no more ranges need to be checked\n",
Expand Down Expand Up @@ -2087,7 +2084,6 @@
" dest_start, src_start, interval_len = [int(x) for x in line.split()]\n",
" map.add_range(src_start=src_start, dest_start=dest_start, range_length=interval_len)\n",
" \n",
" map.finalise()\n",
" maps.append(map)\n",
" \n",
" return seeds, maps"
Expand All @@ -2100,16 +2096,16 @@
"outputs": [],
"source": [
"def solve_part1(data):\n",
" seeds, source_maps = parse_data(data)\n",
" seeds, maps = parse_data(data)\n",
" location_map = {}\n",
" \n",
" logger.debug(f\"{seeds=}\")\n",
" for current_map in source_maps:\n",
" for current_map in maps:\n",
" logger.debug(f\"{current_map=}\")\n",
" \n",
" for seed in seeds:\n",
" current_val = seed\n",
" for current_map in source_maps:\n",
" for current_map in maps:\n",
" current_val = current_map.get_target(current_val)\n",
" \n",
" location_map[seed] = current_val\n",
Expand Down Expand Up @@ -2227,15 +2223,15 @@
"outputs": [],
"source": [
"def solve_part2(data):\n",
" seeds, source_maps = parse_data(data)\n",
" seeds, maps = parse_data(data)\n",
" \n",
" # convert seeds to intervals, of format [(start, end), ...] where end is exclusive\n",
" # let's call them intervals, to avoid confusion with the ranges we stored in our GardinerMap\n",
" seed_intervals = [(seeds[i], seeds[i]+seeds[i+1]) for i in range(0, len(seeds), 2)]\n",
" logger.debug(f\"{seed_intervals=}\")\n",
" \n",
" current_intervals = seed_intervals\n",
" for current_map in source_maps:\n",
" for current_map in maps:\n",
" current_intervals = current_map.map_intervals(current_intervals)\n",
" logger.debug(f\"Mapping to {current_map.dest_type}: {current_intervals}\")\n",
" \n",
Expand All @@ -2252,7 +2248,7 @@
"sample_answers = [46]\n",
"\n",
"for curr_input, curr_ans in zip(sample_inputs, sample_answers):\n",
" validate(solve_part2(curr_input.splitlines()), curr_ans) # test with sample data\n",
" validate(solve_part2(curr_input), curr_ans) # test with sample data\n",
"\n",
"soln = solve_part2(input_data)\n",
"logger.info(f\"Part 2 soln={soln}\")"
Expand Down

0 comments on commit e5e228d

Please sign in to comment.