Skip to main content
Homepage
Explore
Search or go to…
/
Register
Sign in
Explore
Primary navigation
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
?
Collapse sidebar
Snippets
Groups
Projects
Show more breadcrumbs
Regler
Advent of Code 2024
Commits
965da887
Commit
965da887
authored
11 months ago
by
BoB
Browse files
Options
Downloads
Patches
Plain Diff
day8
parent
65f1a381
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 8/day_8_bob.ipynb
+83
-0
83 additions, 0 deletions
day 8/day_8_bob.ipynb
with
83 additions
and
0 deletions
day 8/day_8_bob.ipynb
0 → 100644
+
83
−
0
View file @
965da887
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"#08\n",
"def readdata(filename):\n",
" with open(filename, 'r') as file:\n",
" A = [list(line.strip()) for line in file]\n",
" return A, len(A)\n",
"A, n = readdata('input08.txt')\n",
"\n",
"antenna = {}\n",
"for r in range(n):\n",
" for c in range(n):\n",
" if A[r][c] != \".\":\n",
" antenna.setdefault(A[r][c], []).append((r, c))"
],
"metadata": {
"id": "WTC4nT-lVB9L"
},
"execution_count": 1,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# part 1\n",
"B = [[0] * n for _ in range(n)] # position of antinodes\n",
"for pos in antenna.values():\n",
" for r1, c1 in pos:\n",
" for r2, c2 in pos:\n",
" if (r1, c1) == (r2, c2):\n",
" continue\n",
" r_new, c_new = 2*r2-r1, 2*c2-c1 # Calculate antinode position\n",
" if 0 <= r_new < n and 0 <= c_new < n:\n",
" B[r_new][c_new] = 1\n",
"sum(sum(row) for row in B)"
],
"metadata": {
"id": "bQbhP8ajwPk3"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# part 2\n",
"B = [[0] * n for _ in range(n)] # position of antinodes\n",
"for pos in antenna.values():\n",
" for r, c in pos:\n",
" B[r][c] = 1\n",
" for diff_r, diff_c in [(r2 - r, c2 - c) for r2, c2 in pos if (r2, c2) != (r, c)]:\n",
" r_new, c_new = r + diff_r, c + diff_c\n",
" while 0 <= r_new < n and 0 <= c_new < n:\n",
" B[r_new][c_new] = 1\n",
" r_new += diff_r\n",
" c_new += diff_c\n",
"sum(sum(row) for row in B)"
],
"metadata": {
"id": "n41e8HbQgZ7R"
},
"execution_count": null,
"outputs": []
}
]
}
\ No newline at end of file
%% Cell type:code id: tags:
```
#08
def readdata(filename):
with open(filename, 'r') as file:
A = [list(line.strip()) for line in file]
return A, len(A)
A, n = readdata('input08.txt')
antenna = {}
for r in range(n):
for c in range(n):
if A[r][c] != ".":
antenna.setdefault(A[r][c], []).append((r, c))
```
%% Cell type:code id: tags:
```
# part 1
B = [[0] * n for _ in range(n)] # position of antinodes
for pos in antenna.values():
for r1, c1 in pos:
for r2, c2 in pos:
if (r1, c1) == (r2, c2):
continue
r_new, c_new = 2*r2-r1, 2*c2-c1 # Calculate antinode position
if 0 <= r_new < n and 0 <= c_new < n:
B[r_new][c_new] = 1
sum(sum(row) for row in B)
```
%% Cell type:code id: tags:
```
# part 2
B = [[0] * n for _ in range(n)] # position of antinodes
for pos in antenna.values():
for r, c in pos:
B[r][c] = 1
for diff_r, diff_c in [(r2 - r, c2 - c) for r2, c2 in pos if (r2, c2) != (r, c)]:
r_new, c_new = r + diff_r, c + diff_c
while 0 <= r_new < n and 0 <= c_new < n:
B[r_new][c_new] = 1
r_new += diff_r
c_new += diff_c
sum(sum(row) for row in B)
```
...
...
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