Skip to content

Commit

Permalink
feat(solutions/gmacario): day07 Part 1 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 564a6a0 commit 11ad323
Showing 1 changed file with 91 additions and 142 deletions.
233 changes: 91 additions & 142 deletions solutions/gmacario/day07/day07.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -52,49 +52,15 @@
"execution_count": 3,
"id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| input_lines: ['190: 10 19',\n",
" '3267: 81 40 27',\n",
" '83: 17 5',\n",
" '156: 15 6',\n",
" '7290: 6 8 6 15',\n",
" '161011: 16 10 13',\n",
" '192: 17 8 14',\n",
" '21037: 9 7 18 13',\n",
" '292: 11 6 16 20']\n"
]
},
{
"data": {
"text/plain": [
"['190: 10 19',\n",
" '3267: 81 40 27',\n",
" '83: 17 5',\n",
" '156: 15 6',\n",
" '7290: 6 8 6 15',\n",
" '161011: 16 10 13',\n",
" '192: 17 8 14',\n",
" '21037: 9 7 18 13',\n",
" '292: 11 6 16 20']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"# Read the puzzle input into a list of strings, one per line\n",
"#\n",
"with open(\"input_day07_gmacario.txt\", 'r') as file:\n",
" input_lines = [line.rstrip() for line in file]\n",
" input_lines = test.splitlines() # Uncomment for debug\n",
" # input_lines = test.splitlines() # Uncomment for debug\n",
"\n",
"ic(input_lines)"
"# ic(input_lines)"
]
},
{
Expand Down Expand Up @@ -126,112 +92,117 @@
"execution_count": 5,
"id": "d35b2b1a-a493-4c55-8696-a4af4299708a",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| test_equations: [(190, [10, 19]),\n",
" (3267, [81, 40, 27]),\n",
" (83, [17, 5]),\n",
" (156, [15, 6]),\n",
" (7290, [6, 8, 6, 15]),\n",
" (161011, [16, 10, 13]),\n",
" (192, [17, 8, 14]),\n",
" (21037, [9, 7, 18, 13]),\n",
" (292, [11, 6, 16, 20])]\n"
]
},
{
"data": {
"text/plain": [
"[(190, [10, 19]),\n",
" (3267, [81, 40, 27]),\n",
" (83, [17, 5]),\n",
" (156, [15, 6]),\n",
" (7290, [6, 8, 6, 15]),\n",
" (161011, [16, 10, 13]),\n",
" (192, [17, 8, 14]),\n",
" (21037, [9, 7, 18, 13]),\n",
" (292, [11, 6, 16, 20])]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"test_equations = [line_to_test_entry(l) for l in input_lines]\n",
"\n",
"ic(test_equations)"
"# ic(test_equations)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "5cbc3551-7488-4416-9193-e86b85246736",
"metadata": {},
"outputs": [],
"source": [
"# def find_operators(result:int, operands: tuple) -> tuple:\n",
"# \"\"\"\n",
"# Find the operands which applied to operands produce the result\n",
"# Return a list with the operators, or None if no solution is found\n",
"# \"\"\"\n",
"# operators = list()\n",
"\n",
"# total = operands[0]\n",
"# # totals = [operands[0]]\n",
"\n",
"# for o in operands[1:]:\n",
"# for op in ['+', '*']:\n",
"# if op == '+':\n",
"# total += o\n",
"# else:\n",
"# total *= o\n",
"# # ic(total, result, o, op)\n",
"# if total == result:\n",
"# operators.append(op)\n",
"# break\n",
" \n",
"# # ic(result, operands, operators)\n",
"# return tuple(operators)\n",
"\n",
"# # ic(find_operators(test_equations[0][0], test_equations[0][1])) # 190\n",
"# ic(find_operators(test_equations[1][0], test_equations[1][1])) # 3267"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "03896837-6eb5-4003-a940-bd637c273656",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| total: 121, result: 3267, o: 40, op: '+'\n",
"ic| total: 4840, result: 3267, o: 40, op: '*'\n",
"ic| total: 4867, result: 3267, o: 27, op: '+'\n",
"ic| total: 131409, result: 3267, o: 27, op: '*'\n",
"ic| result: 3267, operands: [81, 40, 27], operators: []\n",
"ic| find_operators(test_equations[1][0], test_equations[1][1]): ()\n"
"ic| solve_equation(test_equations[1][0], test_equations[1][1]): None\n"
]
},
{
"data": {
"text/plain": [
"()"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def find_operators(result:int, operands: tuple) -> tuple:\n",
" \"\"\"\n",
" Find the operands which applied to operands produce the result\n",
" Return a list with the operators, or None if no solution is found\n",
" \"\"\"\n",
" operators = list()\n",
"\n",
" total = operands[0]\n",
" # totals = [operands[0]]\n",
"\n",
" for o in operands[1:]:\n",
" for op in ['+', '*']:\n",
" if op == '+':\n",
" total += o\n",
"def solve_equation(result: int, operands: tuple) -> tuple:\n",
" # ic(\"solve_equation\", result, operands)\n",
" num_operators = len(operands) - 1\n",
" possible_operators = list()\n",
" for k in range(2 ** num_operators):\n",
" seq = bin(k)[2:].zfill(num_operators)\n",
" seq2 = list(seq.replace(\"0\", \"+\").replace(\"1\", \"*\"))\n",
" # ic(result, operands, seq, seq2)\n",
" # ic(\"Trying\", seq2, \"with\", operands, \"to get\", result)\n",
" total = operands[0]\n",
" for operand, operator in zip(operands[1:], seq2):\n",
" # ic(total, operand, operator)\n",
" if operator == '+':\n",
" total += operand\n",
" else:\n",
" total *= o\n",
" ic(total, result, o, op)\n",
" if total == result:\n",
" operators.append(op)\n",
" break\n",
" \n",
" ic(result, operands, operators)\n",
" return tuple(operators)\n",
" total *= operand\n",
" if total > result:\n",
" # ic(\"Got a\", total, \"bigger than\", result, \"--> continue\")\n",
" continue\n",
" # ic(\"After loop:\", seq2, total, result)\n",
" if total == result:\n",
" # ic(\"FOUND\", seq2, total, result)\n",
" return tuple(seq2)\n",
" \n",
" # ic(\"WARNING: No results found\", result, total)\n",
" return None\n",
"\n",
"# ic(find_operators(test_equations[0][0], test_equations[0][1])) # 190\n",
"ic(find_operators(test_equations[1][0], test_equations[1][1])) # 3267"
"ic(solve_equation(test_equations[1][0], test_equations[1][1])) # 3267"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03896837-6eb5-4003-a940-bd637c273656",
"execution_count": 8,
"id": "64b57e8b-70a5-4dc3-b738-25239bcf28f4",
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Day 07 Part 1: RESULT: 932137732557\n"
]
}
],
"source": [
"def solve_part1(test_equations):\n",
" result = 0\n",
" for eq in test_equations:\n",
" if solve_equation(eq[0], eq[1]) != None:\n",
" result += eq[0]\n",
" return result\n",
"\n",
"print(\"Day 07 Part 1: RESULT:\", solve_part1(test_equations))"
]
},
{
"cell_type": "code",
Expand All @@ -241,14 +212,6 @@
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ea0b61ee-501f-4893-ad9a-e93fdb317e33",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -307,25 +270,11 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": null,
"id": "8a366015-d692-4bf7-aa35-1f250d022cb2",
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'solve_part1' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDay 07 Part 1: RESULT:\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[43msolve_part1\u001b[49m(lab_map))\n",
"\u001b[0;31mNameError\u001b[0m: name 'solve_part1' is not defined"
]
}
],
"source": [
"print(\"Day 07 Part 1: RESULT:\", solve_part1(lab_map))"
]
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
Expand Down

0 comments on commit 11ad323

Please sign in to comment.