示例#1
0
        return max(list(self._registers.values()))

    def _update_max_register(self):
        if self.current_max_register > self.max_register:
            self.max_register = self.current_max_register

    def _apply_instruction(self, instruction):
        value1 = self._registers[instruction.check_register]
        value2 = instruction.check_value
        if self._check(value1, value2, instruction.condition):
            self._update_register(instruction.change_register,
                                  instruction.change_value,
                                  instruction.operation)
            self._update_max_register()

    def _check(self, a, b, op):
        return self.comparison_lookup[op](a, b)

    def _update_register(self, register, change, op):
        result = self.operation_lookup[op](self._registers[register], change)
        self._registers[register] = result


if __name__ == '__main__':
    raw_lines = open_file("day8").read().splitlines()
    instructions = [Instruction(line) for line in raw_lines]
    reg = Registers(instructions)
    reg.apply_instructions()
    print(reg.current_max_register)
    print(reg.max_register)
示例#2
0
        elif direction == 'sw':
            self.x += -1

        self._set_z()


def point_from_directions(directions):
    max_distance = 0
    p = HexPoint(0, 0, 0)
    for d in directions:
        p.move(d)
        distance = manhattan_distance(p, HexPoint(0, 0, 0))
        if distance > max_distance:
            max_distance = distance
    return p, max_distance


def manhattan_distance(point_a, point_b):
    return max((
        abs(point_a.x - point_b.x),
        abs(point_a.y - point_b.y),
        abs(point_a.z - point_b.z),
    ))


if __name__ == '__main__':
    directions = open_file("day11").read().strip().split(',')
    p, max_distance = point_from_directions(directions)
    print(manhattan_distance(p, HexPoint(0, 0, 0)))
    print(max_distance)
示例#3
0
        moves = 0

        while True:
            try:
                offset = self.offsets[current_position]
            except IndexError:
                break
            self.increment_offset(current_position)
            current_position += offset
            moves += 1
        return moves

    def increment_offset(self, index):
        self.offsets[index] += 1


class ThreeOrMoreReader(OffsetReader):
    def increment_offset(self, index):
        value = self.offsets[index]
        if value >= 3:
            self.offsets[index] -= 1
        else:
            self.offsets[index] += 1


if __name__ == '__main__':
    offsets = [int(i) for i in open_file("day5").read().splitlines()]
    print(OffsetReader(offsets).run())
    offsets = [int(i) for i in open_file("day5").read().splitlines()]
    print(ThreeOrMoreReader(offsets).run())
示例#4
0
    return pipes


def group(program, pipes):
    connected = set([program])
    checked = set()

    while connected.difference(checked):
        for p in connected.difference(checked):
            for k, v in pipes.items():
                if p == k or p in v:
                    connected.update(set(v))
                    connected.add(k)
            checked.add(p)
    return connected


def groups(pipes):
    groups = []
    for p in pipes:
        g = group(p, pipes)
        if g not in groups:
            groups.append(g)
    return groups


if __name__ == '__main__':
    pipes = parse_input(open_file("day12").read().splitlines())
    print(len(group('0', pipes)))
    print(len(groups(pipes)))
示例#5
0
        self.hex = ''

    def _parse_lengths(self, lengths):
        return [ord(i) for i in lengths] + [17, 31, 73, 47, 23]

    def hash(self):
        for i in range(64):
            self._hash()
        self._undo_rotations()
        self._compact_hash()
        self._hexlify()
        return self._hex

    def _compact_hash(self):
        self._dense_hash = []
        for _ in range(16):
            block = [self._numbers.pop(0) for i in range(16)]
            self._dense_hash.append(reduce(operator.xor, block))

    def _hexlify(self):
        self._hex = binascii.hexlify(bytearray(self._dense_hash))


if __name__ == '__main__':
    lengths = open_file("day10").read().strip()
    h = Hash(lengths)
    h.hash()
    print(h.product(0, 1))
    h = AsciiHash(lengths)
    print(h.hash())
示例#6
0
from aoc.shared import open_file
from aoc.days.day3.part1 import point_from_tile, manhattan_distance
from aoc.days.day3.part2 import SpiralGrid

if __name__ == '__main__':
    raw_num = open_file("day3").read().strip()
    start_p = point_from_tile(int(raw_num))
    end_p = point_from_tile(1)
    print(manhattan_distance(start_p, end_p))

    sg = SpiralGrid(int(raw_num))
    sg.build()
    print(sg.max_value)
示例#7
0
    def _convert_row_to_ints(self, row):
        return [int(s) for s in row]

    def _row_checksum(self, row):
        raise NotImplementedError()


class MaxMinSheet(Sheet):
    def _row_checksum(self, row):
        return max(row) - min(row)


class EvenDivisorsSheet(Sheet):
    def _row_checksum(self, row):
        pairs = itertools.combinations(row, 2)
        for pair in pairs:
            high, low = max(pair), min(pair)
            if high % low == 0:
                return int(high / low)


if __name__ == '__main__':
    input_file = open_file("day2")
    sheet = MaxMinSheet(input_file)
    print(sheet.checksum)

    input_file = open_file("day2")
    sheet = EvenDivisorsSheet(input_file)
    print(sheet.checksum)
示例#8
0
from aoc.shared import open_file


def split_to_words(passphrase):
    return passphrase.split(' ')


def unique_words(words):
    return sorted(list(set(words))) == sorted(words)


def no_anagrams(words):
    reduced = sorted(set("".join(sorted(word)) for word in words))
    original = sorted("".join(sorted(word)) for word in words)
    return reduced == original


def count_valids(passphrases, comparison_function):
    valids = [p for p in passphrases if comparison_function(p)]
    return len(valids)


if __name__ == '__main__':
    passphrases = [
        split_to_words(line) for line in open_file("day4").read().splitlines()
    ]
    print(count_valids(passphrases, unique_words))
    print(count_valids(passphrases, no_anagrams))
示例#9
0
    def _saw_current_distribution(self):
        return self.banks in self._snapshots

    def _add_snapshot(self):
        self._snapshots.append(copy.copy(self.banks))

    def _remove_max_bank(self):
        max_bank = max(self.banks)
        max_index = self.banks.index(max_bank)
        self.banks[max_index] = 0
        return max_bank, max_index

    def _redistribution_indices(self, bank_value, bank_index):
        self._reset_indices()
        # rotate left and don't include bank_index
        rotation = (bank_index + 1) * -1
        self._indices.rotate(rotation)
        index_gen = itertools.cycle(self._indices)
        return [next(index_gen) for _ in range(bank_value)]

    def _reset_indices(self):
        self._indices = deque(range(0, len(self.banks)))


if __name__ == '__main__':
    banks = [int(i) for i in open_file("day6").read().strip().split()]
    m = Memory(banks)
    m.redistribute()
    print(m.redistribution_count)
    print(m.infinite_loop_size)
示例#10
0
from collections import deque

from aoc.shared import open_file


class Reducer:
    def __init__(self, digits, offset=1):
        self.digits = digits
        self.offset = offset

    def reduce(self):
        return sum(self._num_if_equal(a, b) for a, b in self._pairs())

    def _pairs(self):
        rotated = deque(self.digits)
        rotated.rotate(self.offset)
        return zip(self.digits, rotated)

    def _num_if_equal(self, a, b):
        if a == b:
            return int(a)
        return 0


if __name__ == '__main__':
    input_data = open_file("day1").read().strip()
    reducer = Reducer(input_data)
    print(reducer.reduce())
    reducer = Reducer(input_data, offset=int(len(input_data) / 2))
    print(reducer.reduce())