Skip to content

Commit 8bc24dd

Browse files
committed
old 2022
1 parent a15445b commit 8bc24dd

File tree

4 files changed

+177
-16
lines changed

4 files changed

+177
-16
lines changed

.utils/new_year.py

+16-15
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010
year = datetime.now().year
1111

1212
# Create a new directory
13-
new_dir = Path(cwd, f'{year}/tests').mkdir(exist_ok=True)
13+
new_dir = Path(cwd, f"{year}/tests").mkdir(exist_ok=True)
1414

1515
# Copy file from .utils to new directory
16-
shutil.copy(Path(cwd, '.utils', 'day.py'), Path(cwd, f'{year}'))
17-
shutil.copy(Path(cwd, '.template', 'conftest.py'), Path(cwd, f'{year}/tests'))
16+
shutil.copy(Path(cwd, ".utils", "day.py"), Path(cwd, f"{year}"))
17+
shutil.copy(Path(cwd, ".templates", "conftest.py"), Path(cwd, f"{year}/tests"))
1818

1919

2020
with open(Path(Path(__file__).resolve().parent.parent, "README.md"), "r+") as f:
2121
splitter = "## 🌟 Completion Status 🌟"
2222
goal_splitter = f"### {year-1}"
2323
old_readme = f.read()
24-
24+
2525
if str(year) in old_readme:
2626
print("Already updated!")
2727
exit()
@@ -36,18 +36,19 @@
3636

3737
comment = " <!--{y}.{d:02d}.{p}--> |"
3838

