Skip to content

Instantly share code, notes, and snippets.

@ZhangYiJiang
Last active December 9, 2015 14:20
Show Gist options
  • Save ZhangYiJiang/69265cc35d96f374a859 to your computer and use it in GitHub Desktop.
Save ZhangYiJiang/69265cc35d96f374a859 to your computer and use it in GitHub Desktop.
with open('1.in') as f:
data = f.read()
print('First half')
print(data.count('(') - data.count(')'))
i = 0
floor = 0
while floor >= 0:
if data[i] == '(':
floor += 1
else:
floor -= 1
i += 1
print('Second half')
print(i)
with open('2.in') as f:
data = f.read().split('\n')[:-1]
area = 0
peri = 0
for l in data:
h, w, l = [int(i) for i in l.split('x')]
a, b, c = l*w, w*h, h*l
area += 2*(a+b+c) + min(a, b, c)
peri += 2*(h+w+l - max(h,w,l)) + h*w*l
print('First Half')
print(area)
print('Second Half')
print(peri)
with open('3.in') as f:
data = f.read()
houses = set()
houses.add((0, 0))
x = 0
y = 0
for c in data:
if c == '^':
y += 1
if c == 'v':
y -= 1
if c == '>':
x += 1
if c == '<':
x -= 1
houses.add((x, y))
print('First half')
print(len(houses))
houses = set()
houses.add((0, 0))
x = [0, 0]
y = [0, 0]
for i, c in enumerate(data):
p = i%2
if c == '^':
y[p] += 1
if c == 'v':
y[p] -= 1
if c == '>':
x[p] += 1
if c == '<':
x[p] -= 1
houses.add((x[p], y[p]))
print('Second half')
print(len(houses))
from hashlib import md5
input = 'yzbqklnj'
i = 0
hash = ''
while not hash.startswith('00000'):
i += 1
hash = md5(input + str(i)).hexdigest()
print('First half')
print(i)
while not hash.startswith('000000'):
i += 1
hash = md5(input + str(i)).hexdigest()
print('Second half')
print(i)
with open('5.in') as f:
data = f.read().split('\n')
nice = 0
for w in data:
if 'ab' in w or 'cd' in w or 'pq' in w or 'xy' in w:
continue
if sum([w.count(c) for c in 'aeiou']) < 3:
continue
for i in range(1, len(w)):
if w[i] == w[i-1]:
nice += 1
break
print('First half')
print(nice)
nice = 0
for w in data:
for i in range(1, len(w)):
if w.count(w[i-1] + w[i]) > 1:
break
else:
continue
for i in range(2, len(w)):
if w[i] == w[i-2]:
break
else:
continue
nice += 1
print('Second half')
print(nice)
with open('7.in') as f:
instructions = f.read().split('\n')[:-1]
operations = {
'AND': lambda a, b: a & b,
'OR': lambda a, b: a | b,
'NOT': lambda a, b: ~b,
'RSHIFT': lambda a, b: a >> b,
'LSHIFT': lambda a, b: a << b
}
depedency = {}
memo = {}
for inst in instructions:
left, right = inst.split(' -> ')
depedency[right] = left
def resolve(var):
if var in memo:
return memo[var]
if var == '':
return var
if var.isdigit():
return int(var)
dependents = depedency[var]
if dependents.isdigit():
return int(dependents)
for op, func in operations.items():
if op in dependents:
print(dependents)
operands = [resolve(a.strip()) for a in dependents.split(op)]
memo[var] = func(*operands)
return memo[var]
return resolve(dependents)
print('Part 1')
print(resolve('a'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment