Beispiel #1
0
def part1():
    d = [int(x) for x in aodfile.stripped_lines("input/input01.txt")]
    n = 0
    for i in range(1, len(d)):
        if d[i] > d[i-1]:
            n += 1
    print(n)
Beispiel #2
0
def part1():
    t = 0
    for l in aodfile.stripped_lines("input/input08.txt"):
        v = l.split('|')[1]
        for v2 in v.split():
            t += len(v2) == 2 or len(v2) == 7 or len(v2) == 3 or len(v2) == 4
    print(t)
Beispiel #3
0
def run():
    screen = [['.'] * 50 for i in range(6)]
    lines = aodfile.stripped_lines("input/input08.txt")
    for l in lines:
        m = re.match('rect (\d+)x(\d+)', l)
        if m:
            mat.set(screen, 0, 0,
                    int(m.group(2)) - 1,
                    int(m.group(1)) - 1, '#')
            continue
        m = re.match('rotate row y=(\d+) by (\d+)', l)
        if m:
            mat.rotate_row(screen, int(m.group(1)), int(m.group(2)))
            continue
        m = re.match('rotate column x=(\d+) by (\d+)', l)
        if m:
            mat.rotate_col(screen, int(m.group(1)), int(m.group(2)))
            continue
    cnt = 0
    for r in range(len(screen)):
        for c in range(len(screen[0])):
            if screen[r][c] == '#':
                cnt += 1
    print(cnt)
    print(mat.pp(screen))
Beispiel #4
0
def overlap(only_vertical):
    def dir(val):
        if val > 0:
            return -1
        elif val == 0:
            return 0
        return 1
    lines = []
    for l in aodfile.stripped_lines("input/input05.txt"):
        m = re.match('(\d+),(\d+) -> (\d+),(\d+)',l)
        if m:
            lines.append(((int(m.group(1)), int(m.group(2))), (int(m.group(3)), int(m.group(4)))))
    m = defaultdict(int)
    for p1p2 in lines:
        if only_vertical and p1p2[0][0] != p1p2[1][0] and p1p2[0][1] != p1p2[1][1]:
            continue
        ix = dir(p1p2[0][0] - p1p2[1][0])
        iy = dir(p1p2[0][1] - p1p2[1][1])
        p = p1p2[0]
        #print("xxx", p1p2)
        while (True):
            m[p] += 1
            if p == p1p2[1]:
                break
            p = (p[0]+ix, p[1]+iy)
    n = 0
    for k in m.keys():
        if m[k] >= 2:
            n += 1
    print(n)
Beispiel #5
0
def part2():
    d = [int(x) for x in aodfile.stripped_lines("input/input01.txt")]
    n = 0
    for i in range(1, len(d)-2):
        if d[i+1]+d[i+2] > d[i-1]+d[i+1]:
            n += 1
    print(n)
    return
Beispiel #6
0
def part1():
    m = defaultdict(list)
    for l in aodfile.stripped_lines("input/input12.txt"):
        a, b = l.split('-')
        m[a].append(b)
        m[b].append(a)
    print(m)
    print(num_paths(m, 'start', 'end', defaultdict(int)))
Beispiel #7
0
def part1():
    lines = aodfile.stripped_lines("input/input04.txt")
    tot = 0
    for l in lines:
        name, id, checksum = parse_room(l)
        if is_valid(name, checksum):
            tot += id
    print(tot)
Beispiel #8
0
def part1():
    m = []
    for l in aodfile.stripped_lines("input/input11.txt"):
        m.append([MIN] + list(map(int, l)) + [MIN])
    m = [[MIN] * len(m[0])] + m + [[MIN] * len(m[0])]
    nf = 0
    for i in range(100):
        nf += step(m)
    print(nf)
Beispiel #9
0
def part1():
    lines = aodfile.stripped_lines("input/input03.txt")
    vals = []
    valid = 0
    for l in lines:
        t = sorted(map(lambda x: int(x), l.split()))
        if t[0] + t[1] > t[2]:
            valid += 1
    print(valid)