39-
new_table = ["\n\n| | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th | 10th | 11th | 12th | 13th |",
40-
"\n| ------ |" + " --- |" * 13,
41-
"\n| Part 1 |" + "".join((comment.format(y=year, d=day, p=1) for day in range(1, 14))),
42-
"\n| Part 2 |" + "".join((comment.format(y=year, d=day, p=2) for day in range(1, 14))),
43-
"\n\n| | 14th | 15th | 16th | 17th | 18th | 19th | 20th | 21st | 22nd | 23rd | 24th | 25th |",
44-
"\n| ------ |" + " --- |" * 12,
45-
"\n| Part 1 |" + "".join((comment.format(y=year, d=day, p=1) for day in range(14, 26))),
46-
"\n| Part 2 |" + "".join((comment.format(y=year, d=day, p=2) for day in range(14, 26))),
47-
""
39+
new_table = [
40+
"\n\n| | 1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th | 10th | 11th | 12th | 13th |",
41+
"\n| ------ |" + " --- |" * 13,
42+
"\n| Part 1 |" + "".join((comment.format(y=year, d=day, p=1) for day in range(1, 14))),
43+
"\n| Part 2 |" + "".join((comment.format(y=year, d=day, p=2) for day in range(1, 14))),
44+
"\n\n| | 14th | 15th | 16th | 17th | 18th | 19th | 20th | 21st | 22nd | 23rd | 24th | 25th |",
45+
"\n| ------ |" + " --- |" * 12,
46+
"\n| Part 1 |" + "".join((comment.format(y=year, d=day, p=1) for day in range(14, 26))),
47+
"\n| Part 2 |" + "".join((comment.format(y=year, d=day, p=2) for day in range(14, 26))),
48+
"",
4849
]
4950

5051
new_text = [intro, new_goals, goal_splitter, goals, splitter, new_year_head] + new_table + [completion_status]
51-
52+
5253
f.seek(0)
53-
f.write("".join(new_text))
54+
f.write("".join(new_text))

2022/Readme.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,10 @@ It's December 24th when I finally solved this with heavy help from the solutions
152152
### 2022-12-18
153153
- Made a logic error, where I thought a single dimension has to be offset by one to be a neighbour. No idea how I had that error in my mind.
154154
- `cube = nx.grid_graph(dim=(range(min_x-2, max_x+2), range(min_y-2, max_y+2), range(min_z-2, max_z+2)) SyntaxError: '(' was never closed` just autocomplete things.
155-
- `KeyError: (-2, -2, -2)` oops. wrong minimum
155+
- `KeyError: (-2, -2, -2)` oops. wrong minimum
156+
157+
### 2022-12-19
158+
- `self.robot_cost = {ore: None, clay: None, obsidian: None, geode: None} NameError: name 'ore' is not defined. Did you mean: 'ord'?` forgot to make the keys strings
159+
- `self.robot_cost["ore"] = extract_cost(re.search(r'Each ore robot costs ([\d a-z]+).', self.data).group(1)) NameError: name 'extract_cost' is not defined` method not function. my bad
160+
- `def Factory: SyntaxError: expected '('` that should've been a class
161+
- `self.ore += self.robots['ore']AttributeError: 'Factory' object has no attribute 'ore'` worked on changing the resources to a Counter

2022/day19.py

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
from day import Day
2+
from aocd import submit
3+
from collections import Counter, deque
4+
import re
5+
# Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 4 ore. Each obsidian robot costs 4 ore and 12 clay. Each geode robot costs 4 ore and 19 obsidian.
6+
7+
8+
class Blueprint:
9+
def __init__(self, data):
10+
self.data = data
11+
self.number = None
12+
self.robots = Counter({"ore": {}, "clay": {}, "obsidian": {}, "geode": {}})
13+
self.parse()
14+
15+
def __str__(self):
16+
return f"Blueprint {self.number}: robot {self.robots['ore']}, clay robot - {self.robots['clay']}, obsidian robot - {self.robots['obsidian']}, geode robot - {self.robots['geode']}"
17+
18+
def __repr__(self):
19+
return self.__str__()
20+
21+
def parse(self):
22+
self.number = re.search(r'Blueprint (\d+):', self.data).group(1)
23+
self.robots['ore'] = self.extract_cost(re.search(r'Each ore robot costs ([\d a-z]+).', self.data).group(1))
24+
self.robots['clay'] = self.extract_cost(re.search(r'Each clay robot costs ([\d a-z]+).', self.data).group(1))
25+
self.robots['obsidian'] = self.extract_cost(re.search(r'Each obsidian robot costs ([\d a-z]+).', self.data).group(1))
26+
self.robots['geode'] = self.extract_cost(re.search(r'Each geode robot costs ([\d a-z]+).', self.data).group(1))
27+
28+
def extract_cost(self, costs):
29+
parsed_cost = Counter()
30+
for cost in costs.split(' and '):
31+
match cost.split(' '):
32+
case [num, 'ore']:
33+
parsed_cost['ore'] = int(num)
34+
case [num, 'clay']:
35+
parsed_cost['clay'] = int(num)
36+
case [num, 'obsidian']:
37+
parsed_cost['obsidian'] = int(num)
38+
return parsed_cost
39+
40+
class Factory:
41+
def __init__(self, blueprint, minute=24):
42+
self.blueprint = blueprint
43+
self.resources = Counter({"ore": 0, "clay": 0, "obsidian": 0, "geode": 0})
44+
self.minutes = minute
45+
self.robots = Counter({"ore": 1, "clay": 0, "obsidian": 0, "geode": 0})
46+
47+
def __str__(self):
48+
return f"Factory: {self.blueprint.number}\n\tStorage: {self.resources}\n\tRobots: {self.robots+Counter()}\n"
49+
50+
def __repr__(self):
51+
return self.__str__()
52+
53+
def mine(self):
54+
for resource in self.resources.keys():
55+
self.resources[resource] += self.robots[resource]
56+
57+
def build(self, robot=None):
58+
if robot is None:
59+
robot = []
60+
else:
61+
self.resources = self.resources - self.blueprint.robots[robot]
62+
robot = [robot]
63+
64+
self.built_robots = Counter(robot)
65+
66+
def deploy(self):
67+
self.robots = self.robots + self.built_robots
68+
self.built_robots = Counter()
69+
70+
def available_to_build(self):
71+
return [name for name, robot in self.blueprint.robots.items() if self.resources >= robot]
72+
73+
def run(self, robot=None):
74+
self.build(robot)
75+
self.mine()
76+
self.deploy()
77+
self.minutes -= 1
78+
79+
def find_max_geodes(self):
80+
while self.minutes > 0:
81+
self.run()
82+
return self.resources["geode"]
83+
84+
def quality(self):
85+
return self.resources["geode"] * self.number
86+
87+
88+
def main(day, part=1):
89+
day.parse_list()
90+
day.apply(Blueprint)
91+
day.apply(Factory)
92+
if part == 1:
93+
day.data[1].run()
94+
day.data[1].run()
95+
day.data[1].run()
96+
day.data[1].run()
97+
day.data[1].run()
98+
day.data[1].run()
99+
return day.data
100+
if part == 2:
101+
pass
102+
103+
if __name__ == "__main__":
104+
day = Day(19)
105+
day.download()
106+
107+
day.load()
108+
data = """Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian.
109+
Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 12 obsidian."""
110+
111+
day.load(data)
112+
p1 = main(day)
113+
print(p1)
114+
# submit(p1, part="a", day=19, year=2022)
115+
116+
# day.load()
117+
# p2 = main(day, part=2)
118+
# print(p2)
119+
# submit(p2, part="b", day=19, year=2022)

2022/tests/test_day19.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import sys
2+
import pytest
3+
4+
sys.path.insert(0, ".")
5+
from day import Day
6+
from day19 import *
7+
8+
@pytest.fixture(scope="function")
9+
def example():
10+
day = Day(19)
11+
data = """Blueprint 1: Each ore robot costs 4 ore. Each clay robot costs 2 ore. Each obsidian robot costs 3 ore and 14 clay. Each geode robot costs 2 ore and 7 obsidian.
12+
Blueprint 2: Each ore robot costs 2 ore. Each clay robot costs 3 ore. Each obsidian robot costs 3 ore and 8 clay. Each geode robot costs 3 ore and 12 obsidian."""
13+
14+
day.load(data)
15+
return day
16+
17+
@pytest.fixture(scope="function")
18+
def day():
19+
day = Day(19)
20+
day.load()
21+
return day
22+
23+
## Part 1
24+
def test_example(example):
25+
assert main(example, part=1) == 33
26+
27+
def test_part1(day):
28+
assert main(day, part=1) == False
29+
30+
## Part 2
31+
def test_example_p2(example):
32+
assert main(example, part=2) == False
33+
34+
def test_part2(day):
35+
assert main(day, part=2) == False

0 commit comments

Comments
 (0)