aboutsummaryrefslogtreecommitdiff
path: root/src/philo/philo.py
blob: e7e2e1db6bfa5cdc3790f141b892d08a0511fef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ############################################################################ #
#                                                                              #
#                                                         :::      ::::::::    #
#    philo.py                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: charles <me@cacharle.xyz>                  +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/09/27 12:54:12 by charles           #+#    #+#              #
#    Updated: 2020/09/27 16:50:22 by charles          ###   ########.fr        #
#                                                                              #
# ############################################################################ #

import itertools
from philo.event import Event

class Philo:
    def __init__(self, id_: int, meal_num: int = 1):
        self._logs = []
        self.id = id_
        self.meal_num = meal_num

    def add_log(self, log):
        self._logs.append(log)

    def check(self):
        grouped = [(e, list(g)) for e, g in itertools.groupby(self._logs, (lambda x: x.event))]
        for e, g in grouped:
            if e is Event.EATING:
                if len(g) != self.meal_num:
                    raise RuntimeError("lala")
            else:
                if len(g) != 1:
                    raise RuntimeError("1lala")

        events = [e for e, _ in grouped]
        for e1, e2 in zip(events, events[1:]):
            if e2 is Event.DIED:
                break
            if e1 is Event.THINKING and e2 is not Event.EATING:
                raise RuntimeError("2lala")
            elif e1 is Event.EATING and e2 is not Event.SLEEPING:
                raise RuntimeError("2lala")
            elif e1 is Event.SLEEPING and e2 is not Event.EATING:
                raise RuntimeError("2lala")

    @property
    def last_event(self):
        if len(self._logs) == 0:
            return Event.NONE
        return self._logs[-1].event