Exemple #1
0
def get_data():
    lines = readlines(rpath('day15.txt', 'aoc2021'))
    graph = {}
    for y, line in enumerate(lines):
        for x, r in enumerate(line):
            graph[x, y] = int(r)
    return graph, len(line)
def get_data():
  data = readlines(rpath('day13.txt', 'aoc2021'))
  pic = [line.split(',') for line in data if ',' in line]
  pic = [(int(x), int(y)) for x, y in pic]
  folds = [line.replace('fold along ', '').split('=')
    for line in data if line.startswith('fold')]
  folds = [(x, int(y)) for x, y in folds]
  return pic, folds
def get_data():
  data = readlines(rpath('day20.txt', 'aoc2021'), sep='\n\n')
  enhancement, image = data
  image = image.strip().split('\n')
  imgsz = len(image)
  image = {
    (x, y): c
    for y, line in enumerate(image)
    for x, c in enumerate(line)}
  return image, enhancement, imgsz
def get_data():
  data = readlines(rpath('day19.txt', 'aoc2021'), sep='\n\n')
  scanners = []
  for sc in data:
    lines = sc.split('\n')
    name, vecs = lines[0], lines[1:]
    name = name.replace('--- scanner ', 'Scn').replace(' ---', '')
    vecs = [Vector(*[int(q) for q in l.split(',')]) for l in vecs]
    scanners.append(Scanner(name, vecs))
  relations = get_relations(scanners)
  return scanners, relations
def get_data():
    data = readlines(rpath('day12.txt', 'aoc2021'))
    graph = {'upper': set()}
    for line in data:
        src, dst = line.split('-')
        if src.isupper():
            graph['upper'].add(src)
        if dst.isupper():
            graph['upper'].add(dst)

        if src in graph:
            graph[src].append(dst)
        else:
            graph[src] = [dst]
        if dst in graph:
            graph[dst].append(src)
        else:
            graph[dst] = [src]
    return graph
Exemple #6
0
def get_data():
  return readlines(rpath('day07.txt', 'aoc2021'), conv=int, sep=',')
def part2(data):
    signals = []
    for perm in permutations((5, 6, 7, 8, 9)):
        cpus = [
            Intcode(debug=False, wfi_mode=True, inputs=[p])
            for _, p in enumerate(perm)
        ]
        for cpu in cpus:
            cpu.load_program(data)

        out = 0
        i = 0
        while True:
            cpu = cpus[i % 5]
            cpu.feed_inputs(out)
            cpu.run()
            out = cpu.outputs[-1]
            if all([cpu.halt for cpu in cpus]):
                break
            i += 1
        signals.append(cpu.outputs[-1])
    return max(signals)


if __name__ == '__main__':
    data = readlines(rpath('day07.txt', 'aoc2019'), sep=',', conv=int)

    print(part1(data))
    print(part2(data))
Exemple #8
0

def get_loop_size(sub, target):
    i = 1
    val = 1
    while True:
        val = transform(sub, val)
        if val == target:
            return i
        i += 1


def get_encryption_key(public, loop_sz):
    return transform(public, 1, n=loop_sz)


def part1(cpub, dpub):
    card_loopsz = get_loop_size(7, cpub)
    door_loopsz = get_loop_size(7, dpub)
    card_key = get_encryption_key(cpub, door_loopsz)
    door_key = get_encryption_key(dpub, card_loopsz)

    assert card_key == door_key

    return card_key


if __name__ == '__main__':
    card_pub, door_pub = readlines(rpath('day25.txt', 'aoc2020'), conv=int)
    print(part1(card_pub, door_pub))
                        print('<', end='')

                elif (x, y) in self.hull:
                    print('#' if self.hull[(x, y)] else black, end='')
                else:
                    print(' ', end='')
            print()


def part1(data):
    cpu = Intcode(wfi_mode=True, debug=False)
    cpu.load_program(data)
    robot = Robot(cpu)
    robot.start(0)
    return len(robot.hull)


def part2(data):
    cpu = Intcode(wfi_mode=True, debug=False)
    cpu.load_program(data)
    robot = Robot(cpu)
    robot.start(1)
    robot.print_hull(black=' ')


if __name__ == '__main__':
    data = readraw(rpath('day11.txt', 'aoc2019'), sep=',', conv=int)

    print(part1(data))
    part2(data)
Exemple #10
0
def get_layers(data, w, h):
    return [data[i:i + w * h] for i in range(0, len(data), w * h)]


