Skip to content
Snippets Groups Projects
Verified Commit d65b5ceb authored by mirabilos's avatar mirabilos Committed by mirabilos
Browse files

and done

parent f574a633
No related branches found
No related tags found
No related merge requests found
i.dot
i.png
#!/usr/bin/env python3
import functools
import re
f = {}
z = []
zre = re.compile('^z[0-9]+$')
def makeint(i):
def _res():
return i
return _res
def makeand(a, b):
@functools.cache
def _res():
return f[a]() and f[b]()
return _res
def makeor(a, b):
@functools.cache
def _res():
return f[a]() or f[b]()
return _res
def makexor(a, b):
@functools.cache
def _res():
return f[a]() ^ f[b]()
return _res
makes = { 'AND': makeand, 'OR': makeor, 'XOR': makexor }
with open("inputfile", "r") as F:
while True:
s = F.readline().strip()
if s == '':
break
s = s.split(': ')
f[s[0]] = makeint(int(s[1]))
while True:
s = F.readline().strip()
if s == '':
break
s = s.split()
f[s[4]] = makes[s[1]](s[0], s[2])
m = zre.fullmatch(s[4])
if m:
z.append(s[4])
#print(f)
#for k, v in f.items():
# print(k, v())
#print(z)
r = 0
for k in sorted(z, reverse=True):
r = (r << 1) | f[k]()
print(r)
#!/bin/mksh
<i sed \
-e 's/> vss/SWPA1/' \
-e 's/> z14/SWPA2/' \
-e 's/> kdh/SWPB1/' \
-e 's/> hjf/SWPB2/' \
-e 's/> z31/SWPC1/' \
-e 's/> kpp/SWPC2/' \
-e 's/> z35/SWPD1/' \
-e 's/> sgj/SWPD2/' \
| sed \
-e 's/SWPA1/> z14/' \
-e 's/SWPA2/> vss/' \
-e 's/SWPB1/> hjf/' \
-e 's/SWPB2/> kdh/' \
-e 's/SWPC1/> kpp/' \
-e 's/SWPC2/> z31/' \
-e 's/SWPD1/> sgj/' \
-e 's/SWPD2/> z35/' \
>inputfile
{
echo 'strict digraph "adder" {'
fgrep -- '->' inputfile | while read a op b dash r; do
if [[ $a = y* ]]; then
x=$a
a=$b
b=$x
fi
print -r -- " \"$a\" -> \"$a $op $b\";"
print -r -- " \"$b\" -> \"$a $op $b\";"
print -r -- " \"$a $op $b\" -> \"$r\";"
done
print '}'
} >i.dot
dot -Tpng i.dot >i.png~ &
{
echo obase=2
./s1.py
} | bc
wait
mv i.png~ i.png
#!/usr/bin/env python3
import re
rlck = re.compile('^(#*)\\.*$')
rkey = re.compile('^\\.*(#*)$')
lck = []
key = []
def clck(x):
return len(rlck.fullmatch(''.join(x)).group(1))
def ckey(x):
return len(rkey.fullmatch(''.join(x)).group(1))
with open("inputfile", "r") as F:
while True:
s = F.readline().strip()
a = list(F.readline().strip())
b = list(F.readline().strip())
c = list(F.readline().strip())
d = list(F.readline().strip())
e = list(F.readline().strip())
f = F.readline().strip()
if s == '#####' and f == '.....':
lck.append(tuple(map(clck, zip(a, b, c, d, e))))
elif s == '.....' and f == '#####':
key.append(tuple(map(ckey, zip(a, b, c, d, e))))
else:
raise Exception("weird %s" % repr([s,a,b,c,d,e,f]))
if F.readline() == '':
break
def add(t):
a, b = t
return a + b
count = 0
for l in lck:
for k in key:
s = tuple(map(add, zip(l, k)))
if len(tuple(filter(lambda x: x > 5, s))) == 0:
count += 1
#print(l, k, s, len(tuple(filter(lambda x: x > 5, s))) == 0 and 1 or 0)
print(count)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment