2020 Advent of Code solutions in various languages
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
5.8 KiB

import math
3 years ago
import numpy as np
with open("/tmp/test.txt") as f:
# with open("../input/06-01.txt") as f:
input = f.read().splitlines()
inpout
input
3 years ago
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ]
from itertools import groupby
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ]
recs
recs = [ ([ split(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ]
recs = [ [ split(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ]
def clean(x):
return([chr for chr in x])
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ]
recs
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ]
recs
[ clean(g) for g in recs ]
recs = [ grp for key, grp in groupby(input, lambda str: str != "") if key ]
[ clean(g) for g in recs ]
recs
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ]
[ [ g for g in rec] for rec in recs ]
[ [ clean(g) for g in rec] for rec in recs ]
def clean(x):
[chr for chr in x]
[ [ clean(g) for g in rec] for rec in recs ]
def clean(x):
res = [chr for chr in x]
return(length(res))
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ]
def clean(x):
res = [chr for chr in x]
return(len(res))
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ]
recs
recs = [ list(grp) for key, grp in groupby(input, lambda str: str != "") if key ]
recs
def clean(x):
res = [chr for chr in x]
return(res)
return(len(res))
recs = [ [ clean(g) for g in grp ] for key, grp in groupby(input, lambda str: str != "") if key ]
recs
flatten = lambda t: [item for sublist in t for item in sublist]
recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
recs
def clean(x):
res = set([chr for chr in x])
return(len(res))
recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
recs
flatten = lambda x: [item for sublist in x for item in sublist]
clean = lambda x: len(set([chr for chr in x]))
recs
sum(recs)
with open("../input/06-01.txt") as f:
input = f.read().splitlines()
flatten = lambda x: [item for sublist in x for item in sublist]
clean = lambda x: len(set([chr for chr in x]))
recs = [ clean(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
sum(recs)
from collections import Counter
count02 = lambda x: Counter(x)
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
with open("/tmp/test.txt") as f:
# with open("../input/06-01.txt") as f:
input = f.read().splitlines()
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
count02 = lambda x: list(Counter(x))
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
count02 = lambda x: Counter(x)
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
[Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 2, 'b': 1, 'c': 1}), Counter({'a': 4}), Counter({'b': 1})]
a = [Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 2, 'b': 1, 'c': 1}), Counter({'a': 4}), Counter({'b': 1})]
a[0].key
a[0].values()
[v for v in a[0].values() if v == 1]
[ grp for grp in a]
[ [ v for v in grp.values() if v == 1 ] for grp in a ]
count02 = lambda x: [ answer for size, answer in (len(x), Counter(x)) ]
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
count02 = lambda x: [ answer for size, answer in [ len(x), Counter(x) ] ]
a = [Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 1, 'b': 1, 'c': 1}), Counter({'a': 2, 'b': 1, 'c': 1}), Counter({'a': 4}), Counter({'b': 1})]
a
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
def count02(grp):
grp
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
sum([ count01(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ])
flatten = lambda x: [item for sublist in x for item in sublist]
count01 = lambda x: len(set([chr for chr in x]))
# 06-01
sum([ count01(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ])
def count02(grp):
grp
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
def count02(grp):
return(grp)
[ count02(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ]
[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ]
def count02(grp):
return(len(grp))
[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ]
def count02(grp):
grp_sz = len(grp)
return(Counter(flatten(grp)))
[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ]
def count02(grp):
return([ val for val in Counter(flatten(grp)).values() if val == len(grp)])
[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ]
def count02(grp):
return(len([ val for val in Counter(flatten(grp)).values() if val == len(grp)]))
[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ]
count02 = lambda x: len([ val for val in Counter(flatten(x)).values() if val == len(x)])
[ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ]
# with open("/tmp/test.txt") as f:
with open("../input/06-01.txt") as f:
input = f.read().splitlines()
count01 = lambda x: len(set([chr for chr in x]))
sum([ count01(flatten(list(grp))) for key, grp in groupby(input, lambda str: str != "") if key ])
count02 = lambda x: len([ val for val in Counter(flatten(x)).values() if val == len(x)])
sum([ count02(list(grp)) for key, grp in groupby(input, lambda str: str != "") if key ])