-
Notifications
You must be signed in to change notification settings - Fork 0
/
D14.py
68 lines (59 loc) · 1.91 KB
/
D14.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'''
# PART 1
## Input
We are provided with a grid of rocks and open space:
O....#....
O.OO#....#
.....##...
OO.#O....O
.O.....O#.
O.#..O.#.#
..O..#O..O
.......O..
#....###..
#OO..#....
The '0's are rocks that will roll and the '#'s are rocks that
are fixed in place. When we tip the platform represented by this
grid upwards, towards the north, the rocks rearrange to look like
this:
OOOO.#.O..
OO..#....#
OO..O##..O
O..#.OO...
........#.
..#....#.#
..O..#.O.O
..O.......
#....###..
#....#....
## Objective
We want to add up 'weights' of each round stone, starting with a weight
equal to the number of rows (10 in the example) for each stone in the top row
and one less for each stone in each subsequent row.
## Approach
This is easier to thing about if we rotate the grid so that each column
is a list that we can parse. Then it is simply a matter of counting how many
round stones lie between the static stones and the edges of the grid, and
then calulating their weights based on where they would land.
'''
with open('D14.txt', 'r') as file:
grid = file.read().splitlines()
cols = [[grid[x][y] for x in range(len(grid))] for y in range(len(grid[0]))]
def weigh_slided_rocks(col):
ret = 0
previous_block = -1
block_index = col.index("#") if "#" in col else len(col)
while True:
if block_index == previous_block:
return ret
count = col[previous_block+1 : block_index].count('O')
# print(f'found {count} rocks in this section')
add = sum(range(len(col) - previous_block -count, len(col) - previous_block))
# print(f'adding {add}')
ret += add
previous_block = block_index
try:
block_index = col.index('#', previous_block+1)
except:
block_index = len(col)
print(f'part 1: {sum([weigh_slided_rocks(col) for col in cols])}')