Skip to content

Commit

Permalink
🚧 solved day 7 both parts
Browse files Browse the repository at this point in the history
  • Loading branch information
kzkedzierska committed Dec 9, 2024
1 parent 237a8cb commit 0189fe8
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 11 deletions.
116 changes: 105 additions & 11 deletions 2024/day_07/notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand All @@ -40,7 +40,7 @@
"3749"
]
},
"execution_count": 29,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -59,7 +59,7 @@
" new_running_res = running_res + equat[0]\n",
" else:\n",
" new_running_res = running_res * equat[0]\n",
" \n",
"\n",
" if new_running_res > res:\n",
" return 0\n",
" elif len(equat) == 1 and new_running_res == res:\n",
Expand All @@ -79,10 +79,12 @@
" result = int(equat[0])\n",
" el = [int(i) for i in equat[1].split(\" \")]\n",
"\n",
" \n",
" if (calc(result, el[0], el[1:], sign=\"+\") + calc(result, el[0], el[1:], sign=\"*\")) > 0:\n",
" correct_equations+=result\n",
" \n",
" if (\n",
" calc(result, el[0], el[1:], sign=\"+\")\n",
" + calc(result, el[0], el[1:], sign=\"*\")\n",
" ) > 0:\n",
" correct_equations += result\n",
"\n",
" return correct_equations\n",
"\n",
"\n",
Expand All @@ -91,15 +93,15 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2 μs, sys: 0 ns, total: 2 μs\n",
"Wall time: 3.81 μs\n"
"CPU times: user 3 μs, sys: 1 μs, total: 4 μs\n",
"Wall time: 10 μs\n"
]
},
{
Expand All @@ -108,7 +110,7 @@
"2654749936343"
]
},
"execution_count": 30,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -118,6 +120,98 @@
"\n",
"part_one(\"../inputs/day_07.txt\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11387"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def part_two(input):\n",
" if Path(input).exists():\n",
" with open(input, \"r\") as f:\n",
" input = f.read()\n",
"\n",
" def calc(res, running_res, equat, sign=\"+\"):\n",
" if sign == \"+\":\n",
" new_running_res = running_res + equat[0]\n",
" elif sign == \"*\":\n",
" new_running_res = running_res * equat[0]\n",
" else: # sign = \"||\"\n",
" new_running_res = int(str(running_res) + str(equat[0]))\n",
"\n",
" if new_running_res > res:\n",
" return 0\n",
" elif len(equat) == 1 and new_running_res == res:\n",
" return 1\n",
" elif len(equat) == 1 and new_running_res != res:\n",
" return 0\n",
" else:\n",
" return (\n",
" calc(res, new_running_res, equat[1:], sign=\"+\")\n",
" + calc(res, new_running_res, equat[1:], sign=\"*\")\n",
" + calc(res, new_running_res, equat[1:], sign=\"||\")\n",
" )\n",
"\n",
" correct_equations = 0\n",
" for line in input.splitlines():\n",
" line = line.strip()\n",
"\n",
" equat = line.split(\": \")\n",
" result = int(equat[0])\n",
" el = [int(i) for i in equat[1].split(\" \")]\n",
"\n",
" if (\n",
" calc(result, el[0], el[1:], sign=\"+\")\n",
" + calc(result, el[0], el[1:], sign=\"*\")\n",
" + calc(result, el[0], el[1:], sign=\"||\")\n",
" ) > 0:\n",
" correct_equations += result\n",
"\n",
" return correct_equations\n",
"\n",
"\n",
"part_two(\"../inputs/example_day_07.txt\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.19 s, sys: 9.25 ms, total: 2.2 s\n",
"Wall time: 2.26 s\n"
]
},
{
"data": {
"text/plain": [
"124060392153684"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%time part_two(\"../inputs/day_07.txt\")"
]
}
],
"metadata": {
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ For an overview of my journey and insights, check out my [blog post on AoC 2024]
|3 | [📄 HTML](http://kasia.codes/resources/aoc/2024/day_03) · [📓 Notebook](2024/day_03/notebook.ipynb) · [💻 Bash](2024/day_01/solution.sh) |||
|4 | [📄 HTML](http://kasia.codes/resources/aoc/2024/day_04) · [📓 Notebook](2024/day_04/notebook.ipynb) |||
|5 | [📄 HTML](http://kasia.codes/resources/aoc/2024/day_05) · [📓 Notebook](2024/day_05/notebook.ipynb) |||
|6 | 📄 HTML · [📓 Notebook [WIP]](2024/day_06/notebook.ipynb) || |
|7 | 📄 HTML · [📓 Notebook [WIP]](2024/day_07/notebook.ipynb) |||

<details>
<summary> <h2 id="2023"> 2023 </h2> </summary>
Expand Down

0 comments on commit 0189fe8

Please sign in to comment.