diff --git a/.gitignore b/.gitignore index 06c798b..a146bb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ input.txt +.DS_Store diff --git a/2015/day01/day01.py b/2015/day01/day01.py new file mode 100755 index 0000000..32f4675 --- /dev/null +++ b/2015/day01/day01.py @@ -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()) diff --git a/2015/day02/day02.py b/2015/day02/day02.py new file mode 100755 index 0000000..2f0c426 --- /dev/null +++ b/2015/day02/day02.py @@ -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()) \ No newline at end of file diff --git a/2015/day03/day03.py b/2015/day03/day03.py new file mode 100755 index 0000000..58dd54e --- /dev/null +++ b/2015/day03/day03.py @@ -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()) \ No newline at end of file diff --git a/2015/day04/day04.py b/2015/day04/day04.py new file mode 100755 index 0000000..9e22679 --- /dev/null +++ b/2015/day04/day04.py @@ -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()) \ No newline at end of file diff --git a/2015/day05/day05.py b/2015/day05/day05.py new file mode 100755 index 0000000..812bf84 --- /dev/null +++ b/2015/day05/day05.py @@ -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()) \ No newline at end of file diff --git a/2015/day06/day06.py b/2015/day06/day06.py new file mode 100755 index 0000000..8a23a3c --- /dev/null +++ b/2015/day06/day06.py @@ -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()) \ No newline at end of file