Skip to content

Commit 8ed8303

Browse files
committed
refactor n move
1 parent 74ca35c commit 8ed8303

14 files changed

+145
-26
lines changed

.idea/Advent-of-Code.iml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

2024/Days/07.py AoC_2024/Days/07.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pathlib import Path
1010
from typing import List, Tuple
1111

12-
INPUT = """
12+
DEMO_INPUT = """
1313
190: 10 19
1414
3267: 81 40 27
1515
83: 17 5
@@ -21,9 +21,6 @@
2121
292: 11 6 16 20
2222
"""
2323

24-
# TODO: Update filename `input` and uncomment once ready!
25-
# INPUT = open(Path(__file__).parent / "input").read()
26-
2724

2825
def remap(sections: str) -> list[tuple[int, list[int]]]:
2926
"""
@@ -112,10 +109,28 @@ def calibration_result_with_concat_operator(equations: list[tuple[int, list[int]
112109
return sum_of_test_values
113110

114111

115-
if __name__ == '__main__':
112+
def solve(input_file):
113+
114+
if input_file:
115+
demo = False
116+
print(f"Solving with input file {input_file}")
117+
try:
118+
INPUT = open(input_file).read()
119+
except FileNotFoundError:
120+
print('File not found, using default ...')
121+
INPUT = open(Path(__file__).parent.parent / 'Inputs' / '07').read()
122+
else:
123+
demo = True
124+
INPUT = DEMO_INPUT
125+
126+
print("Solving Day 7 Problem! 🎄")
116127

117128
_equations = remap(INPUT)
118129
# Part 1
119130
print('Part 1: ', calibration_result_of_true_equations(_equations))
120131
# Part 2
121132
print('Part 2: ', calibration_result_with_concat_operator(_equations))
133+
134+
135+
if __name__ == '__main__':
136+
solve()

2024/Days/14.py AoC_2024/Days/14.py

+27-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from tqdm import tqdm
1414

15-
from .utils import Grid, Point
15+
from AoC_2024.utils import Grid, Point
1616

1717

1818
DEMO_INPUT = """
@@ -44,15 +44,16 @@ def remap(lines: str) -> list[tuple[int, int], tuple[int, int]]:
4444
for pv in robot_pos_and_velocities]
4545

4646

47-
def part_1(width=101, height=103, default=0):
47+
def part_1(equations: list[tuple[int, int], tuple[int, int]],
48+
width=101, height=103, default=0):
4849

4950
g = Grid([[default] * width] * height)
5051

5152
robot_to_point: dict[int, Point] = {}
5253
robot_to_velocity: dict[int, Point] = {}
5354
quadrant_to_num_robots: dict[int, int] = defaultdict(int)
5455

55-
for i, (start, velocity) in enumerate(_equations):
56+
for i, (start, velocity) in enumerate(equations):
5657

5758
robot_to_point[i] = pt = Point(*start)
5859
robot_to_velocity[i] = Point(*velocity)
@@ -87,7 +88,8 @@ def part_1(width=101, height=103, default=0):
8788
return safety_factor
8889

8990

90-
def part_2(width=101, height=103,
91+
def part_2(equations: list[tuple[int, int], tuple[int, int]],
92+
width=101, height=103,
9193
default='.',
9294
max_iterations=100_000):
9395

@@ -101,7 +103,7 @@ def part_2(width=101, height=103,
101103
final_t = None
102104
final_grid = None
103105

104-
for i, (start, velocity) in enumerate(_equations):
106+
for i, (start, velocity) in enumerate(equations):
105107
robot_to_point[i] = pt = Point(*start)
106108
robot_to_velocity[i] = Point(*velocity)
107109

@@ -150,32 +152,38 @@ def part_2(width=101, height=103,
150152
return final_t
151153

152154

153-
if __name__ == '__main__':
154-
if len(argv) <= 1:
155-
demo = True
156-
input_file = None
157-
else:
155+
def solve(input_file=None):
156+
if input_file:
158157
demo = False
159-
input_file = argv[1]
160-
161-
if demo:
162-
INPUT = DEMO_INPUT
163-
_width = 11
164-
_height = 7
165-
else:
158+
default = ' '
159+
print(f"Solving with input file {input_file}")
166160
try:
167161
INPUT = open(input_file).read()
168162
except FileNotFoundError:
163+
print('File not found, using default ...')
169164
INPUT = open(Path(__file__).parent.parent / 'Inputs' / '14').read()
170165
_width = 101
171166
_height = 103
172167

168+
else:
169+
demo = True
170+
default = '.'
171+
INPUT = DEMO_INPUT
172+
_width = 11
173+
_height = 7
174+
175+
print("Solving Day 14 Problem! 🎄")
176+
173177
_equations = remap(INPUT)
174178

175179
# Part 1
176-
_safety_factor = part_1(_width, _height)
180+
_safety_factor = part_1(_equations, _width, _height)
177181
print('Part 1:', _safety_factor)
178182

179183
# Part 2
180-
_t = part_2(default=' ')
184+
_t = part_2(_equations, default=default)
181185
print('Part 2:', _t)
186+
187+
188+
if __name__ == '__main__':
189+
solve()

AoC_2024/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__version__ = '0.1'

AoC_2024/run.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import sys
2+
from importlib import import_module
3+
4+
def main():
5+
# Get the day number from the command line arguments
6+
day_number = sys.argv[1].zfill(2) # e.g., '14'
7+
input_file = sys.argv[2] if len(sys.argv) > 2 else None # Optional input file
8+
9+
# Try to import the corresponding day module dynamically
10+
try:
11+
# Dynamically import the day script (e.g., AoC_2024.Days.day_14)
12+
day_module = import_module(f"AoC_2024.Days.{day_number}")
13+
# Call the solution function if it exists in the module
14+
if hasattr(day_module, "solve"):
15+
day_module.solve(input_file)
16+
else:
17+
print(f"No 'solve' function found in {day_number}.py.")
18+
except ModuleNotFoundError:
19+
print(f"Day {day_number} not found.")
20+
except Exception as e:
21+
print(f"Error: {e}")
22+
23+
if __name__ == "__main__":
24+
main()
File renamed without changes.

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
11
# Advent of Code (Python)
22

3+
> Advent of Code Solutions in Python
4+
5+
## Requirements
6+
7+
Requires **Python 3.9+** - Tested on `PY 3.13`.
8+
9+
## Setup
10+
11+
First, clone the Git Repo with HTTPS / SSH, or download
12+
the zip file.
13+
14+
```bash
15+
# HTTPS
16+
$ git clone https://github.com/rnag/Advent-of-Code.git
17+
# SSH
18+
git clone [email protected]:rnag/Advent-of-Code.git
19+
```
20+
21+
Next, `cd` into cloned `Advent-of-Code` project folder.
22+
23+
Then:
24+
25+
```console
26+
$ pip install -e .
27+
```
28+
29+
Run a specific Day script with `aoc <day>`.
30+
31+
For example, to solve Day 5 Problem:
32+
33+
```console
34+
$ aoc 5
35+
```

pyproject.toml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[tool.hatch.metadata]
6+
name = "Advent-of-Code"
7+
version = "0.1"
8+
description = "Solutions for Advent of Code 2024"
9+
authors = [
10+
{ name = "Ritvik Nag", email = "[email protected]" }
11+
]
12+
license = { file = "LICENSE" }
13+
readme = "README.md"
14+
keywords = ["advent of code", "python", "2024", "challenges"]
15+
classifiers = [
16+
"Programming Language :: Python :: 3",
17+
"Programming Language :: Python :: 3.13",
18+
"License :: OSI Approved :: MIT License",
19+
"Operating System :: OS Independent",
20+
]
21+
22+
[tool.hatch.version]
23+
path = "AoC_2024/__init__.py"
24+
25+
[tool.hatch.packages]
26+
include = ["AoC_2024", "AoC_2024.Days"]
27+
28+
[tool.hatch.build.targets.wheel]
29+
packages = ["AoC_2024"]
30+
31+
[project]
32+
name = "Advent-of-Code"
33+
dynamic = ["version"]
34+
dependencies = [
35+
"tqdm",
36+
]
37+
38+
[project.scripts]
39+
aoc = "AoC_2024.run:main"

requirements.txt

-1
This file was deleted.

0 commit comments

Comments
 (0)