-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.py
102 lines (77 loc) · 2.89 KB
/
day3.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import pytest
from array import array
from pprint import pprint
class Claim(object):
def __init__(self, data=None):
self.map = []
if data:
self.lines = data
else:
with open("./day3.txt", 'r') as f:
self.lines = f.readlines()
self.max_edge = array('i', [0,0])
self.max_rect = array('i', [0,0])
for line in self.lines:
clm_id = int(line.split("@")[0].strip("#"))
clm_edge = list(map(int, line.split("@")[1].split(":")[0].split(",")))
clm_rect = list(map(int, line.split("@")[1].split(":")[1].split("x")))
self.map.append([clm_id, clm_edge, clm_rect])
if clm_edge[0] > self.max_edge[0]:
self.max_edge[0] = clm_edge[1]
if clm_edge[1] > self.max_edge[1]:
self.max_edge[1] = clm_edge[1]
if clm_rect[0] > self.max_rect[0]:
self.max_rect[0] = clm_rect[0]
if clm_rect[1] > self.max_rect[1]:
self.max_rect[1] = clm_rect[1]
def print_data(self):
for k,v in enumerate(self.lines):
print("line {}: {}".format(k,v))
for i in self.map:
print(i)
def populate_map(self):
col = self.max_rect[0] + self.max_edge[0]
row = self.max_rect[1] + self.max_edge[1]
claim_map = [[0]*col for i in range(row)]
for i in self.map:
start_col, start_row = i[1]
clm_width, clm_height = i[2]
for j in range(start_col, start_col+clm_width):
for k in range(start_row, start_row+clm_height):
if claim_map[k][j] == 0:
claim_map[k][j] = 1
else:
claim_map[k][j] = 2
sum = 0
for i in range(col):
sum += claim_map[i].count(2)
print(sum)
def overlap_map(self):
col = self.max_rect[0] + self.max_edge[0]
row = self.max_rect[1] + self.max_edge[1]
claim_map = [[0]*col for i in range(row)]
for i in self.map:
start_col, start_row = i[1]
clm_width, clm_height = i[2]
for j in range(start_col, start_col+clm_width):
for k in range(start_row, start_row+clm_height):
claim_map[k][j] += 1
done = False
for i in self.map:
tmp=[]
start_col, start_row = i[1]
clm_width, clm_height = i[2]
for j in range(start_col, start_col+clm_width):
for k in range(start_row, start_row+clm_height):
tmp.append(claim_map[k][j])
if all(map(lambda x: x == 1, tmp)):
print(i[0])
def test_read_file():
data = \
["#1 @ 1,3: 4x4",
"#2 @ 3,1: 4x4",
"#3 @ 5,5: 2x2"]
# clm = Claim(data)
clm = Claim()
# clm.populate_map()
clm.overlap_map()