def memorygame(starting, place=2020):
    seen = dict()
    for i, x in enumerate(starting[:-1]):
        seen[x] = i + 1
    count = len(starting)
    previous = starting[-1]
    while count < place:
        if previous in seen:
            nxt = count - seen[previous]
            seen[previous] = count
            previous = nxt
        else:
            seen[previous] = count
            previous = 0
        count += 1
    return previous


def main():
    starting = [9, 12, 1, 4, 17, 0, 18]

    part1 = memorygame(starting, 2020)
    print("Part I: ", part1)

    part2 = memorygame(starting, 30000000)
    print("Part II:", part2)


if __name__ == '__main__':
    main()
