Skip to content

Commit

Permalink
Merge pull request #49 from Moelf/rk/py
Browse files Browse the repository at this point in the history
day 11 python.
  • Loading branch information
RongkunWang authored Dec 11, 2023
2 parents d192557 + 2e5d204 commit 1f6b575
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
10 changes: 10 additions & 0 deletions inputs/11_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
2 changes: 2 additions & 0 deletions solutions/11_example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
374
82000210
77 changes: 77 additions & 0 deletions src/python/11_rk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python3
import sys
import collections

def parse(infile):
'''
build the map
'''
fullMap = [[c for c in line.strip()] for line in infile]
return fullMap


def dis(pos1, pos2, empty_row, empty_col, sol2 = False):
# empty col inside
dist = len([i for i in empty_row if (i - pos1[0]) * (i - pos2[0]) < 0])
dist += len([i for i in empty_col if (i - pos1[1]) * (i - pos2[1]) < 0])

dist *= (1000000-1) if sol2 else 1
# test case
# dist *= (10-1) if sol2 else 1
# dist *= (100-1) if sol2 else 1

dist += abs(pos1[0] - pos2[0])
dist += abs(pos1[1] - pos2[1])

# print(pos1, pos2, dist)
return dist


def sol1(m):
height = len(m)
width = len(m[0])
l_galaxy = []
l_empty_row = []
l_empty_col = []

for r, row_content in enumerate(m):
all_dot = True
for c, char in enumerate(row_content):
if char == "#":
l_galaxy.append((r, c))
all_dot = False
if all_dot:
l_empty_row.append(r)

for c in range(width):
all_dot = True
for r in range(height):
if m[r][c] == "#":
all_dot = False
if all_dot:
l_empty_col.append(c)


# print(l_empty_row)
# print(l_empty_col)
s = 0
s2 = 0
for i in range(len(l_galaxy)):
for j in range(i+1, len(l_galaxy)):
s += dis(l_galaxy[i], l_galaxy[j], l_empty_row, l_empty_col)
s2 += dis(l_galaxy[i], l_galaxy[j], l_empty_row, l_empty_col, True)
pass
pass
return s, s2


def main():
with open(sys.argv[1], 'r') as infile:
m = parse(infile)
# print(m)
sol = sol1(m)
print(sol[0])
print(sol[1])
pass

main()

0 comments on commit 1f6b575

Please sign in to comment.