8000 Part 2 tests pass · dumbledad/adventofcode@5839877 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5839877

Browse files
author
Tim Regan
committed
Part 2 tests pass
1 parent b90178b commit 5839877

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

2022/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@ fixed it!
9393
- Writing `logging.info` statements to reproduce Eric's explanatory test is a useful additional test
9494
if the algorithm passes on the test data but fails on the 'real' data (see [reddit thread](https://old.reddit.com/r/adventofcode/comments/zkq7qk/2022_day_13_part_1_debugging_advice_please/))
9595
- Lots of my time is wasted with sloppy writing, e.g. `[i for i in range(self.sorted_packets) if self.sorted_packets[i] in self.divider_packets.contains]` instead of `[i + 1 for i in range(len(self.sorted_packets)) if self.sorted_packets[i] in self.divider_packets]`
96+
97+
### Day 14
98+
99+
- `extend` instead of `append` adds the items in the list. No need to worry about how to flatten.

2022/day14.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ def __init__(self, filename):
1616
if idx + 1 < len(coords):
1717
start_line = tuple([int(num) for num in coords[idx].split(',')])
1818
end_line = tuple([int(num) for num in coords[idx + 1].split(',')])
19-
2019
if start_line[0] <= e 8000 nd_line[0]:
2120
x_range = range(start_line[0], end_line[0] + 1)
2221
else:
@@ -51,17 +50,19 @@ def bounds(self):
5150
bounds['max_y'] = int(match.group(2))
5251
return bounds
5352

54-
def draw(self, row_numbers=False):
53+
def draw(self, row_numbers=False, with_floor=False):
54+
bonkers_min_x = self.start[0] - ((self.bounds['max_x'] + self.bounds['max_y']) - self.start[0])
55+
min_x = bonkers_min_x if with_floor else self.bounds['min_x']
5556
for i, row in enumerate(self.grid):
5657
row_number_str = f'{i} ' if row_numbers else ''
57-
print(f"{row_number_str}{''.join(row[self.bounds['min_x']:])}")
58+
print(f"{row_number_str}{''.join(row[min_x:])}")
5859

5960
def drop_grain(self, with_floor=True):
60-
if self.grid[self.start[1]][self.start[0]] == 'o': # There's sand blocking the start
61-
return False
6261
grain_coords = self.start
63-
while grain_coords[1] < self.bounds['max_y']:
64-
if self.grid[grain_coords[1] + 1][grain_coords[0]] == '.':
62+
while (grain_coords[1] < self.bounds['max_y']) or with_floor:
63+
if self.grid[self.start[1]][self.start[0]] == 'o': # There's sand blocking the start
64+
return False
65+
elif self.grid[grain_coords[1] + 1][grain_coords[0]] == '.':
6566
grain_coords = (grain_coords[0], grain_coords[1] + 1)
6667
elif self.grid[grain_coords[1] + 1][grain_coords[0] - 1] == '.':
6768
grain_coords = (grain_coords[0] - 1, grain_coords[1] + 1)
@@ -74,14 +75,25 @@ def drop_grain(self, with_floor=True):
7475
return False
7576

7677
def keep_pouring(self, with_floor=True):
78+
if with_floor:
79+
self._add_floor()
7780
while(self.drop_grain(with_floor)):
81+
self.draw(with_floor=with_floor)
7882
continue
7983
return len(self.grains)
8084

85+
def _add_floor(self):
86+
max_x = self.bounds['max_x'] + self.bounds['max_y']
87+
for i, row in enumerate(self.grid):
88+
row.extend(['.'] * self.bounds['max_y'])
89+
self.grid.append(['.'] * max_x) # WARNING: assumes grains do not fall into -x territory
90+
self.grid.append(['#'] * max_x)
91+
8192
def main():
82-
filename = 'inputs/2022/day14.txt'
93+
filename = 'inputs/2022/day14-test.txt'
8394
cave = Cave(filename)
84-
print(f'Part 1: {cave.keep_pouring(False)}')
95+
cave.keep_pouring(True)
96+
#print(f'Part 1: {cave.keep_pouring(False)}')
8597

8698
if __name__ == '__main__':
8799
main()

2022/test_day14.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616
9 #########.
1717
'''
1818

19+
with_floor = '''............o............
20+
...........ooo...........
21+
..........ooooo..........
22+
.........ooooooo.........
23+
........oo#ooo##o........
24+
.......ooo#ooo#ooo.......
25+
......oo###ooo#oooo......
26+
.....oooo.oooo#ooooo.....
27+
....oooooooooo#oooooo....
28+
...ooo#########ooooooo...
29+
..ooooo.......ooooooooo..
30+
#########################
31+
'''
32+
1933
def test_cave_init():
2034
cave = Cave(filename)
2135
assert cave.data
@@ -29,7 +43,13 @@ def test_cave_bounds():
2943
'max_y': 9
3044
}
3145

32-
def test_cave_draw(capfd):
46+
def test_cave_draw_no_floor(capfd):
47+
cave = Cave(filename)
48+
cave.draw(row_numbers=True)
49+
out, _ = capfd.readouterr()
50+
assert out == initial_grid
51+
52+
def test_cave_draw_floor(capfd):
3353
cave = Cave(filename)
3454
cave.draw(row_numbers=True)
3555
out, _ = capfd.readouterr()
@@ -51,3 +71,5 @@ def test_cave_drop_grain():
5171
def test_cave_keep_pouring():
5272
cave = Cave(filename)
5373
assert cave.keep_pouring(False) == 24
74+
cave = Cave(filename)
75+
assert cave.keep_pouring(True) == 93

0 commit comments

Comments
 (0)
0