Exemple #1
0
    def process_goal(self, goal_data):
        if self.is_movie:
            return

        if not goal_data:
            return

        return Goal(lookup(goal_data, 'formula'), lookup(goal_data, 'size'),
                    lookup(goal_data, 'speed'), lookup(goal_data, 'expected'))
Exemple #2
0
    def get_program(self, key):
        if self.is_movie:
            return None

        sol = lookup(self.programs, key)

        if not sol and (key == 'fast' or key == 'small'):
            sol = lookup(self.programs, 'optimal')

        return sol
Exemple #3
0
    def process_data(self, data):
        if self.is_movie:
            return

        self.available = lookup(data, 'available', [])
        self.available.extend(ALWAYS_AVAILABLE)

        self.goal = self.process_goal(lookup(data, 'goal'))
        self.input = self.process_input(lookup(data, 'input'))
        self.registers = self.process_registers(lookup(data, 'registers'))
        self.programs = self.process_programs(lookup(data, 'programs', dict()))
Exemple #4
0
    def process_input(self, input_data):
        if self.is_movie:
            return

        if not input_data:
            return

        input_obj = Input(lookup(input_data, 'alphabet'),
                          lookup(input_data, 'count'),
                          lookup(input_data, 'sample'))

        return input_obj
Exemple #5
0
    def __init__(self, key, data):
        self.original_data = data
        self.key = str(key)
        self.name = lookup(data, 'name')
        self.is_movie = lookup(data, 'movie')

        self.available = []
        self.goal = None
        self.input = None
        self.registers = None
        self.programs = None
        self.inboxes = []  # list of input tried

        self.process_data(data)
Exemple #6
0
    def process_registers(self, register_data):
        if self.is_movie:
            return

        if not register_data:
            return Registers(0, [])

        values = lookup(register_data, 'values', [])

        new_vals = dict()

        for key in values:
            new_vals[str(key)] = values[key]

        return Registers(lookup(register_data, 'count', 0), values)
Exemple #7
0
    def process_programs(self, programs_data):
        if self.is_movie:
            return

        programs = dict()

        for key, program in programs_data.items():
            pre_commands = []

            # expand any repeats
            for cmd_item in program['commands']:
                if type(cmd_item) is dict:
                    cmd_repeat = lookup(cmd_item, 'repeat')
                    if cmd_repeat:
                        count = cmd_repeat['count']

                        for i in range(0, count):
                            pre_commands.extend(cmd_repeat['commands'])

                        continue

                pre_commands.append(cmd_item)

            ln = 0
            labels = dict()
            commands = []

            for str_cmd in pre_commands:
                command = self.process_str_command(ln, str_cmd)

                if not command:
                    continue

                if command.label is not None:
                    labels[command.label] = ln

                commands.append(command)

                ln += 1

            size = lookup(program, 'size', self.goal.size)
            speed = lookup(program, 'speed', self.goal.speed)

            programs[key] = Program(key, commands, size, speed, labels)

        return programs
Exemple #8
0
    def check_commands(self, command_list=None):
        command_list = command_list or self.command_list

        command_set = set(command_list)

        for cmd in command_set:
            ok = lookup(command_set, cmd)
            if not ok:
                return cmd
Exemple #9
0
    def test_lookup(self):
        tests = [
            LookupTest('set', {1, 2, 3}, 1, 1, 4),
            LookupTest('list', [4, 5, 6], 1, 5, 4),
            LookupTest('dict', dict(a=7, b=8, c=9), 'a', 7, 'd'),
            LookupTest('tuple', (4, 5, 6), 1, 5, 4),
        ]

        # You probably wouldn't use `lookup` with namedtuple, but...
        tests.append(LookupTest('namedtuple', tests[0], 0, 'set', 12))

        if_none = 'blah'
        for test in tests:
            self.assertEqual(
                lookup(test.collection, test.good_key), test.expected_good,
                f"Lookup ({test.good_key}) for '{test.name}': ok")
            self.assertEqual(lookup(test.collection, test.bad_key), None,
                             "Lookup() is None for '{test.name}': ok")
            self.assertEqual(lookup(test.collection, test.bad_key,
                                    if_none), if_none,
                             "Lookup() for missing with if_none for set ok")