def part1(layers):
    min0layer = min(layers, key=lambda m: m.count('0'))
    return min0layer.count('1') * min0layer.count('2')


def part2(layers, w, h):
    img = layers[0]
    for layer in layers[1:]:
        new_img = ''
        for i, l in zip(img, layer):
            new_img += l if i == '2' else i
        img = new_img

    img = [img[i:i + w] for i in range(0, len(img), w)]
    for line in img:
        print(line.replace('0', ' ').replace('1', '#'))


if __name__ == '__main__':
    data = readlines(rpath('day08.txt', 'aoc2019'))[0]
    w, h = 25, 6
    layers = get_layers(data, w, h)

    print(part1(layers))
    part2(layers, w, h)
    if mono:
      valid.append(i)

  return valid

def part1(data):
  return len(valid1(data))

def part2(data):
  valid = valid1(data)
  new = []
  for n in valid:
    strn = str(n)
    match = re.search(rx, strn)
    if match:
      strpd = strn.replace(match.group(0), '')
      m2 = re.search(rx, strpd)
      if not m2 and len(strpd) in (2,3) and len(set(strpd)) != len(strpd):
        new.append(n)

    else:
      new.append(n)
  return len(new)


if __name__ == '__main__':
  data = readlines(rpath('day04.txt', 'aoc2019'),
                   conv=lambda m: [int(x) for x in m.split('-')])[0]
  print(part1(data))
  print(part2(data))
Exemple #12
0
def get_data():
  data = readraw(rpath('day16.txt', 'aoc2021')).strip()
  data = ''.join([f'{int(c, 16):04b}' for c in data]).rstrip('0')
  return data
        if first == string[0]:
            return match(ruleset, rule[1:], string[1:])
    elif isinstance(first, list):
        matches = []
        for subrule in first:
            matches.append(match(ruleset, subrule + rule[1:], string))
        return any(matches)


def part1(ruleset, strings):
    matches = [match(ruleset, [0], string) for string in strings]
    return len([m for m in matches if m is True])


def part2(ruleset, strings):
    ruleset[8] = [[42], [42, 8]]
    ruleset[11] = [[42, 31], [42, 11, 31]]
    matches = [match(ruleset, [0], string) for string in strings]
    return len([m for m in matches if m is True])


if __name__ == '__main__':
    data = readlines(rpath('day19.txt', 'aoc2020'), sep='\n\n')

    rules, strings = data[0], data[1]
    strings = strings.split('\n')
    ruleset = parse_rules(rules)

    print(part1(ruleset, strings))
    print(part2(ruleset, strings))
Exemple #14
0
    # Now if a given field is valid in only one column across all tickets,
    # then that must be it, we save that information and remove the column
    # marked as valid from remaining fields
    field_cols = {}
    while len(field_cols) != field_cnt:
        field, val = [(f, v) for f, v in rng_cols.items()
                      if v.count('1') == 1][0]
        field_cols[field] = val.index('1')
        rng_cols = {
            rname: f'{int(c, 2) & ~int(val, 2):020b}'
            for rname, c in rng_cols.items()
        }

    my_fields = {
        k: v
        for k, v in field_cols.items() if k.startswith('departure')
    }
    res = [mytick[i] for i in my_fields.values()]
    return reduce(mul, res)


if __name__ == '__main__':
    data = [
        d.split('\n')
        for d in readlines(rpath('day16.txt', 'aoc2020'), sep='\n\n')
    ]
    ranges, mytick, tickets, allrng = parse(data)

    print(part1(ranges, tickets, allrng))
    print(part2(ranges, tickets, allrng, mytick))
Exemple #15
0
def get_data():
    return readlines(rpath('day18.txt', 'aoc2021'),
                     conv=lambda line: parse(eval(line)))
def part1(data):
    all_ings_list = sum([d[0] for d in data], [])
    all_ings = set(all_ings_list)
    unsafe_ings = set.union(*get_mapping(data).values())
    safe_ings = all_ings - unsafe_ings
    return sum([all_ings_list.count(i) for i in safe_ings])


def part2(data):
    mapping = get_mapping(data)

    identified = {}
    while len(identified) != len(mapping.keys()):
        for agen, ings in mapping.items():
            if len(ings) == 1:
                ing = ings.pop()
                identified[agen] = ing
                for a, i in mapping.items():
                    mapping[a] -= {ing}
                break

    return ','.join(
        [x[1] for x in sorted(identified.items(), key=lambda m: m[0])])


if __name__ == '__main__':
    data = readlines(rpath('day21.txt', 'aoc2020'), conv=parse)

    print(part1(data))
    print(part2(data))
