clssn

6572938?v=4

clssn
: clssn

About me

Nothing here yet. Update your profile at /profiles/clssn.adoc

Day 00: python

Hello AOC2019

Python

Let’s see whether I’ll find the time for AOC this year. Saying 'hello' is definitely possible though :-)

Run python solution.py

Unresolved directive in ../../../../../../day00/python/clssn/README.adoc - include::solution.pyp[]

Day 01: python

Day 1 Solution in Python

Disclaimer

The code of this solution is not perfect in any way but it seems to do its job. This is due to the fact that I don’t want to entirely miss the AOC this year again due to family implications but instead participate in an efficient way.

So don’t expect fancy interface descriptions and nicely edited readmes unless I need it myself to handle the complexity of the puzzle :-)

total_fuel_mass = 0
total_fuel_mass_recursive = 0

def calc_fuel(m):
    return int(m)/3 - 2

def calc_fuel_recursive(m):
    fuel_m = calc_fuel(m)
    return (fuel_m + calc_fuel_recursive(fuel_m)) if fuel_m >= 0 else 0

with open("input.txt", "r") as f:
    for line in f:
        total_fuel_mass += calc_fuel(line)
        total_fuel_mass_recursive += calc_fuel_recursive(line)

print("Fuel first star:  {}".format(total_fuel_mass))
print("Fuel second star: {}".format(total_fuel_mass_recursive))

Day 02: python

Day 2 Solution in Python

Disclaimer

The code of this solution is not perfect in any way but it seems to do its job. This is due to the fact that I don’t want to entirely miss the AOC this year again due to family implications but instead participate in an efficient way.

So don’t expect fancy interface descriptions and nicely edited readmes unless I need it myself to handle the complexity of the puzzle :-)

input = []
with open('input.txt') as f:
    for line in f:
        input += [ int(x) for x in line.strip().split(',') ]

op = { 1: lambda a,b: a+b, 2: lambda a,b: a*b}

def solve(m, noun, verb):
    m[1] = noun
    m[2] = verb

    pc = 0
    while True:
        o, a, b, d = m[pc:pc+4]
        if o == 99:
            break
        r = op[o](m[a], m[b])
        m[d] = r
        pc += 4
    return m[0]

print("Result first star: {}".format(solve(list(input),12,2)))

for noun in range(0, 99):
    for verb in range(0, 99):
        try:
            r = solve(list(input), noun, verb)
        except(KeyError):
            pass
        if r == 19690720:
            print("Result second star: {}".format(noun * 100 + verb))

Day 03: python

Day 3 Solution in Python

Disclaimer

The code of this solution is not perfect in any way but it seems to do its job. This is due to the fact that I don’t want to entirely miss the AOC this year again due to family implications but instead participate in an efficient way.

So don’t expect fancy interface descriptions and nicely edited readmes unless I need it myself to handle the complexity of the puzzle :-)

Day 03 Notes

At first I didn’t read the instructions thoroughly and thought the input was about checkerboard style coordinates with letters for x and numbers for y axis. I spent 1 hour coding the totally wrong solution until I asked myself where to find the central port (since both paths seemingly started from different locations). Re-reading the instructions I got aware of the fact that only letters R, L, U and D are used…​

with open('input.txt') as f:
    paths = [ line.strip().split(',') for line in f ]

v = {'L': (-1, 0), 'R': (1, 0), 'U': (0, 1), 'D': (0, -1)}

def add(p0, p1):
    return (p0[0]+p1[0], p0[1]+p1[1])

def sample_path(path):
    p = (0, 0)
    locations = set()
    dists = {}
    length = 0
    while len(path):
        cmd = path.pop(0)
        d, n = cmd[0], int(cmd[1:])
        while n:
            length += 1
            p = add(p, v[d])
            locations.add(p)
            n -= 1
            if p not in dists:
                dists[p] = length
    return locations, dists

def manhattan(intersects):
    norm = lambda q: abs(q[0])+abs(q[1])
    return norm(min(intersects, key=norm))

def wire(intersects, dists_0, dists_1):
    norm = lambda x: dists_0[x] + dists_1[x]
    return norm(min(intersects, key=norm))

locs_0, dists_0 = sample_path(paths[0])
locs_1, dists_1 = sample_path(paths[1])
intersects = locs_0 & locs_1

print("Star1: {}".format(manhattan(intersects)))
print("Star2: {}".format(wire(intersects, dists_0, dists_1)))

Day 04: python

Day 4 Solution in Python

Disclaimer

The code of this solution is not perfect in any way but it seems to do its job. This is due to the fact that I don’t want to entirely miss the AOC this year again due to family implications but instead participate in an efficient way.

So don’t expect fancy interface descriptions and nicely edited readmes unless I need it myself to handle the complexity of the puzzle :-)

