Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
Advent of Code 2024
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Regler
Advent of Code 2024
Commits
0078bf02
Commit
0078bf02
authored
7 months ago
by
BoB
Browse files
Options
Downloads
Patches
Plain Diff
beating max :-0
parent
a3c1c04d
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
day 13/day_13_bob.ipynb
+50
-0
50 additions, 0 deletions
day 13/day_13_bob.ipynb
with
50 additions
and
0 deletions
day 13/day_13_bob.ipynb
0 → 100644
+
50
−
0
View file @
0078bf02
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"#day13 - cleaned up code:\n",
"import re\n",
"def readfile(filename):\n",
" with open(filename, 'r') as file:\n",
" return [\n",
" [int(num) for num in re.findall(r'[\\+=](\\d+)', line)]\n",
" for line in file if line.strip()\n",
" ]\n",
"numbers = readfile('input13.txt')\n",
"A, B, Prize = numbers[::3], numbers[1::3], numbers[2::3]\n",
"\n",
"import numpy as np\n",
"extradist = 10000000000000 # part1: extradist = 0\n",
"totalcost = 0\n",
"for i in range(len(A)):\n",
" M = np.array([A[i], B[i]]).T # Transpose to get correct 2x2 matrix\n",
" p = np.array(Prize[i])[:, None] + extradist\n",
" x = np.linalg.solve(M, p)\n",
" x_round = np.rint(x).astype(int)\n",
" if np.allclose(M @ x_round - p, np.zeros_like(p)):\n",
" totalcost += int(3*x_round[0]+x_round[1])\n",
"print(f\"total cost = {totalcost}\")"
],
"metadata": {
"id": "fzAQHgYHH6xZ"
},
"execution_count": null,
"outputs": []
}
]
}
\ No newline at end of file
%% Cell type:code id: tags:
```
#day13 - cleaned up code:
import re
def readfile(filename):
with open(filename, 'r') as file:
return [
[int(num) for num in re.findall(r'[\+=](\d+)', line)]
for line in file if line.strip()
]
numbers = readfile('input13.txt')
A, B, Prize = numbers[::3], numbers[1::3], numbers[2::3]
import numpy as np
extradist = 10000000000000 # part1: extradist = 0
totalcost = 0
for i in range(len(A)):
M = np.array([A[i], B[i]]).T # Transpose to get correct 2x2 matrix
p = np.array(Prize[i])[:, None] + extradist
x = np.linalg.solve(M, p)
x_round = np.rint(x).astype(int)
if np.allclose(M @ x_round - p, np.zeros_like(p)):
totalcost += int(3*x_round[0]+x_round[1])
print(f"total cost = {totalcost}")
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment