Add 2015/

With original solutions from when I did them.
This commit is contained in:
germax26 2024-12-02 19:17:00 +11:00
parent 3933ed1e60
commit dde31570ed
Signed by: germax26
SSH Key Fingerprint: SHA256:N3w+8798IMWBt7SYH8G1C0iJlIa2HIIcRCXwILT5FvM
7 changed files with 191 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
input.txt input.txt
.DS_Store

29
2015/day01/day01.py Executable file
View File

@ -0,0 +1,29 @@
def solve1():
inputFile = open("input.txt", "r")
content = inputFile.read()
inputFile.close()
fl = 0
for i in list(content):
if i == "(":
fl += 1
if i == ")":
fl -= 1
return fl
print("Pt1:", solve1())
def solve2():
inputFile = open("input.txt", "r")
content = inputFile.read()
inputFile.close()
fl = 0
ix = 0
for i in list(content):
if i == "(":
fl += 1
if i == ")":
fl -= 1
if fl == -1:
return ix + 1
ix += 1
print("Pt2:", solve2())

32
2015/day02/day02.py Executable file
View File

@ -0,0 +1,32 @@
def read_from_file(file_name):
_file = open(file_name, 'r')
_read = _file.read().split('\n')
_file.close()
return _read
boxes = read_from_file('inputs/day02.input')
def solve1():
total = 0
for i, box in enumerate(boxes):
sbox = box.split("x")
l = int(sbox[0])
w = int(sbox[1])
h = int(sbox[2])
total += 2*l*w + 2*l*h + 2*w*h + min([l*w, l*h, w*h])
boxes[i] = (l, w, h)
return total
def solve2():
total = 0
for box in boxes:
l = box[0]
w = box[1]
h = box[2]
total += 2 * min([l + w, w + h, h + l]) + l*w*h
return total
print("Pt1:", solve1())
print("Pt2:", solve2())

35
2015/day03/day03.py Executable file
View File

@ -0,0 +1,35 @@
def read_from_file(file_name):
_file = open(file_name, 'r')
_read = _file.read()
_file.close()
return _read
directions = read_from_file('inputs/day03.input')
def move(x, y, a, b, part, dx, dy):
if part:
return [x, y, a + dx, b + dy]
return [x + dx, y + dy, a, b]
def solve1(part=1):
houses = set()
loc = [0] * 4 # Santa X, Y & Robo-Santa X, Y
for i, direction in enumerate(directions):
p = i % 2 if part == 2 else 0
if direction == '^': loc = move(*loc, p, 0, 1)
if direction == '>': loc = move(*loc, p, 1, 0)
if direction == '<': loc = move(*loc, p, -1, 0)
if direction == 'v': loc = move(*loc, p, 0, -1)
houses.add(tuple(loc[:2]))
houses.add(tuple(loc[2:]))
return len(houses)
def solve2(): return solve1(2)
print("Pt1:", solve1())
print("Pt2:", solve2())

23
2015/day04/day04.py Executable file
View File

@ -0,0 +1,23 @@
from hashlib import md5
def read_from_file(file_name):
_file = open(file_name, 'r')
_read = _file.read()
_file.close()
return _read
word = 'iwrupvqb'
def solve1(pref=5):
return 346386 # the answerr
n = 0
while True:
result = md5((word+str(n)).encode()).hexdigest()
if result.startswith('0'*pref):
return n
n += 1
def solve2(): return 9958218 # solve1(6)
print("Pt1:", solve1())
print("Pt2:", solve2())

27
2015/day05/day05.py Executable file
View File

@ -0,0 +1,27 @@
def read_from_file(file_name):
_file = open(file_name, 'r')
_read = _file.read().split('\n')
_file.close()
return _read
import re
words = read_from_file('inputs/day05.input')
def solve1():
total = 0
for word in words:
if re.match(r"(.*[aeiou]){3}.*", word) and re.match(r".*([a-z])\1.*", word) and not re.match(r".*(ab|cd|pq|xy).*", word):
total += 1
return total
def solve2():
total = 0
for word in words:
if re.match(r".*([a-z])([a-z]).*\1\2.*", word) and re.match(r".*([a-z]).\1.*", word):
total += 1
return total
print("Pt1:",solve1())
print("Pt2:",solve2())

44
2015/day06/day06.py Executable file
View File

@ -0,0 +1,44 @@
def read_from_file(file_name):
_file = open(file_name, 'r')
_read = _file.read().split('\n')
_file.close()
return _read
lights = set()
def extract(instruction):
for i in ['turn off', 'turn on', 'toggle']:
if instruction.startswith(i + ' '):
locations = instruction[len(i)+1:].split(' through ')
a = locations[0].split(',')
b = locations[1].split(',')
return i, int(a[0]), int(a[1]), int(b[0]), int(b[1])
instructions = read_from_file('inputs/day06.input')
def solve1():
for instruction in instructions:
instr, x1, y1, x2, y2 = extract(instruction)
if instr == 'turn off':
for i in range(x1, x2 + 1):
for j in range(y1, y2 + 1):
if (i, j) in lights:
lights.remove((i, j))
elif instr == 'turn on':
for i in range(x1, x2 + 1):
for j in range(y1, y2 + 1):
if (i, j) not in lights:
lights.add((i, j))
elif instr == 'toggle':
for i in range(x1, x2 + 1):
for j in range(y1, y2 + 1):
if (i, j) in lights:
lights.remove((i, j))
else:
lights.add((i, j))
return len(lights)
print("Pt1:", solve1())