

Advent of Code 2021 Python Solution: Day 8
source link: https://dev.to/qviper/advent-of-code-2021-python-solution-day-8-76p
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

First part was very easy but for the second part, I took little help from here.
Solution
import numpy as np
from collections import Counter
data,data1 = get_data(day=8)
def permutation(li: list):
all_ps = set()
psl = np.prod(np.linspace(1,len(li), len(li)).astype(int))
while len(all_ps)!=psl:
curr_ps = np.random.choice(range(len(li)), len(li), replace=False)
curr_ps = "".join([li[i] for i in curr_ps])
all_ps.add(curr_ps)
return all_ps
all_ps = permutation("abcdefg")
d = {
"abcefg": 0,
"cf": 1,
"acdeg": 2,
"acdfg": 3,
"bcdf": 4,
"abdfg": 5,
"abdefg": 6,
"acf": 7,
"abcdefg": 8,
"abcdfg": 9,
}
cnts = {2:1, 4:4, 3:7, 7:8}
sol1 = 0
sol2 = 0
txtv = {t.strip().split(": ")[0]:int(t.strip().split(": ")[1]) for t in txt.strip().split("\n")}
for row in data1:
signals, output = row.split("|")
signals = [s.strip() for s in signals.strip().split(" ")]
output = [s.strip() for s in output.strip().split(" ")]
for o in output:
l = len(o)
if len(ls.get(l))==1:
v=ls.get(l)[0]
sol1+=1
for pr in all_ps:
to = str.maketrans("abcdefg", pr)
ts = ["".join(sorted(sig.translate(to))) for sig in signals]
top = ["".join(sorted(op.translate(to))) for op in output]
if all(code in d for code in ts):
sol2 += int("".join(str(d[code]) for code in top))
break
sol1, sol2
Enter fullscreen mode
Exit fullscreen mode
Later I knew there is actually a python generator permutation
inside itertools
.
Recommend
-
6
Advent of Code 2020: Python Solution (25 Part Series) I knew crab can also navigate but I could not find my best solution to today's challenge so I had to head over...
-
6
News Advent of Code 2021 in Kotlin, Day 1
-
12
First part was not much harder to crack but it still took plenty of time. But second part was tricky. Part 1 import numpy as np data,data1 = get_data(day=9) dl = len(data1[0]) dt = np.array([int(d) fo...
-
5
I forgot the time of the challenge but still managed to make it done. Stack came to the aid. Part 1 data,data1=get_data(day=10) table = { ")": 3, "]": 57, "}": 1197, ">": 25137} p...
-
11
Getters and Setters in Javascript: What's the POINT? Toby Parent - Dec 10
-
9
Getters and Setters in Javascript: What's the POINT? Toby Parent - Dec 10
-
8
Higher Order Functions Explained in Plain English Shadid Haque - Dec 6
-
2
iOS clang: error: linker command failed with exit code 1 (use -v to see invocation) ochieng seth - Nov 23
-
10
Our algorithm was: wishListsDiffer. Go to the subject itself for more details CodeSandbox with a possible set of properties you may have come wi...
-
4
Advent of PBT 2021 Our algorithm was: spyOnSanta. Go to the subject itself for more details CodeSandbox with a possible set of propertie...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK