Skip to content

Commit 256dacf

Browse files
authored
Merge pull request #19 from B-AROL-O/gmacario/fix-alv67-2024
feat: add @alv67 aoc-2024 solutions (with Super-Linter fix)
2 parents a248fe9 + 6a94db6 commit 256dacf

43 files changed

Lines changed: 11910 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/linter.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
VALIDATE_GITHUB_ACTIONS_ZIZMOR: false
3838
VALIDATE_GITLEAKS: false
3939
VALIDATE_JSON: false
40+
VALIDATE_JUPYTER_NBQA_BLACK: false
4041
VALIDATE_JUPYTER_NBQA_FLAKE8: false
4142
VALIDATE_JUPYTER_NBQA_ISORT: false
4243
VALIDATE_JUPYTER_NBQA_MYPY: false
@@ -45,6 +46,7 @@ jobs:
4546
VALIDATE_PYTHON_BLACK: false
4647
VALIDATE_PYTHON_FLAKE8: false
4748
VALIDATE_PYTHON_ISORT: false
49+
VALIDATE_PYTHON_MYPY: false
4850
VALIDATE_PYTHON_PYINK: false
4951
VALIDATE_PYTHON_PYLINT: false
5052
VALIDATE_PYTHON_RUFF_FORMAT: false

2024/alv67/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 2024/alv67
2+
3+
Solutions to [Advent of Code 2024](https://adventofcode.com/2024) by [Alessandro Varesi](https://github.com/alv67)
4+
5+
<!-- EOF -->

2024/alv67/day06/day06.ipynb

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# -- DAY 6 --\n"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 1,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"The solution of Puzzle 1 is: 4982\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"# --- Part One ---\n",
25+
"\n",
26+
"import io\n",
27+
"from icecream import ic\n",
28+
"\n",
29+
"test = \"\"\"....#.....\n",
30+
".........#\n",
31+
"..........\n",
32+
"..#.......\n",
33+
".......#..\n",
34+
"..........\n",
35+
".#..^.....\n",
36+
"........#.\n",
37+
"#.........\n",
38+
"......#...\"\"\"\n",
39+
"\n",
40+
"retvalue = 0\n",
41+
"\n",
42+
"DIREZIONE = [\"up\", \"right\", \"down\", \"left\"]\n",
43+
"MOVEMENT = { \"up\": (0, -1), \"right\": (1, 0), \"down\": (0, 1), \"left\": (-1, 0) }\n",
44+
"\n",
45+
"class Map:\n",
46+
" \"\"\"\n",
47+
" La mappa consiste di una griglia di caratteri, la quale viene importata dal file o da una stringa.\n",
48+
" Le coordinate x, y rappresentano rispettivamente una posizione orizzontale e verticale.\n",
49+
" Contiene degli ostacoli (\"#\") e una posizione di partenza (\"^\")\n",
50+
" La classe è anche rappresentata una guardia che si muove nella griglia. \n",
51+
" La sua posizione è aggiornata ogni volta che il guardia si muove. Viene anche mantenuta la sua direzione.\n",
52+
" La posizione None indica non è presente nella griglia.\n",
53+
" \"\"\"\n",
54+
" def __init__(self, grid):\n",
55+
" self.guard = {\"pos\": None, \"dir\": 0}\n",
56+
" self.visited = set()\n",
57+
" self.grid = self.import_grid(grid)\n",
58+
" self.map_size = len(self.grid[0]), len(self.grid)\n",
59+
" self.obstacles, self.guard[\"pos\"] = self._map_grid(self.grid)\n",
60+
" if self.guard[\"pos\"]:\n",
61+
" self.visited.add(self.guard[\"pos\"])\n",
62+
"\n",
63+
" #print (self.guard)\n",
64+
"\n",
65+
"\n",
66+
" def import_grid(self,grid):\n",
67+
" if isinstance(grid, io.TextIOBase):\n",
68+
" #print (\"hai passato un file\")\n",
69+
" return [list(x.strip()) for x in grid]\n",
70+
" elif isinstance(grid, str):\n",
71+
" # print (\"hai passato una stringa\")\n",
72+
" return [list(x) for x in grid.splitlines()]\n",
73+
" elif isinstance(grid, list):\n",
74+
" # print (\"hai passato una lista\")\n",
75+
" return grid\n",
76+
" else:\n",
77+
" print (\"grid must be a file, a string or a list\")\n",
78+
" \n",
79+
" def _map_grid(self, grid):\n",
80+
" obstacles = {(x, y): grid[y][x] for y in range(len(grid)) for x in range(len(grid[0])) if grid[y][x] == \"#\"}\n",
81+
" position = [(x, y) for y in range(len(grid)) for x in range(len(grid[0])) if grid[y][x] == \"^\"]\n",
82+
" if len(position) != 1:\n",
83+
" raise Exception(\"there must be only one position\")\n",
84+
" return obstacles, position[0]\n",
85+
" \n",
86+
" def guard_step(self) -> bool:\n",
87+
" \"\"\"\n",
88+
" move the guard straight in the direction he is facing\n",
89+
" \"\"\"\n",
90+
" pos = self.guard[\"pos\"]\n",
91+
" move = MOVEMENT[DIREZIONE[self.guard[\"dir\"]]]\n",
92+
" new_pos = pos[0] + move[0], pos[1] + move[1]\n",
93+
" if new_pos in self.obstacles:\n",
94+
" # DEBUG ic(new_pos,stp,self.guard[\"pos\"])\n",
95+
" return False\n",
96+
" if new_pos[0] < 0 or new_pos[1] < 0 or new_pos[0] >= self.map_size[0] or new_pos[1] >= self.map_size[1]:\n",
97+
" # print(\"out of map\")\n",
98+
" self.guard[\"pos\"] = None\n",
99+
" return False\n",
100+
" self.guard[\"pos\"] = new_pos\n",
101+
" return True\n",
102+
"\n",
103+
" # DEBUG print (self.guard)\n",
104+
"\n",
105+
" def guard_move(self, steps = None) -> int:\n",
106+
" \"\"\" \n",
107+
" move the guard straight in the direction he is facing\n",
108+
" stops wheb he reaches an obstacle\n",
109+
" steps is the number of steps to move, None means infinite\n",
110+
" return number of steps\n",
111+
" \"\"\"\n",
112+
" # DEBUG ic(\"gard move\", self.guard[\"dir\"])\n",
113+
" stp = 0 # steps done \n",
114+
" while steps is None or stp < steps:\n",
115+
" if not self.guard_step():\n",
116+
" return stp\n",
117+
" self.visited.add(self.guard[\"pos\"])\n",
118+
" stp += 1\n",
119+
"\n",
120+
" return stp\n",
121+
"\n",
122+
" def guard_rotate(self, dir = 1):\n",
123+
" \"\"\"\n",
124+
" rotate right if dir = 1 (default), left if dir = -1\n",
125+
" return the new direction\n",
126+
" \"\"\"\n",
127+
" if dir == 1 or dir == -1:\n",
128+
" self.guard[\"dir\"] = (self.guard[\"dir\"] + dir) % 4\n",
129+
" return self.guard[\"dir\"]\n",
130+
"\n",
131+
"with open(\"input.txt\",\"r\") as file:\n",
132+
"\n",
133+
" room = Map(file)\n",
134+
"\n",
135+
" retvalue = 1\n",
136+
"\n",
137+
" while room.guard[\"pos\"]:\n",
138+
" # for i in range(11):\n",
139+
" steps = room.guard_move()\n",
140+
" retvalue = retvalue + steps\n",
141+
" room.guard_rotate(1)\n",
142+
" \n",
143+
" retvalue = len(room.visited)\n",
144+
" # room.guard_rotate(1)\n",
145+
"\n",
146+
"\n",
147+
"print (f\"The solution of Puzzle 1 is: {retvalue}\")\n"
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"metadata": {},
154+
"outputs": [
155+
{
156+
"name": "stdout",
157+
"output_type": "stream",
158+
"text": [
159+
"{((89, 84), 0)}\n",
160+
"The solution of Puzzle 2 is: 1663\n"
161+
]
162+
}
163+
],
164+
"source": [
165+
"# --- Part Two ---\n",
166+
"\n",
167+
"# Use of object defined in part 1\n",
168+
"\n",
169+
"class Guard(Map):\n",
170+
" \"\"\"\n",
171+
" Inheritance of the class Map\n",
172+
" Now visited keep track also of the direction\n",
173+
" \"\"\"\n",
174+
"\n",
175+
" def __init__(self, grid):\n",
176+
" super().__init__(grid)\n",
177+
" self.visited = set()\n",
178+
" self.initial_guard = self.guard.copy()\n",
179+
" self.obstacles_origin = self.obstacles.copy()\n",
180+
" self.visited.add((self.guard[\"pos\"], self.guard[\"dir\"]))\n",
181+
" #print (self.guard)\n",
182+
"\n",
183+
" def add_obstable(self,x, y , c = \"O\"):\n",
184+
" self.obstacles[(x, y)] = c\n",
185+
"\n",
186+
" def restart(self):\n",
187+
" self.guard = self.initial_guard.copy()\n",
188+
" self.obstacles = self.obstacles_origin.copy()\n",
189+
" self.visited = set()\n",
190+
" if self.guard[\"pos\"]:\n",
191+
" self.visited.add(self.guard[\"pos\"])\n",
192+
"\n",
193+
" def guard_move(self, steps = None) -> int:\n",
194+
" \"\"\" \n",
195+
" move the guard straight in the direction he is facing\n",
196+
" stops wheb he reaches an obstacle\n",
197+
" steps is the number of steps to move, None means infinite\n",
198+
" return number of steps (-1 if a loop is intercempted)\n",
199+
" \"\"\"\n",
200+
" # DEBUG ic(\"gard move\", self.guard[\"dir\"])\n",
201+
" stp = 0 # steps done \n",
202+
" while steps is None or stp < steps:\n",
203+
" if not self.guard_step():\n",
204+
" return stp\n",
205+
" if (self.guard[\"pos\"], self.guard[\"dir\"]) in self.visited:\n",
206+
" # print (\"Found Loop\")\n",
207+
" return -1\n",
208+
" self.visited.add((self.guard[\"pos\"], self.guard[\"dir\"]))\n",
209+
" stp += 1\n",
210+
"\n",
211+
" return stp\n",
212+
"\n",
213+
" #print (self.guard\n",
214+
"\n",
215+
"with open(\"input.txt\",\"r\") as file:\n",
216+
"\n",
217+
" room = Guard(file)\n",
218+
"\n",
219+
" print (room.visited)\n",
220+
"\n",
221+
" retvalue = 0\n",
222+
"\n",
223+
" for y in range(room.map_size[1]):\n",
224+
" for x in range(room.map_size[0]):\n",
225+
" if (x, y) == room.guard[\"pos\"]:\n",
226+
" # print (f\"{x,y} posizione della guardia\")\n",
227+
" continue\n",
228+
" if (x, y) in room.obstacles:\n",
229+
" # print (f\"({x,y}) posizione di un ostacolo\")\n",
230+
" continue\n",
231+
" room.add_obstable(x, y) \n",
232+
" while room.guard[\"pos\"]:\n",
233+
" #for i in range(11):\n",
234+
" steps = room.guard_move()\n",
235+
" if steps == -1:\n",
236+
" retvalue += 1\n",
237+
" #print(f\"LOOP per ostacolo in ({x,y})\")\n",
238+
" break\n",
239+
" room.guard_rotate(1)\n",
240+
" else:\n",
241+
" # print(f\"no loop per ostacolo in ({x,y})\")\n",
242+
" pass\n",
243+
" room.restart()\n",
244+
" \n",
245+
"\n",
246+
" # retvalue = len(room.visited)\n",
247+
"\n",
248+
"print (f\"The solution of Puzzle 2 is: {retvalue}\")\n"
249+
]
250+
}
251+
],
252+
"metadata": {
253+
"kernelspec": {
254+
"display_name": "Python 3",
255+
"language": "python",
256+
"name": "python3"
257+
},
258+
"language_info": {
259+
"codemirror_mode": {
260+
"name": "ipython",
261+
"version": 3
262+
},
263+
"file_extension": ".py",
264+
"mimetype": "text/x-python",
265+
"name": "python",
266+
"nbconvert_exporter": "python",
267+
"pygments_lexer": "ipython3",
268+
"version": "3.12.1"
269+
}
270+
},
271+
"nbformat": 4,
272+
"nbformat_minor": 2
273+
}

0 commit comments

Comments
 (0)