Cleaned the code using a more functional style
This commit is contained in:
@@ -1,33 +1,29 @@
|
|||||||
with open('input') as f:
|
from functools import reduce
|
||||||
data = [line.strip() for line in f.read().split('\n\n')]
|
|
||||||
|
with open('input') as f:
|
||||||
|
data = [line.strip().split() for line in f.read().split('\n\n')]
|
||||||
|
|
||||||
|
def parse_data(data):
|
||||||
|
groups = []
|
||||||
|
for line in data:
|
||||||
|
group = []
|
||||||
|
for answers in line:
|
||||||
|
group.append(set(answers))
|
||||||
|
groups.append(group)
|
||||||
|
return groups
|
||||||
|
|
||||||
|
def solve_a(groups):
|
||||||
|
union = lambda a, b: a | b
|
||||||
|
count = lambda g: len(reduce(union, g))
|
||||||
|
return sum(map(count, groups))
|
||||||
|
|
||||||
def solve_a(data):
|
|
||||||
count = 0
|
|
||||||
for group in data:
|
|
||||||
answers = set()
|
|
||||||
for c in group:
|
|
||||||
if c != '\n':
|
|
||||||
answers.add(c)
|
|
||||||
count += len(answers)
|
|
||||||
return count
|
|
||||||
|
|
||||||
def solve_b(data):
|
def solve_b(data):
|
||||||
count = 0
|
intersection = lambda a, b: a & b
|
||||||
for group in data:
|
count = lambda g: len(reduce(intersection, g))
|
||||||
count_map = {}
|
return sum(map(count, groups))
|
||||||
for c in group:
|
|
||||||
if c == '\n': continue
|
|
||||||
if c in count_map:
|
|
||||||
count_map[c] += 1
|
|
||||||
else:
|
|
||||||
count_map[c] = 1
|
|
||||||
for k in count_map:
|
|
||||||
if count_map[k] == len(group.split()): count += 1
|
|
||||||
print(count_map, count, len(group.split()))
|
|
||||||
|
|
||||||
return count
|
|
||||||
|
|
||||||
|
|
||||||
print(solve_a(data))
|
groups = parse_data(data)
|
||||||
print(solve_b(data))
|
print(solve_a(groups))
|
||||||
|
print(solve_b(groups))
|
||||||
|
|||||||
Reference in New Issue
Block a user