예제 #1
0
def main():
    lsp = lisp.Lisp(lisp.Lexer(), lisp.Parser(), lisp.Evaluator())
    lst = [
        "(caar '((1 2) (3 4)))",
        "(cadr '((1 2) (3 4)))",
        "(cdar '((1 2) (3 4)))",
        "(cddr '((1 2) (3 4)))",
        "(null? '())",
        "(null? 0)",
        "(zero? 0)",
        "(zero? 3)",
        "(> 1 2)",
        "(> 2 1)",
        "(min 1 6 5 2 -9)",
        "(max 1 6 5 2 -9)",
        "(map (lambda (x) (+ x x)) '(1 2 3 4 5))",
        "(filter even? '(1 2 3 4 5 6))",
        "(abs +1)",
        "(abs -1)",
        "(abs +1.2)",
        "(length '(1 2))",
        "(list-ref '(1 2 3 4 5) 3)",
        "(append '(1 2 3 4) '(5 6 7 8))",
        "(reverse '(1 2 3 4 5))",
        "(sqrt 2.0)",
        #"(map (lambda (n) (expt 2 n)) '(1 2 3 4))",
        "(let ((x 1)) x)",
        "(let ((x 1) (y 2)) (+ x y))",
    ]
    for n in lst:
        result = lsp.evaluator.eval(lsp.parser.parse(lsp.lexer.tokenize(n)))
        print n, "=>", result
예제 #2
0
    def test_part_two(self):
        "Test part two example of Lisp object"

        # 1. Create Lisp object from text
        myobj = lisp.Lisp(part2=True, text=aoc_01.from_text(PART_TWO_TEXT))

        # 2. Check the part two result
        self.assertEqual(myobj.part_two(verbose=False), PART_TWO_RESULT)
예제 #3
0
    def test_part_one(self):
        "Test part one example of Lisp object"

        # 1. Create Lisp object from text
        myobj = lisp.Lisp(text=aoc_01.from_text(PART_ONE_TEXT))

        # 2. Check the part one result
        self.assertEqual(myobj.part_one(verbose=False), PART_ONE_RESULT)
예제 #4
0
    def test_text_init(self):
        "Test the Lisp object creation from text"

        # 1. Create Lisp object from text
        myobj = lisp.Lisp(text=aoc_01.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 7)
예제 #5
0
    def test_empty_init(self):
        "Test the default Lisp creation"

        # 1. Create default Lisp object
        myobj = lisp.Lisp()

        # 2. Make sure it has the default values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(myobj.text, None)
예제 #6
0
    def test_part_one_examples(self):
        "Test the part one examples"

        # 1. Loop for all the examples
        for example, result in EXAMPLES.items():

            # 2. Create Lisp object from text
            myobj = lisp.Lisp(text=[example])

            # 2. Check the part one result
            self.assertEqual(myobj.part_one(verbose=False), result)
예제 #7
0
def part_two(args, input_lines):
    "Process part two of the puzzle"

    # 1. Create the puzzle solver
    solver = lisp.Lisp(part2=True, text=input_lines)

    # 2. Determine the solution for part two
    solution = solver.part_two(verbose=args.verbose, limit=args.limit)
    if solution is None:
        print("There is no solution")
    else:
        print("The solution for part two is %s" % (solution))

    # 3. Return result
    return solution is not None