import re

def check_rules(x, check_star2):
    s = str(x)
    def monotonous(s):
        for i in range(0, len(s)-1):
            if s[i] > s[i+1]:
                return False
        return True

    def has_twin(s):
        return 2 in count_series(s)

    def noncanonical_series(s):
        return count_series(s) - {1}

    def count_series(x):
        return set([ len(x.groups()[0]) for x in re.finditer(r'((.)\2*)',x) ])

    return monotonous(s) and noncanonical_series(s) and (not check_star2 or has_twin(s))

def check_range(begin, end, star2):
    return [ x for x in range(begin, end) if check_rules(x, star2) ]

star1 = check_range(193651, 649729, False)
print("Result first star:  {}".format(len(star1)))

star2 = check_range(193651, 649729, True)
print("Result second star: {}".format(len(star2)))

Day 05: python

Day 5 Solution in Python

Disclaimer

The code of this solution is not perfect in any way but it seems to do its job. This is due to the fact that I don’t want to entirely miss the AOC this year again due to family implications but instead participate in an efficient way.

So don’t expect fancy interface descriptions and nicely edited readmes unless I need it myself to handle the complexity of the puzzle :-)

Day 05 Notes

This one drove me crazy. I didn’t want to spend the effort of thorough refactoring and test, but I ended up spending hours for trying around.

With tests in place: Piece of cake! :-|

class IntcodeComputer:
    def __init__(self, mem):
        self.m = mem
        self.pc = 0

    def compute(self, input=[]):
        output=[]
        class Param:
            def __init__(self, m, addr, mode):
                self.m = m
                self.addr = addr
                self.mode = mode

            def get(self):
                if self.mode:
                    return self.m[self.addr]
                else:
                    return self.m[self.m[self.addr]]

            def set(self, v):
                if self.mode:
                    self.m[self.addr] = v
                else:
                    self.m[self.m[self.addr]] = v

        def add(a, b, r):
            r.set(a.get() + b.get())

        def mul(a, b, r):
            r.set(a.get() * b.get())

        def inp(a):
            a.set(input.pop(0))

        def outp(a):
            output.append(a.get())

        def jnz(a, b):
            if a.get():
                self.pc = b.get()

        def jz(a, b):
            if not a.get():
                self.pc = b.get()

        def lt(a, b, r):
            r.set(1 if a.get() < b.get() else 0)

        def eq(a, b, r):
            r.set(1 if a.get() == b.get() else 0)

        op = { 1: add, 2: mul, 3: inp, 4: outp, 5: jnz, 6: jz, 7: lt, 8: eq}
        while True:
            pc = self.pc
            instr = self.m[pc]
            o = instr % 100
            if o == 99:
                break
            n = op[o].__code__.co_argcount
            mode = [ instr//(10**(i+2))%10 for i in range(0, n) ]
            par = [ Param(self.m, pc+1+i, mode[i]) for i in range(0, n)]
            op[o](*par)
            if pc == self.pc:
                self.pc += 1+n
        return output

if __name__ == "__main__":

    input = []
    with open('input.txt') as f:
        for line in f:
            input += [ int(x) for x in line.strip().split(',') ]

    def comp(inp):
        c = IntcodeComputer(list(input))
        return c.compute(inp)

    print("Result first star: {}".format(comp([1])))
    print("Result second star: {}".format(comp([5])))

Day 06: python

Day 6 Solution in Python

Disclaimer

The code of this solution is not perfect in any way but it seems to do its job. This is due to the fact that I don’t want to entirely miss the AOC this year again due to family implications but instead participate in an efficient way.

So don’t expect fancy interface descriptions and nicely edited readmes unless I need it myself to handle the complexity of the puzzle :-)

orbit_map = {}
with open('input.txt') as f:
    for line in f:
        vk = line.strip().split(')')
        orbit_map[vk[1]] = vk[0]

def orbiters(omap, x):
    return { i[0] for i in omap.items() if i[1] == x }

def count_direct_indirect_orbits(orbit_map):
    count = 0
    visit = [('COM', 1)]
    while len(visit):
        o, depth = visit.pop()
        l = list(orbiters(orbit_map, o))
        count += len(l) * depth
        visit += [ (x, depth+1) for x in l ]
    return count

def count_hops_to_santa(orbit_map):
    def route_to_com(start):
        route = [start]
        while route[-1] != 'COM':
            route.append(orbit_map[route[-1]])
        return route
    you_to_com = route_to_com('YOU')
    san_to_com = route_to_com('SAN')
    for i, x in enumerate(you_to_com):
        if x in san_to_com:
            return san_to_com.index(x) + i - 2

print('Star1 count: {}'.format(count_direct_indirect_orbits(orbit_map)))
print('Star2 count: {}'.format(count_hops_to_santa(orbit_map)))