-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_data.py
169 lines (119 loc) · 3.81 KB
/
read_data.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import re
def get_data_00(path: str) -> str:
with open(path) as f:
content = f.read()
return content.strip()
def get_data_01_int_list(path: str) -> list[int]:
"""
File content example:
173
178
179
Return example: [173, 178, 179]
"""
data = open(path, 'r').read()
numbers = [int(number) for number in data.split()]
return numbers
def get_data_02_str_list(path: str) -> list[str]:
"""
File content example:
217, 490 -> 217, 764
44, 270 -> 373, 599
Return example: ['217,490 -> 217,764', '44,270 -> 373,599', '440,139 -> 440,303']
"""
with open(path) as f:
input_lines = [line.rstrip() for line in f]
return input_lines
def get_data_03_two_dim_int_list(path: str) -> list[list[int]]:
"""
File content example:
91 60 70 64 83
35 41 79 55 31
7 58 25 3 47
Return example: [[91, 60, 70, 64, 83], [35, 41, 79, 55, 31], [7, 58, 25, 3, 47]]
"""
input_data = [num.strip() for num in open(path, 'r').read().split('\n')]
input_data = [[int(num) for num in row.split()] for row in input_data if len(row) > 0]
return input_data
def get_data_04_two_dim_str_list(path: str) -> list[list[str]]:
"""
File content example:
A Y
B X
C Z
Return example: [['A', 'Y'], ['B', 'X'], ['C', 'Z']]
"""
with open(path) as f:
input_lines = [line.strip() for line in f]
return [line.split() for line in input_lines]
def get_data_05_str_int_two_cols(path: str) -> list[tuple[str, int]]:
"""
File content example:
R 4
U 4
L 3
Return example: [('A', 4), ('U', 4), ('L', 3)]
"""
return [(c[0], int(c[1])) if len(c) == 2 else (c[0],) for c in [row.split() for row in open(path, 'r').readlines()]]
def get_data_06_alternate_rows_lists(path: str) -> tuple[list[str], list[int]]:
"""
File content example:
forward
2
forward
6
forward
8
Return example: (['forward', 'forward', 'forward'], [2, 6, 8])
"""
data = open(path, 'r').read().split()
directions = [data[i] for i in range(len(data)) if i % 2 == 0]
values = [int(data[i]) for i in range(len(data)) if i % 2 == 1]
return directions, values
def get_data_07(path: str) -> tuple[set[tuple[int, int]], list[tuple[str, int]]]:
"""
File content example:
323,511
1240,588
1210,140
fold along x = 655
fold along y = 447
fold along x = 327
Return example: ({(335, 767), (13, 753), (843, 289)}, [('x', 655), ('y', 447), ('x', 327)])
"""
point_pattern = re.compile(r'\d*,\d*')
instrucrtions_pattern = re.compile(r'[xy]=\d*')
points = set()
with open(path) as f:
s = f.read()
pairs = point_pattern.findall(s)
instructions = [i.split('=') for i in instrucrtions_pattern.findall(s)]
pairs = [pair.split(',') for pair in pairs]
points.update([(int(x), int(y)) for x, y in pairs])
instructions = [(i[0], int(i[1])) for i in instructions]
return points, instructions
def get_data_08(path: str) -> tuple[str, dict[str, str]]:
"""
File content example:
HBHVVNPCNFPSVKBPPCBH
HV -> B
KS -> F
NH -> P
Return example: (HBHVVNPCNFPSVKBPPCBH, {'HV': 'B', 'KS': 'F', 'NH': 'P'})
"""
with open(path, 'r') as f:
lines = f.read()
input_rules = lines.split()
start = input_rules[0]
input_rules = {input_rules[a]: input_rules[a + 2] for a in range(1, len(input_rules) - 1) if (a - 1) % 3 == 0}
return start, input_rules
def get_data_09_all_line_ints(path: str) -> list[list[int]]:
"""
File content example:
2-4,6-8
2-3,4-5
5-7,7-9
Return example: [[2, 4, 6, 8], [2, 3, 4, 5], [5, 7, 7, 9]]
"""
data = get_data_02_str_list(path)
return [[int(n) for n in gr] for gr in [re.findall(r'-?\d+', row) for row in data]]