Beispiel #10
0
def solve_code(startx, starty, pad):
    x, y = startx, starty
    lines = aodfile.stripped_lines("input/input02.txt")
    code = ""
    for l in lines:
        l = l.strip()
        for c in l:
            x, y = inc(x, y, c, pad)
        code += pad[y][x]
    return code
Beispiel #11
0
def part1():
    lines = aodfile.stripped_lines("input/input06.txt")
    cnt = [
        list(map(lambda x: [x, 0], ascii_lowercase))
        for x in range(len(lines[0]))
    ]
    for l in lines:
        for i in range(len(l)):
            cnt[i][ord(l[i]) - ord('a')][1] += 1
    sort_dec = map(lambda x: sorted(x, key=lambda y: -y[1]), cnt)
    res = ''.join(map(lambda x: x[0][0], sort_dec))
    print(res)
Beispiel #12
0
def part2():
    m = []
    for l in aodfile.stripped_lines("input/input11.txt"):
        m.append([MIN] + list(map(int, l)) + [MIN])
    m = [[MIN] * len(m[0])] + m + [[MIN] * len(m[0])]
    n = (len(m) - 2) * (len(m[0]) - 2)
    i = 0
    while True:
        i += 1
        if step(m) == n:
            print(i)
            break
Beispiel #13
0
def run_at_minima(func):
    hm = []
    for l in aodfile.stripped_lines("input/input09.txt"):
        hm.append([10] + list(map(int, l)) + [10])
    hm = [[10] * len(hm[0])] + hm + [[10] * len(hm[0])]
    for r in range(1, len(hm) - 1):
        for c in range(1, len(hm[0]) - 1):
            low = True
            for d in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                if hm[r + d[0]][c + d[1]] <= hm[r][c]:
                    low = False
                    break
            if low:
                func(hm, r, c)
Beispiel #14
0
def part1():
    insts = []
    for l in aodfile.stripped_lines("input/input02.txt"):
        v = l.split()
        insts.append((v[0], int(v[1])))
    hp, d = 0, 0
    for i in insts:
        if i[0] == 'forward':
            hp += i[1]
        elif i[0] == 'up':
            d -= i[1]
        elif i[0] == 'down':
            d += i[1]
    print(d*hp)
Beispiel #15
0
def parse_input():
    nums, boards, board = [], [], []
    for l in aodfile.stripped_lines("input/input04.txt"):
        if nums == []:
            nums = list(map(lambda x: int(x), l.split(',')))
            continue
        if len(l) == 0 and len(board) > 0:
            boards.append(board)
            board = []
            continue
        if len(l) > 0:
            board.append(list(map(lambda x: [int(x), False], l.split())))
    if len(board) > 0:
        boards.append(board)
    return nums, boards
Beispiel #16
0
def part2():
    lines = aodfile.stripped_lines("input/input03.txt")
    vals = [[], [], []]
    for l in lines:
        t = list(map(lambda x: int(x), l.split()))
        for i in range(len(t)):
            vals[i].append(t[i])
    v = vals[0] + vals[1] + vals[2]
    i, valid = 0, 0
    while i < len(v):
        t = sorted(v[i:i + 3])
        if t[0] + t[1] > t[2]:
            valid += 1
        i += 3
    print(valid)
Beispiel #17
0
def run(part):
    runq = deque()  # work queue (contains names of bots ready to do work)
    conf = {}  # conf[bot][{0,1}] = [bot to receive {hi, lo} value]
    state = {}  # contains input values. len(state[bot]) = 2 -> bot to runq
    lines = aodfile.stripped_lines("input/input10.txt")

    def send(dst, val):
        if dst not in state.keys():
            state[dst] = []
        state[dst].append(val)
        if len(state[dst]) == 2:
            runq.extend([dst])

    # Parse input, build initial state.
    for l in lines:
        m = re.match('value (\d+) goes to bot (\d+)', l)
        if m:
            bot = 'b' + m.group(2)
            if bot not in state.keys():
                state[bot] = [int(m.group(1))]
            else:
                state[bot].append(int(m.group(1)))
                runq.extend([bot])
            continue
        m = re.match(
            'bot (\d+) gives low to (\w)\w+ (\d+) and high to (\w)\w+ (\d+)',
            l)
        if m:
            bot = 'b' + m.group(1)
            lo, hi = m.group(2) + m.group(3), m.group(4) + m.group(5)
            conf[bot] = [lo, hi]
            continue

    # Simulate the network.
    while len(runq) > 0:
        bot = runq.popleft()
        lo, hi = conf[bot][0], conf[bot][1]
        lov, hiv = min(state[bot]), max(state[bot])
        if part == 1 and lov == 17 and hiv == 61:
            print(bot[1:])
            return
        send(lo, lov)
        send(hi, hiv)
    if part == 2:
        print(state['o0'][0] * state['o1'][0] * state['o2'][0])
    return
