Skip to content

Commit

Permalink
2024 day 2 done
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed-dash committed Dec 2, 2024
1 parent 3cbe3ff commit d7b879e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 12 deletions.
104 changes: 92 additions & 12 deletions src/AoC_2024/Dazbo's_Advent_of_Code_2024.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@
"metadata": {},
"source": [
"---\n",
"## Day 2: title"
"## Day 2: Red-Nosed Reports"
]
},
{
Expand Down Expand Up @@ -904,7 +904,31 @@
"source": [
"### Day 2 Part 1\n",
"\n",
"Overview..."
"Our data is a set of reports. Each line is one report. Each report contains a list of numbers called levels. Like this:\n",
"\n",
"```text\n",
"7 6 4 2 1\n",
"1 2 7 8 9\n",
"9 7 6 2 1\n",
"1 3 2 4 5\n",
"8 6 4 4 1\n",
"1 3 6 7 9\n",
"```\n",
"\n",
"Reports are safe if:\n",
"\n",
"- The levels are either all increasing or all decreasing\n",
"- And any two adjacent levels must differ by at least 1 and at most 3.\n",
"\n",
"**How many reports are safe?**\n",
"\n",
"#### Solution Approach\n",
"\n",
"- Split each record into a list of int values, called `levels`.\n",
"- Create `is_safe()` function which:\n",
" - Creates a list of difference values - called `diffs` - by subtracting `levels` with offset 1 from `levels`.\n",
" - Then, use the `all` iterator operator function to check if our condition is true for every diff in `diffs`.\n",
" - The condition is: that every diff is positive and `<=MAX_DIFF` or every diff is negative and `>=MAX_DIFF`."
]
},
{
Expand All @@ -913,8 +937,27 @@
"metadata": {},
"outputs": [],
"source": [
"def is_safe(levels: list[int], max_diff: int) -> bool:\n",
" \"\"\" Determine if a record - i.e. a list of levels - is safe.\n",
" Do this by determining the differences between each level in the record.\n",
" Check if the differences are all positive and within the max, \n",
" or all negative and within the max. \"\"\"\n",
" \n",
" # Create pairs of levels from successive levels\n",
" # For each pair, substract second from first to get the difference\n",
" diffs = [first-second for first, second in zip(levels, levels[1:])]\n",
" \n",
" return (all(0 < diff <= max_diff for diff in diffs) or \n",
" all(-max_diff <= diff < 0 for diff in diffs))\n",
"\n",
"def solve_part1(data):\n",
" pass"
" safe_count = 0\n",
" for report in data:\n",
" levels = list(map(int, report.split()))\n",
" if is_safe(levels, 3):\n",
" safe_count += 1\n",
" \n",
" return safe_count"
]
},
{
Expand All @@ -925,11 +968,17 @@
"source": [
"%%time\n",
"sample_inputs = []\n",
"sample_inputs.append(\"\"\"abcdef\"\"\")\n",
"sample_answers = [\"uvwxyz\"]\n",
"sample_inputs.append(\"\"\"\\\n",
"7 6 4 2 1\n",
"1 2 7 8 9\n",
"9 7 6 2 1\n",
"1 3 2 4 5\n",
"8 6 4 4 1\n",
"1 3 6 7 9\"\"\")\n",
"sample_answers = [2]\n",
"\n",
"for curr_input, curr_ans in zip(sample_inputs, sample_answers):\n",
" validate(solve_part1(curr_input), curr_ans) # test with sample data\n",
" validate(solve_part1(curr_input.splitlines()), curr_ans) # test with sample data\n",
" logger.info(\"Test passed\")\n",
"\n",
"logger.info(\"All tests passed!\")\n",
Expand All @@ -944,7 +993,20 @@
"source": [
"### Day 2 Part 2\n",
"\n",
"Overview..."
"We now have a _Problem Dampener_, which means we can tolerate a single bad level in any given record.\n",
"\n",
"So we must try removing levels from records, and then testing if the report is safe.\n",
"\n",
"**How many reports are now safe?**\n",
"\n",
"#### Solution Approach\n",
"\n",
"Same as before, but this time we will iterate through the index values of each record, and remove that level from the record. I.e.\n",
"\n",
"- Loop through each index value for the record.\n",
"- For the current index value, concatenate the levels up to this index with the levels after this index. Thus, we always end up with a list that is one shorter than the original list. (Note that trimming off the first or last level will never make a safe record _unsafe_.)\n",
"- Pass shortened list to our `is_safe()` function.\n",
"- If the shortened record is safe, then we can add this record to our counter and move on to the next record."
]
},
{
Expand All @@ -954,7 +1016,19 @@
"outputs": [],
"source": [
"def solve_part2(data):\n",
" pass"
" safe_count = 0\n",
" for report in data:\n",
" levels = list(map(int, report.split()))\n",
"\n",
" # Slice out one level at a time, and check if the new record is safe\n",
" for idx in range(len(levels)):\n",
" # Take levels up this idx, and concatenate with levels AFTER this idx\n",
" trimmed_levels = levels[:idx] + levels[idx+1:]\n",
" if is_safe(trimmed_levels, 3):\n",
" safe_count += 1\n",
" break\n",
" \n",
" return safe_count"
]
},
{
Expand All @@ -965,12 +1039,18 @@
"source": [
"%%time\n",
"sample_inputs = []\n",
"sample_inputs.append(\"\"\"abcdef\"\"\")\n",
"sample_answers = [\"uvwxyz\"]\n",
"sample_inputs.append(\"\"\"\\\n",
"7 6 4 2 1\n",
"1 2 7 8 9\n",
"9 7 6 2 1\n",
"1 3 2 4 5\n",
"8 6 4 4 1\n",
"1 3 6 7 9\"\"\")\n",
"sample_answers = [4]\n",
"\n",
"for curr_input, curr_ans in zip(sample_inputs, sample_answers):\n",
" validate(solve_part2(curr_input), curr_ans) # test with sample data\n",
" logger.info(\"Test passed\") \n",
" validate(solve_part2(curr_input.splitlines()), curr_ans) # test with sample data\n",
" logger.info(\"Test passed\")\n",
"\n",
"logger.info(\"Tests passed!\")\n",
"\n",
Expand Down
Binary file added src/AoC_2024/d02/input/input.txt
Binary file not shown.

0 comments on commit d7b879e

Please sign in to comment.