def part1(p1p, p2p):
  pmin = min(set(p1p) & set(p2p), key=lambda p: dist(p))
  return dist(pmin)

def part2(p1p, p2p):
  isect = set(p1p) & set(p2p) - set([(0,0)])
  p1d, p2d = {}, {}

  for i, p in enumerate(p1p):
    if p in isect:
      p1d[p] = i

  for i, p in enumerate(p2p):
    if p in isect:
      p2d[p] = i

  pd = {p: d+p2d[p] for p, d in p1d.items()}

  return min(pd.values())


if __name__ == '__main__':
  data = readlines(rpath('day03.txt', 'aoc2019'), conv=lambda l: l.split(','))
  p1, p2 = data
  p1p = get_points(p1)
  p2p = get_points(p2)

  print(part1(p1p, p2p))
  print(part2(p1p, p2p))
        with open('day20.png', 'wb') as f:
            f.write(png)
        subprocess.call(('open', 'day20.png'))


def part1(data):
    maze = Maze(data)
    #maze.draw()
    maze.graphviz(with_dead_ends=False)

    aa, zz = maze.portals['AA'], maze.portals['ZZ']
    print(f'AA={aa}')
    paths = maze.find_paths([maze.edges_for_vertex(aa)], zz)
    #print(maze.edges_for_vertex((17, 8)))
    #print(maze.edges)
    print(
        min(
            sum([e.dist for e in path]) for path in paths
            if path[-1].dst == zz))


def part2(data):
    pass


if __name__ == '__main__':
    with open(rpath('day20.txt', 'aoc2019')) as f:
        data = f.read().split('\n')
    print(part1(data))
    print(part2(data))
Exemple #19
0
                new_active.append((x, y, z, w))
        else:
            if neighcnt == 3:
                new_active.append((x, y, z, w))
    return new_active


def part1(data):
    active = [(x, y, 0) for y, line in enumerate(data)
              for x, val in enumerate(line) if val == '#']

    a = active
    for i in range(6):
        a = iterate3d(a)
    return len(a)


def part2(data):
    active = [(x, y, 0, 0) for y, line in enumerate(data)
              for x, val in enumerate(line) if val == '#']
    a = active
    for i in range(6):
        a = iterate4d(a)
    return len(a)


if __name__ == "__main__":
    data = readlines(rpath('day17.txt', 'aoc2020'))
    print(part1(data))
    print(part2(data))
Exemple #20
0
                firstdata = datad[t2]
                firstlbl = t2
                break

    img = get_img(lines)
    img = join_img(img)

    imgc = img
    for i in range(4):
        hashcnt, monstercnt = find_monsters(imgc)
        if monstercnt:
            return hashcnt
        imgc = rotated(imgc)

    imgc = f(imgc)
    for i in range(4):
        hashcnt, monstercnt = find_monsters(imgc)
        if monstercnt:
            return hashcnt
        imgc = rotated(imgc)


if __name__ == '__main__':
    data = readlines(rpath('day20.txt', 'aoc2020'), sep='\n\n', conv=parse)
    datad = dict(data)
    conns = all_conns(data)
    corners = get_corners(conns)

    print(part1(corners))
    print(part2(data, datad, conns, corners))
    num = ls[i]
    for n, k in permutations(range(plen + 1), 2):
        if n != k and ls[i - n] + ls[i - k] == num:
            return True
    return False


def part1(data):
    for i, n in enumerate(data):
        if i < 25:
            continue

        if not sum_of_previous(data, i, plen=25):
            return n


def part2(ls, n):
    for plen in range(2, len(ls)):
        for i in range(len(ls) - plen):
            chunk = ls[i:i + plen]
            if sum(chunk) == n:
                return min(chunk) + max(chunk)
    return None


if __name__ == '__main__':
    data = readlines(rpath('day09.txt', 'aoc2020'), conv=int)
    part1_res = part1(data)
    print(part1_res)
    print(part2(data, part1_res))
Exemple #22
0
def get_data():
    data = readlines(rpath('day04.txt', 'aoc2021'), sep='\n\n')
    nums, boards = data[0], data[1:]
    nums = [int(n) for n in nums.split(',')]
    boards = [[int(n) for n in re.split(r'\s+', b)] for b in boards]
    return nums, boards
        val = 0
        patgen = pattern(i + 1)
        for _ in range(rep):
            for digit in signal:
                val += next(patgen) * digit
        out.append(abs(val) % 10)

    return out


