Skip to content

Instantly share code, notes, and snippets.

@naranjja
Created October 5, 2018 06:58
Show Gist options
  • Save naranjja/889eb0fc50c2bce387d72ddab5ef6f08 to your computer and use it in GitHub Desktop.
Save naranjja/889eb0fc50c2bce387d72ddab5ef6f08 to your computer and use it in GitHub Desktop.
My Advent of Code solutions!
def validate(passphrase):
passphrase = passphrase.split(' ')
for word in passphrase:
if passphrase.count(word) > 1: return 0
return 1
def f(x):
y = 0
for passphrase in x:
y += validate(passphrase)
return y
with open('./four.input', 'r') as file:
x = [_.strip() for _ in file.readlines()]
print(f(x))
def f(x):
y = 0
if x.isdigit():
x = [int(_) for _ in str(x)]
for i in range(len(x)):
try:
if (x[i] == x[i + 1]): y += x[i]
except IndexError: pass
if i == len(x) - 1:
if x[0] == x[i]: y += x[i]
return y
with open('./one.input', 'r') as file:
x = file.read().strip()
print('<<<', f(x))
def f(goal):
x = y = dx = 0
dy = -1
step = 0
while True:
step += 1
if goal == step:
return abs(x) + abs(y)
if (x == y) or (x < 0 and x == -y) or (x > 0 and x == 1-y):
dx, dy = -dy, dx
x, y = x+dx, y+dy
print(f(289326))
def part2(goal):
coords = [(1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1)]
x = y = dx = 0
dy = -1
grid = {}
while True:
total = 0
for offset in coords:
ox, oy = offset
if (x+ox, y+oy) in grid:
total += grid[(x+ox, y+oy)]
if total > int(goal):
return total
if (x, y) == (0, 0):
grid[(0, 0)] = 1
else:
grid[(x, y)] = total
if (x == y) or (x < 0 and x == -y) or (x > 0 and x == 1-y):
dx, dy = -dy, dx
x, y = x+dx, y+dy
print(part2(289326))
import pandas as pd
def f(x):
y = 0
for i, row in x.iterrows():
y += max(list(row)) - min(list(row))
return y
x = pd.read_table('./two.input', header=None)
print(f(x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment