#! python3

import itertools
from collections import deque
import time

'''
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
'''
def fibUnderValue(value):
    fibNumbers = [1,2]
    list(next(iter(())) if (fibNumbers[-1]+fibNumbers[-2] >= value) else fibNumbers.append(fibNumbers[-1]+fibNumbers[-2]) for x in itertools.count())
    return [x for x in fibUnderValue(value) if x%2 == 0]

def evenFibUnderValue(value):
    #setup
    fibwindow = deque([1,1,2],3)
    fibNumbers = []

    #calculate fibs
    while(fibwindow[2]<value):
        fibwindow.append(fibwindow[1]+fibwindow[2])
        if fibwindow[0]%2 == 0:
            fibNumbers.append(fibwindow[0])
    #closing ops
    if fibwindow[1]%2 == 0:
        fibNumbers.append(fibwindow[1])
    if fibwindow[2]%2 == 0:
        fibNumbers.append(fibwindow[2])
    return fibNumbers

def main():
    value = 4000000
    print(sum(evenFibUnderValue(value)))
    print(sum(fibUnderValue(value)))

if __name__ == "__main__":
    main()