simshadows

Advent of Code 2015 Solutions

All solutions on this page were written by me without reading any hints or solutions.

My solutions aren’t necessarily great, but they did the job and got me the answers.

My suggestion to run these solutions:

$ cat input.txt | ./solution.py

Links to the full challenge specifications are included throughout this document. The original specifications aren’t allowed to be reproduced, so I can’t repost them here.


Day 1 [Spec]

Part 1 Solution

#!/usr/bin/env python3

from sys import stdin
from functools import reduce

print(reduce(lambda ac, v: (ac + (1 if v == "(" else -1)), stdin.readline().strip(), 0))

Part 2 Solution

#!/usr/bin/env python3

from sys import stdin

v = 0
for i, c in enumerate(stdin.readline()):
    v += (1 if c == "(" else -1)
    if v == -1:
        print(i + 1)
        break

Alternative solution:

#!/usr/bin/env python3

from sys import stdin
from itertools import accumulate

it = accumulate(stdin.readline(), (lambda ac, v: ac + (1 if (v == "(") else -1)), initial=0)
print(list(it).index(-1))

Day 2 [Spec]

Part 1 Solution

#!/usr/bin/env python3

from sys import stdin

dims = [[int(x) for x in s.strip().split("x")] for s in stdin.readlines()]
partial_areas = [sorted([a*b, a*c, b*c]) for a, b, c in dims]
print(sum((2 * (x + y + z) + x) for x, y, z in partial_areas))

Part 2 Solution

#!/usr/bin/env python3

from sys import stdin

dims = [[int(x) for x in s.strip().split("x")] for s in stdin.readlines()]
print(sum((2 * min(a+b, a+c, b+c)) + (a * b * c)for a, b, c in dims))

Day 3 [Spec]

Part 1 Solution

#!/usr/bin/env python3

from sys import stdin
from itertools import accumulate

DIRS = {"^": (-1, 0), "v": (1, 0), ">": (0, 1), "<": (0, -1)}

instrs = [DIRS[x] for x in stdin.readline().strip()]
print(len(set(accumulate(instrs, lambda ac, v: (ac[0] + v[0], ac[1] + v[1]), initial=(0, 0)))))

Part 2 Solution

#!/usr/bin/env python3

from sys import stdin
from itertools import accumulate

DIRS = {"^": (-1, 0), "v": (1, 0), ">": (0, 1), "<": (0, -1)}

seen = {(0, 0)}
santa = [0, 0]
robo = [0, 0]
for i, c in enumerate(stdin.readline().strip()):
    agent = santa if (i % 2) else robo
    agent[0] += DIRS[c][0]
    agent[1] += DIRS[c][1]
    seen.add(tuple(agent))
print(len(seen))