Added 2020 (python)
This commit is contained in:
BIN
2020/6/__pycache__/prog.cpython-38.pyc
Normal file
BIN
2020/6/__pycache__/prog.cpython-38.pyc
Normal file
Binary file not shown.
2084
2020/6/input
Normal file
2084
2020/6/input
Normal file
File diff suppressed because it is too large
Load Diff
61
2020/6/prog.py
Normal file
61
2020/6/prog.py
Normal file
@ -0,0 +1,61 @@
|
||||
def get_input(sample = False):
|
||||
ret = []
|
||||
with open("sample" if sample else "input", "r") as f:
|
||||
group = []
|
||||
for line in f.readlines():
|
||||
if line == "\n": # empty line
|
||||
ret.append(group)
|
||||
group = []
|
||||
else:
|
||||
group.append(line.strip())
|
||||
ret.append(group) # add last group
|
||||
return ret
|
||||
|
||||
def list_of_string_to_list(s: set):
|
||||
ret = []
|
||||
for string in s:
|
||||
ret += list(string)
|
||||
return ret
|
||||
|
||||
def grouped_answers(answers: list):
|
||||
ret = []
|
||||
start_i = 0
|
||||
prev = answers[start_i]
|
||||
for cur_i in range(1, len(answers)):
|
||||
cur = answers[cur_i]
|
||||
if prev != cur:
|
||||
ret.append(answers[start_i:cur_i])
|
||||
start_i = cur_i
|
||||
prev = cur
|
||||
ret.append(answers[start_i:]) # add last answer
|
||||
return ret
|
||||
|
||||
def list_of_string_to_set(s: set):
|
||||
ret = set()
|
||||
for string in s:
|
||||
ret |= set(string)
|
||||
return ret
|
||||
|
||||
def get_result(inp: list, part = 1):
|
||||
ret = 0
|
||||
for group in inp:
|
||||
if part == 1:
|
||||
ret += len(list_of_string_to_set(group))
|
||||
else:
|
||||
group_len = len(group)
|
||||
splitted_answers = list_of_string_to_list(group)
|
||||
splitted_answers.sort()
|
||||
ga = grouped_answers(splitted_answers)
|
||||
ret += sum(map(lambda x: int(len(x) == group_len), ga))
|
||||
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(get_result(get_input(), part = 2))
|
||||
|
||||
|
||||
|
15
2020/6/sample
Normal file
15
2020/6/sample
Normal file
@ -0,0 +1,15 @@
|
||||
abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
23
2020/6/test.py
Normal file
23
2020/6/test.py
Normal file
@ -0,0 +1,23 @@
|
||||
from prog import *
|
||||
|
||||
inp = get_input(sample = True)
|
||||
|
||||
expected_inp = [
|
||||
["abc"],
|
||||
|
||||
["a", "b", "c"],
|
||||
|
||||
["ab", "ac"],
|
||||
|
||||
["a", "a", "a", "a"],
|
||||
|
||||
["b"]
|
||||
]
|
||||
expected_result_1 = 11
|
||||
expected_result_2 = 6
|
||||
|
||||
print(inp == expected_inp)
|
||||
|
||||
print(get_result(inp) == expected_result_1)
|
||||
|
||||
print(get_result(inp, part = 2) == expected_result_2)
|
Reference in New Issue
Block a user