Skip to content

Instantly share code, notes, and snippets.

@jiamo
Last active December 10, 2021 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jiamo/a7be20f1d865e23c5de15fc6f0def08e to your computer and use it in GitHub Desktop.
Save jiamo/a7be20f1d865e23c5de15fc6f0def08e to your computer and use it in GitHub Desktop.
adventofcode-2021-day8
import os
"""
0: 1: 2: 3: 4:
aaaa .... aaaa aaaa ....
b c . c . c . c b c
b c . c . c . c b c
.... .... dddd dddd dddd
e f . f e . . f . f
e f . f e . . f . f
gggg .... gggg gggg ....
5: 6: 7: 8: 9:
aaaa aaaa aaaa aaaa aaaa
b . b . . c b c b c
b . b . . c b c b c
dddd dddd .... dddd dddd
. f e f . f e f . f
. f e f . f e f . f
gggg gggg .... gggg gggg
"""
number = {
"1": 2,
"4": 4,
"7": 3,
"8": 7,
}
number_reverse = {
2: "1",
4: "4",
3: "7",
7: "8",
5: ["3", "2", "5"],
6: ["6", "9", "0"]
}
#
number_index = {
"1": (2, 5),
"4": (1, 2, 3, 5),
"7": (0, 2, 5),
"8": (0, 1, 2, 3, 4, 5, 6),
"2": (0, 2, 3, 4, 6),
"3": (0, 2, 3, 5, 6),
"5": (0, 1, 3, 5, 6),
"0": (0, 1, 2, 4, 5, 6),
"6": (0, 1, 3, 4, 5, 6),
"9": (0, 1, 2, 3, 5, 6),
}
reverse_project = {}
for key, item in number_index.items():
reverse_project[tuple(sorted(item))] = key
number_todo = {}
def fix(f, xs):
new_xs = f(xs)
if xs == new_xs:
return xs
return fix(f, new_xs)
def unify_helper(xs):
new_xs = xs[:]
for x in xs:
for y in xs:
if x == y:
continue
if x[0].issubset(y[0]):
if x not in new_xs:
if x:
new_xs.append(x)
if y[0]-x[0] and y[1]-x[1] and (y[0]-x[0], y[1]-x[1]) not in new_xs:
new_xs.append((y[0]-x[0], y[1]-x[1]))
if y in new_xs:
new_xs.remove(y)
if x[0].intersection(y[0]):
k = x[0].intersection(y[0])
v = x[1].intersection(y[1])
k1 = x[0] - k
v1 = x[1] - v
k2 = y[0] - k
v2 = y[1] - v
if v:
if (k, v) not in new_xs:
new_xs.append((k, v))
if k1 and v1:
if (k1, v1) not in new_xs:
new_xs.append((k1, v1))
if k2 and v2:
if (k2, v2) not in new_xs:
new_xs.append((k2, v2))
return new_xs
z = set(range(0, 7))
w = set('abcdefg')
def unify(xs, ys):
len5_k = set.intersection(*[i[0] for i in ys if len(i[0]) == 5])
len5_v = set.intersection(*[i[1] for i in ys if len(i[1]) == 5])
xs.append((len5_k, len5_v))
len6_k = set.intersection(*[i[0] for i in ys if len(i[0]) == 6])
len6_v = set.intersection(*[i[1] for i in ys if len(i[1]) == 6])
xs.append((len6_k, len6_v))
xs = fix(unify_helper, xs)
g = {}
for k,v in xs:
g[list(v)[0]] = list(k)[0]
return g
def s1(xss):
result = 0
for xs, ys in xss:
for y in ys:
if len(y) in number.values():
result += 1
return result
def s2(xss):
result = 0
for xs, ys in xss:
right = []
second_todo = []
for x in xs:
if len(x) in number.values():
right.append((set(sorted(number_index[number_reverse[len(x)]])), set(sorted(x))))
else:
for i in number_reverse[len(x)]:
second_todo.append((set(number_index[i]), set(sorted(x))))
goal = unify(right, second_todo)
tmp_int = ''
for y in ys:
tmp = [goal[i] for i in y]
tmp_str = reverse_project[tuple(sorted(tmp))]
tmp_int+=tmp_str
result += int(tmp_int)
return result
def main(day, input):
this_dir = os.path.dirname(__file__)
sample = f"{this_dir}/{day}{input}.txt"
with open(sample, "r") as f:
# 0,9 -> 5,9
xss = []
for l in f.readlines():
xs, ys = l.strip().split("|")
xs = [i.strip() for i in xs.strip().split(" ")]
ys = [i.strip() for i in ys.strip().split(" ")]
xss.append((xs, ys))
print(s2(xss))
if __name__ == "__main__":
main('day8', '_sample')
main('day8', '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment