Move python solutions to separate directory
This commit is contained in:
parent
47e97f4533
commit
3903907973
70 changed files with 0 additions and 0 deletions
1
py/2015/01/input.txt
Normal file
1
py/2015/01/input.txt
Normal file
File diff suppressed because one or more lines are too long
36
py/2015/01/solve.py
Normal file
36
py/2015/01/solve.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import sys
|
||||
|
||||
def load_steps(filename):
|
||||
with open(filename, "r") as f:
|
||||
return f.read()[:-1]
|
||||
|
||||
# PART 1
|
||||
|
||||
def count_floors(steps):
|
||||
return steps.count("(") - steps.count(")")
|
||||
|
||||
# PART 2
|
||||
|
||||
def find_basement_char(steps):
|
||||
pos = 0
|
||||
step_nr = 0
|
||||
for step in steps:
|
||||
step_nr += 1
|
||||
if step == "(":
|
||||
pos += 1
|
||||
if step == ")":
|
||||
pos -= 1
|
||||
if pos < 0:
|
||||
return step_nr
|
||||
|
||||
def main(filename):
|
||||
steps = load_steps(filename)
|
||||
print(f"Solutions for {filename}")
|
||||
floor = count_floors(steps)
|
||||
print(f"Part 1: {floor}")
|
||||
step_nr = find_basement_char(steps)
|
||||
print(f"Part 2: {step_nr}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
1000
py/2015/02/input.txt
Normal file
1000
py/2015/02/input.txt
Normal file
File diff suppressed because it is too large
Load diff
44
py/2015/02/solve.py
Normal file
44
py/2015/02/solve.py
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import re
|
||||
import sys
|
||||
|
||||
PACKET_RE = r"(\d+)x(\d+)x(\d+)\n"
|
||||
|
||||
def load_packets(filename):
|
||||
packets = []
|
||||
with open(filename, "r") as f:
|
||||
for line in f:
|
||||
match = re.fullmatch(PACKET_RE, line)
|
||||
a, b, c = match.groups()
|
||||
a, b, c = int(a), int(b), int(c)
|
||||
packets.append((a, b, c))
|
||||
return packets
|
||||
|
||||
# PART 1
|
||||
|
||||
def necessary_area(packet):
|
||||
a, b, c = sorted(packet)
|
||||
return 3*a*b + 2*a*c + 2*b*c
|
||||
|
||||
def total_wrapping_paper(packets):
|
||||
return sum(map(necessary_area, packets))
|
||||
|
||||
# PART 2
|
||||
|
||||
def ribbon_length(packet):
|
||||
a, b, c = sorted(packet)
|
||||
return 2*a + 2*b + a*b*c
|
||||
|
||||
def total_ribbon_length(packets):
|
||||
return sum(map(ribbon_length, packets))
|
||||
|
||||
def main(filename):
|
||||
packets = load_packets(filename)
|
||||
print(f"Solutions for {filename}")
|
||||
total = total_wrapping_paper(packets)
|
||||
print(f"Part 1: {total}")
|
||||
total_2 = total_ribbon_length(packets)
|
||||
print(f"Part 2: {total_2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
1
py/2015/03/input.txt
Normal file
1
py/2015/03/input.txt
Normal file
File diff suppressed because one or more lines are too long
47
py/2015/03/solve.py
Normal file
47
py/2015/03/solve.py
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import sys
|
||||
|
||||
def load_steps(filename):
|
||||
with open(filename, "r") as f:
|
||||
return f.read()[:-1]
|
||||
|
||||
# PART 1
|
||||
|
||||
def find_doubles(steps):
|
||||
x, y = 0, 0
|
||||
houses = {(0, 0)}
|
||||
for step in steps:
|
||||
if step == "^": y += 1
|
||||
elif step == "v": y -= 1
|
||||
elif step == "<": x -= 1
|
||||
elif step == ">": x += 1
|
||||
houses.add((x, y))
|
||||
return houses
|
||||
|
||||
# PART 2
|
||||
|
||||
def split_string(s):
|
||||
s1 = ""
|
||||
s2 = ""
|
||||
while True:
|
||||
if not s: break
|
||||
s1 += s[0]
|
||||
s = s[1:]
|
||||
if not s: break
|
||||
s2 += s[0]
|
||||
s = s[1:]
|
||||
return s1, s2
|
||||
|
||||
def main(filename):
|
||||
steps = load_steps(filename)
|
||||
print(f"Solutions for {filename}")
|
||||
doubles = len(find_doubles(steps))
|
||||
print(f"Part 1: {doubles}")
|
||||
santa, robot = split_string(steps)
|
||||
santa_doubles = find_doubles(santa)
|
||||
robot_doubles = find_doubles(robot)
|
||||
doubles_2 = len(santa_doubles | robot_doubles)
|
||||
print(f"Part 2: {doubles_2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
33
py/2015/04/solve.py
Normal file
33
py/2015/04/solve.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
import hashlib
|
||||
|
||||
# PART 1
|
||||
|
||||
def leading_zeroes(amount, start):
|
||||
n = 1
|
||||
what = "0"*amount
|
||||
while True:
|
||||
text = (start + str(n)).encode("utf-8")
|
||||
h = hashlib.md5(text).hexdigest()
|
||||
if h[:amount] == what:
|
||||
return n
|
||||
|
||||
if n % 100000 == 0:
|
||||
print(f"{n:9} {text} {h}")
|
||||
|
||||
n += 1
|
||||
|
||||
# PART 2
|
||||
|
||||
def main(hashstart):
|
||||
print("Previously calculated:")
|
||||
print("Part 1: 282749")
|
||||
print("Part 2: 9962624")
|
||||
print()
|
||||
print(f"Solutions")
|
||||
n = leading_zeroes(5, hashstart)
|
||||
print(f"Part 1: {n}")
|
||||
n_2 = leading_zeroes(6, hashstart)
|
||||
print(f"Part 2: {n_2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main("yzbqklnj")
|
||||
1000
py/2015/05/input.txt
Normal file
1000
py/2015/05/input.txt
Normal file
File diff suppressed because it is too large
Load diff
54
py/2015/05/solve.py
Normal file
54
py/2015/05/solve.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
import sys
|
||||
|
||||
def load_words(filename):
|
||||
words = []
|
||||
with open(filename, "r") as f:
|
||||
for line in f:
|
||||
words.append(line[:-1])
|
||||
return words
|
||||
|
||||
# PART 1
|
||||
|
||||
def is_vowel(char):
|
||||
return char in "aeiou"
|
||||
|
||||
def has_double_letter(word):
|
||||
# This would look nicer im haskell:
|
||||
return count(zip(word, word[1:]), lambda x: x[0] == x[1]) > 0
|
||||
|
||||
def is_nice(word):
|
||||
if "ab" in word or "cd" in word or "pq" in word or "xy" in word:
|
||||
return False
|
||||
if len(list(filter(is_vowel, word))) < 3:
|
||||
return False
|
||||
return has_double_letter(word)
|
||||
|
||||
def count(what, function):
|
||||
# This would also look nicer in haskell
|
||||
return len(list(filter(function, what)))
|
||||
|
||||
# PART 2
|
||||
|
||||
def has_pair(word):
|
||||
for i in range(len(word)):
|
||||
if word[i:i+2] in word[i+2:]:
|
||||
return True
|
||||
return False
|
||||
|
||||
def has_repeat_letter(word):
|
||||
return count(zip(word, word[2:]), lambda x: x[0] == x[1]) > 0
|
||||
|
||||
def is_nice_2(word):
|
||||
return has_pair(word) and has_repeat_letter(word)
|
||||
|
||||
def main(filename):
|
||||
words = load_words(filename)
|
||||
print(f"Solutions for {filename}")
|
||||
amount = count(words, is_nice)
|
||||
print(f"Part 1: {amount}")
|
||||
amount_2 = count(words, is_nice_2)
|
||||
print(f"Part 2: {amount_2}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
for filename in sys.argv[1:]:
|
||||
main(filename)
|
||||
Loading…
Add table
Add a link
Reference in a new issue