def part1(data):
    signal = data
    for i in range(100):
        signal = evaluate(signal)
    return ''.join((str(x) for x in signal[:8]))


def part2(data):
    signal = data
    for i in range(1):
        signal = evaluate(signal, rep=10000)
    return ''.join((str(x) for x in signal[:8]))


if __name__ == '__main__':
    data = readraw(rpath('day16.txt', 'aoc2019'))
    data = [int(c) for c in data.strip()]
    #data = [int(c) for c in '12345678']
    print(part1(data))
    #print(part2(data))
Exemple #24
0
def part2(asteroids):
    best = best_monitoring(asteroids)[1]
    dets = [
        Detection(best, other, slope(best, other), distance(best, other))
        for other in asteroids
    ]
    dets.sort(key=lambda m: -m.dist)
    slopes = set([det.slope for det in dets])

    for i, slp in enumerate(cycle(slopesort(slopes))):
        others = [det for det in dets if det.slope == slp]
        if others:
            try:
                closest = others.pop()
                dets.remove(closest)
            except IndexError:
                pass

        if i == 199:
            x, y = closest.dst
            return 100 * x + y


if __name__ == '__main__':
    data = readlines(rpath('day10.txt', 'aoc2019'))
    asteroids = [(x, y) for y, line in enumerate(data)
                 for x, char in enumerate(line) if char == '#']
    print(part1(asteroids))
    print(part2(asteroids))
def part1(map_):
    dst = [pos for pos, status in map_.items()
           if status == Droid.STATUS_OXY][0]
    path = find_path(map_, [(0, 0)], dst)
    return len(path) - 1


def part2(map_):
    oxy = [pos for pos, status in map_.items()
           if status == Droid.STATUS_OXY][0]
    adjmap = {**map_}

    minutes = 0
    while any([state == Droid.STATUS_OK for _, state in adjmap.items()]):
        for oxy in [
                pos for pos, status in adjmap.items()
                if status == Droid.STATUS_OXY
        ]:
            for adj in get_adjacent(map_, *oxy):
                adjmap[adj] = Droid.STATUS_OXY
        minutes += 1

    return minutes


if __name__ == '__main__':
    data = readlines(rpath('day15.txt', 'aoc2019'), conv=int, sep=',')
    map_ = get_map(data)
    print(part1(map_))
    print(part2(map_))
Exemple #26
0
def get_data():
    data = readlines(rpath('day17.txt', 'aoc2021'))[0]
    data = re.findall(r'(x|y)=(-?\d+\.\.-?\d+)', data)
    x, y = [(int(x), int(y)) for x, y in [d[1].split('..') for d in data]]
    return x, y
Exemple #27
0
    else:
      pos[1] = nearest_pos[1]
      pos[0] = nearest_pos[0] - diff

  else:
    base = nearest // 2
    nearest_pos = (base, base)
    square = squares[nearest]
    diff = val - square
    if diff > 0:
      pos[0] = nearest_pos[0] + 1
      pos[1] = nearest_pos[1] - diff + 1
    else:
      pos[0] = nearest_pos[0] + diff
      pos[1] = nearest_pos[1]

  return pos

def part1(data):
  pos = numpos(data)
  return abs(pos[0]) + abs(pos[1])

def part2(data):
  pass


if __name__ == '__main__':
  data = readraw(rpath('day03.txt', 'aoc2017'), conv=int)
  print(part1(data))
  print(part2(data))
    ls = Node.make_nodes(data)

    for i in range(100):
        cups = ls.take3()
        ls.put(cups, ls.dst(cups))

    ls.active = ls.get(1)
    return ''.join([str(n.val) for n in ls.nodes()[1:]])


def part2(data):
    data += list(range(max(data) + 1, 1_000_001))
    assert len(data) == 1_000_000
    ls = Node.make_nodes(data)

    for i in range(10_000_000):
        cups = ls.take3()
        ls.put(cups, ls.dst(cups))

    one1 = ls.get(1).next
    one2 = one1.next
    return one1.val * one2.val


if __name__ == '__main__':
    data = readlines(rpath('day23.txt', 'aoc2020'),
                     conv=lambda m: [int(x) for x in m])[0]

    print(part1(data))
    print(part2(data))
def get_data():
    return readlines(rpath('day09.txt', 'aoc2021'))
def get_data():
    data = readlines(rpath('day11.txt', 'aoc2021'))
    data = {(x, y): int(c)
            for y, line in enumerate(data) for x, c in enumerate(line)}
    return data