generated from arol-polito/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip(solutions/gmacario: begin work on day07
Signed-off-by: Gianpaolo Macario <[email protected]>
- Loading branch information
Showing
2 changed files
with
1,210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,360 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "e8c36490-ac44-4e4f-9fc3-97d15fbef673", | ||
"metadata": {}, | ||
"source": [ | ||
"# AoC 2024 - Day 7\n", | ||
"\n", | ||
"<https://adventofcode.com/2024/day/7>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from icecream import ic" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f3458754-aa32-44e5-b229-4f519108459e", | ||
"metadata": {}, | ||
"source": [ | ||
"## Part 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "b8ac6332-ebd1-424b-a781-df1771e81be8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"test = \"\"\"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", | ||
"\"\"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"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" | ||
} | ||
], | ||
"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", | ||
"\n", | ||
"ic(input_lines)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "205f3d2b-403d-47cb-8270-145a341b1ecc", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def line_to_test_entry(line: str) -> tuple():\n", | ||
" \"\"\"\n", | ||
" Convert one line of the puzzle input into a tuple()\n", | ||
" - key: the result of the calibration test\n", | ||
" - operands: a list of the operands in the equation\n", | ||
" \"\"\"\n", | ||
" key = int(line.split(\":\")[0])\n", | ||
" operands = list(map(int, list(line.split(\" \")[1:])))\n", | ||
" # ic(key, operands)\n", | ||
" result = (key, operands)\n", | ||
" # ic(line, result)\n", | ||
" return result\n", | ||
"\n", | ||
"# Test\n", | ||
"# ic(line_to_test_entry(input_lines[0]))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"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" | ||
} | ||
], | ||
"source": [ | ||
"test_equations = [line_to_test_entry(l) for l in input_lines]\n", | ||
"\n", | ||
"ic(test_equations)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "5cbc3551-7488-4416-9193-e86b85246736", | ||
"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" | ||
] | ||
}, | ||
{ | ||
"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", | ||
" 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": null, | ||
"id": "03896837-6eb5-4003-a940-bd637c273656", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "51de10dc-4093-4fce-bd21-50ef40cdf1dd", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "ea0b61ee-501f-4893-ad9a-e93fdb317e33", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "00be4c47-79f6-4c21-9859-9fc329f871cb", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "24c136ce-fe8d-4274-8ed9-4423fa9f52d3", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a57a063b-f310-459e-9c2e-bb921a3d3cf7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "13d150e0-de45-4fc8-b419-195130cee355", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a7d3a2de-90fd-428f-9902-3ee5a5eb109b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b47c8d88-92df-4c5d-8063-b94550af37c2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b11850d1-2907-4176-8098-b26bf45a83a9", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"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))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", | ||
"metadata": {}, | ||
"source": [ | ||
"## Part 2" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.10" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
Oops, something went wrong.