Beispiel #18
0
def part1():
    pts, folds = [], []
    xmax, ymax = 0, 0
    for l in aodfile.stripped_lines("input/input13.txt"):
        m = re.match('^(\d+),(\d+)$', l)
        if m:
            pts.append((int(m.group(1)), int(m.group(2))))
            xmax = max(xmax, pts[-1][0])
            ymax = max(ymax, pts[-1][1])
            continue
        m = re.match('fold along (.)=(\d+)', l)
        if m:
            folds.append((m.group(1), int(m.group(2))))
    h, w = ymax+1, xmax+1
    m = [ ['.'] * w  for i in range(h) ]
    for p in pts:
        m[p[1]][p[0]] = '#'
#    ndots = 0
    print('xxx', len(m), len(m[0]))
    for f in folds:
        if f[0] == 'x':
            print(f, len(m))
            ml, mr = [], []
            for r in m:
                assert(len(r) == 1+f[1]*2)
                ml.append(r[:f[1]])
                mr.append(r[f[1]+1:])
            mr = mat.mirror_h(mr)
            for i in range(len(ml)):
                for j in range(len(ml[0])):
                    if ml[i][j] == '#' or mr[i][j] == '#':
                        ml[i][j] = '#'
            m = ml
        elif f[0] == 'y':
            print(f, len(m))
            assert(len(m) == 1+f[1]*2)
            mt, mb = m[:f[1]], m[f[1]+1:]
            mb = mat.mirror_v(mb)
            for i in range(len(mt)):
                for j in range(len(mt[0])):
                    if mt[i][j] == '#' or mb[i][j] == '#':
                        mt[i][j] = '#'
            m = mt
    for r in m:
        print(''.join(r))
Beispiel #19
0
def part1():
    lines = aodfile.stripped_lines("input/input07.txt")
    cnt = 0
    for l in lines:
        abba, nabba = 0, 0
        hyp = False
        for i in range(len(l)-3):
            if l[i] == '[':
                hyp = True
            elif l[i] == ']':
                hyp = False
            elif l[i] == l[i+3] and l[i] != l[i+1] and l[i+1] == l[i+2]:
                if hyp:
                    nabba += 1
                    break
                else:
                    abba += 1
        if abba > 0 and nabba == 0:
            cnt += 1
    print(cnt)
Beispiel #20
0
def part2():
    n = []
    for l in aodfile.stripped_lines("input/input15.txt"):
        n.append(list(map(int, l)))
    h, w = len(n), len(n[0])
    m = [[INF] * (2 + 5 * w) for i in range(2 + 5 * h)]
    for r in range(h):
        for c in range(w):
            m[1 + r][1 + c] = n[r][c]
    for r in range(1, 1 + h):
        for c in range(1 + w, 1 + 5 * w):
            m[r][c] = m[r][c - w] + 1
            if m[r][c] > 9:
                m[r][c] = 1
    for r in range(1 + h, 1 + 5 * h):
        for c in range(1, 1 + 5 * w):
            m[r][c] = m[r - w][c] + 1
            if m[r][c] > 9:
                m[r][c] = 1
    print(shortest_path(m, (1, 1), (len(m[0]) - 2, len(m) - 2)))
Beispiel #21
0
def part2():
    lines = aodfile.stripped_lines("input/input07.txt")
    cnt = 0
    for l in lines:
        aba, bab = {}, {}
        hyp = False
        for i in range(len(l)-2):
            if l[i] == '[':
                hyp = True
            elif l[i] == ']':
                hyp = False
            elif l[i] == l[i+2] and l[i] != l[i+1]:
                if hyp:
                    bab[l[i+1]+l[i]+l[i+1]] = True
                else:
                    aba[l[i:i+3]] = True
        for k in aba.keys():
            if k in bab.keys():
                cnt += 1
                break
    print(cnt)
