Skip to content

Commit

Permalink
feat(solutions/gmacario): day05 Part 2 solved
Browse files Browse the repository at this point in the history
Signed-off-by: Gianpaolo Macario <[email protected]>
  • Loading branch information
gmacario committed Dec 7, 2024
1 parent 5e31c42 commit 44c2715
Showing 1 changed file with 128 additions and 100 deletions.
228 changes: 128 additions & 100 deletions solutions/gmacario/day05-jupyter/day05.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@
"metadata": {},
"outputs": [],
"source": [
"from icecream import ic"
"from icecream import ic\n",
"import time"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ba8767da-1138-466c-8756-496c662def09",
"metadata": {},
"outputs": [],
"source": [
"# use_test = True # Comment out for using actual puzzle input"
]
},
{
Expand All @@ -30,12 +41,20 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "b8ac6332-ebd1-424b-a781-df1771e81be8",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| 'use_test was undefined - forcing to', use_test: False\n"
]
}
],
"source": [
"test_input = \"\"\"47|53\n",
"test = \"\"\"47|53\n",
"97|13\n",
"97|61\n",
"97|47\n",
Expand Down Expand Up @@ -63,27 +82,37 @@
"75,97,47,61,53\n",
"61,13,29\n",
"97,13,75,29,47\n",
"\"\"\""
"\"\"\"\n",
"\n",
"try:\n",
" use_test\n",
"except NameError:\n",
" use_test = False\n",
" ic(\"use_test was undefined - forcing to\", use_test)\n",
" \n",
"if use_test:\n",
" assert test != None"
]
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b",
"metadata": {},
"outputs": [],
"source": [
"with open(\"input_day05_gmacario.txt\", 'r') as file:\n",
" input_text = file.read()\n",
"\n",
"# input_text = test_input # DEBUG\n",
"if use_test:\n",
" input_text = test\n",
"else:\n",
" with open(\"input_day05_gmacario.txt\", 'r') as file:\n",
" input_text = file.read()\n",
"\n",
"# ic(input_text)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "48622bfa-12c9-43ff-9758-28047f26047e",
"metadata": {},
"outputs": [],
Expand All @@ -107,7 +136,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "93c4e5b7-4d9d-4b82-a530-fd045e2fc6ab",
"metadata": {},
"outputs": [],
Expand All @@ -132,22 +161,21 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "ca2ece58-99f7-4657-a1e8-b164b5982a5e",
"metadata": {},
"outputs": [],
"source": [
"# Sanity Checks with test1\n",
"#\n",
"# assert is_sequence_ordered(page_numbers[0]) == True\n",
"# assert is_sequence_ordered(page_numbers[1]) == True\n",
"# assert is_sequence_ordered(page_numbers[2]) == True\n",
"# assert is_sequence_ordered(page_numbers[3]) == False"
"if use_test:\n",
" assert is_sequence_ordered(page_numbers[0]) == True\n",
" assert is_sequence_ordered(page_numbers[1]) == True\n",
" assert is_sequence_ordered(page_numbers[2]) == True\n",
" assert is_sequence_ordered(page_numbers[3]) == False"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "2f1e2b3c-db37-4ea3-9e24-7cb8cf15dcde",
"metadata": {},
"outputs": [],
Expand All @@ -160,37 +188,45 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "d84ac7eb-0518-41bb-a6a7-e1c62086a71d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| 'Part 1 result', total: 5329\n"
"ic| \"elapsed=\": 'elapsed='\n",
" tm_end - tm_begin: 0.05955862998962402\n",
" \"result=\": 'result='\n",
" part1_result: 5329\n"
]
},
{
"data": {
"text/plain": [
"('Part 1 result', 5329)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"Day 05 Part 1 RESULT:\n",
"5329\n"
]
}
],
"source": [
"total = 0\n",
"for seq in page_numbers:\n",
" # ic(seq)\n",
"\n",
" if is_sequence_ordered(seq):\n",
" total += middle_element(seq) \n",
"def solve_part1():\n",
" total = 0\n",
" for seq in page_numbers:\n",
" # ic(seq)\n",
" \n",
" if is_sequence_ordered(seq):\n",
" total += middle_element(seq)\n",
" return total\n",
"\n",
"ic(\"Part 1 result\", total)"
"tm_begin = time.time()\n",
"part1_result = solve_part1()\n",
"tm_end = time.time()\n",
"ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part1_result)\n",
"print(\"Day 05 Part 1 RESULT:\")\n",
"print(part1_result)"
]
},
{
Expand All @@ -203,47 +239,32 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "3e522b8b-8eae-40bf-844f-12ff20957c00",
"metadata": {},
"outputs": [],
"source": [
"def reorder_sequence(seq):\n",
" # ic(\"BEGIN reorder_sequence\", seq)\n",
"\n",
" # Consider only the page_ordering_rules which contain all element\n",
"\n",
" # ic(len(page_ordering_rules))\n",
" relevant_ordering_rules = [r for r in page_ordering_rules if r[0] in seq and r[1] in seq]\n",
" sorted(relevant_ordering_rules)\n",
" # ic(len(relevant_ordering_rules), relevant_ordering_rules)\n",
" \n",
" result = list(seq)\n",
" # ic(len(page_ordering_rules), len(relevant_ordering_rules), relevant_ordering_rules)\n",
"\n",
" # for rule in sorted(page_ordering_rules):\n",
" # if rule[0] not in result or rule[1] not in result:\n",
" # # ic(\"DEBUG: Skipping(1) rule\", rule)\n",
" # continue\n",
" # pos1 = result.index(rule[0])\n",
" # pos2 = result.index(rule[1])\n",
" # # ic(pos1, pos2)\n",
" # if pos1 > pos2:\n",
" # # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n",
" # result[pos1], result[pos2] = result[pos2], result[pos1]\n",
" # ic(\"DEBUG: After swapping\", pos1, \"and\", pos2, \"->\", result)\n",
" # # ic(\"DEBUG: Relevant\", rule, result)\n",
"\n",
" for rule in relevant_ordering_rules:\n",
" pos1 = result.index(rule[0])\n",
" pos2 = result.index(rule[1])\n",
" # ic(pos1, pos2)\n",
" if pos1 > pos2:\n",
" # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n",
" result[pos1], result[pos2] = result[pos2], result[pos1]\n",
" # print(f\"DEBUG: After swapping positions {pos1:3} and {pos2:3} ->\", result)\n",
" # ic(\"DEBUG: Relevant\", rule, result)\n",
" \n",
" #\n",
" result = list(seq)\n",
" must_reorder = True\n",
" while must_reorder:\n",
" must_reorder = False\n",
" for rule in relevant_ordering_rules:\n",
" pos1 = result.index(rule[0])\n",
" pos2 = result.index(rule[1])\n",
" # ic(pos1, pos2)\n",
" if pos1 > pos2:\n",
" # print(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n",
" a, b = result[pos2], result[pos1]\n",
" result[pos1], result[pos2] = a, b\n",
" # print(f\"DEBUG: After swapping positions {pos1:3} and {pos2:3} ->\", result)\n",
" must_reorder = True\n",
" break\n",
" \n",
" # ic(\"reorder sequence\", seq, \"->\", result)\n",
" # assert is_sequence_ordered(result), ic(seq, result)\n",
Expand All @@ -252,40 +273,38 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "d7393bcb-135a-470e-a37b-4149449ef1d8",
"metadata": {},
"outputs": [],
"source": [
"# Sanity Checks with test1\n",
"#\n",
"# assert is_sequence_ordered(page_numbers[3]) == False\n",
"\n",
"# ic(reorder_sequence(page_numbers[3]))\n",
"# ic(reorder_sequence(page_numbers[4]))\n",
"# ic(reorder_sequence(page_numbers[5]))"
"if use_test:\n",
" assert is_sequence_ordered(page_numbers[3]) == False\n",
" assert reorder_sequence(page_numbers[3]) == [97, 75, 47, 61, 53]\n",
" assert reorder_sequence(page_numbers[4]) == [61, 29, 13]\n",
" assert reorder_sequence(page_numbers[5]) == [97, 75, 47, 29, 13]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"id": "6ed8e5c4-625b-4c4b-be23-5ccbdeaee80e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| reorder_sequence(page_numbers[0]): [67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]\n"
"ic| reorder_sequence(page_numbers[0]): [67, 82, 22, 76, 69, 45, 98, 47, 37, 35, 36, 39, 73]\n"
]
},
{
"data": {
"text/plain": [
"[67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]"
"[67, 82, 22, 76, 69, 45, 98, 47, 37, 35, 36, 39, 73]"
]
},
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -299,39 +318,48 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"id": "9e518d75-370f-495c-9a86-8887562ef15b",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| 'Part 2 result', total: 5896\n"
"ic| \"elapsed=\": 'elapsed='\n",
" tm_end - tm_begin: 0.12361502647399902\n",
" \"result=\": 'result='\n",
" part2_result: 5833\n"
]
},
{
"data": {
"text/plain": [
"('Part 2 result', 5896)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"Day 05 Part 2 RESULT:\n",
"5833\n"
]
}
],
"source": [
"total = 0\n",
"for seq in page_numbers:\n",
" # ic(seq)\n",
"\n",
" if not is_sequence_ordered(seq):\n",
" ordered_seq = reorder_sequence(seq)\n",
" # ic(\"DEBUG: incorrectly-ordered\", seq, \"->\", ordered_seq)\n",
" total += middle_element(ordered_seq)\n",
"def solve_part2():\n",
" total = 0\n",
" for seq in page_numbers:\n",
" # ic(seq)\n",
" \n",
" if not is_sequence_ordered(seq):\n",
" ordered_seq = reorder_sequence(seq)\n",
" # ic(\"DEBUG: incorrectly-ordered\", seq, \"->\", ordered_seq)\n",
" total += middle_element(ordered_seq)\n",
" return total\n",
"\n",
"ic(\"Part 2 result\", total)"
"tm_begin = time.time()\n",
"part2_result = solve_part2()\n",
"tm_end = time.time()\n",
"ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part2_result)\n",
"print(\"Day 05 Part 2 RESULT:\")\n",
"print(part2_result)\n",
"# assert part2_result == 123 # test_data"
]
}
],
Expand Down

0 comments on commit 44c2715

Please sign in to comment.