Beispiel #22
0
def part1():
    template = ""
    rules = defaultdict(str)
    for l in aodfile.stripped_lines("input/input14.txt"):
        m = re.match('^(\w+)$', l)
        if m:
            template = m.group(1)
            continue
        m = re.match('(\w\w) -> (\w)', l)
        if m:
            rules[m.group(1)] = m.group(2)
    for s in range(10):
        template = step(template, rules)
    freq = []
    for a in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        freq.append(template.count(a))
    freq.sort()
    for i in range(len(freq)):
        if freq[i] > 0:
            break
    print(freq[-1] - freq[i])
Beispiel #23
0
def part2():
    template = ""
    rules = defaultdict(str)
    for l in aodfile.stripped_lines("input/input14.txt"):
        m = re.match('^(\w+)$', l)
        if m:
            template = m.group(1)
            continue
        m = re.match('(\w\w) -> (\w)', l)
        if m:
            rules[m.group(1)] = m.group(2)
    freq = defaultdict(int)
    for i in range(len(template) - 1):
        freq[template[i:i + 2]] += 1
    print(freq)
    for s in range(40):
        freq = step2(freq, rules)
        print(freq)
    a = defaultdict(int)
    for s in freq:
        a[s[0]] += freq[s]
    l = min(a, key=lambda x: a[x])
    h = max(a, key=lambda x: a[x])
    print(a[h] - a[l] + 1)
Beispiel #24
0
def part1():
    nums = aodfile.stripped_lines("input/input03.txt")
    g = bitcnt(nums, lambda x: x.count('1') > x.count('0'))
    e = bitcnt(nums, lambda x: x.count('1') < x.count('0'))
    print(g * e)
Beispiel #25
0
def part2():
    lines = aodfile.stripped_lines("input/input04.txt")
    for l in lines:
        name, id, checksum = parse_room(l)
        if is_valid(name, checksum):
            print(rotate(name, id), id)
Beispiel #26
0
def run(rec):
    lines = aodfile.stripped_lines("input/input09.txt")
    sum = 0
    for l in lines:
        sum += expand(l, rec)
    print(sum)
Beispiel #27
0
        pass
    return num


def sum(n1, n2):
    n1, n2 = deepcopy(n1), deepcopy(n2)
    return reduce([n1, n2])


def part1():
    n = nums[0]
    for n2 in nums[1:]:
        n = sum(n, n2)
    print(magnitude(n))


def part2():
    big = 0
    for i in range(len(nums)):
        for j in range(i + 1, len(nums)):
            big = max(big, magnitude(sum(nums[i], nums[j])),
                      magnitude(sum(nums[j], nums[i])))
    print(big)


nums = []
for l in aodfile.stripped_lines("input/input18.txt"):
    nums.append(eval(l))
part1()
part2()
Beispiel #28
0
def part2():
    nums = aodfile.stripped_lines("input/input03.txt")
    o = bitcnt2(nums, lambda x: x.count('1') >= x.count('0'))
    c = bitcnt2(nums, lambda x: x.count('1') < x.count('0'))
    print(o * c)
Beispiel #29
0
from lib import aodfile
import re

def part1(lines):
    nice = 0
    for l in lines:
        if re.match(r'.*(?:ab|cd|pq|xy)', l):
            continue
        if not re.match(r'.*([a-z])\1', l):
            continue
        if not re.match(r'(.*[aeiou]){3,}', l):
            continue
        nice += 1
    return nice

def part2(lines):
    nice = 0
    for l in lines:
        if not re.match(r'.*([a-z][a-z]).*\1', l):
            continue
        if not re.match(r'.*([a-z])[a-z]\1', l):
            continue
        nice += 1
    return nice

fn = "input/input05.txt"
lines = aodfile.stripped_lines(fn)
print(part1(lines))
print(part2(lines))
Beispiel #30
0
def part1():
    insts = []
    for l in aodfile.stripped_lines("input/input03.txt"):
        v = l.split()
        insts.append((v[0], int(v[1])))