Exemplo n.º 1
0
    def __init__(self, text=None):

        # 1. Set the initial values
        self.items = []

        # 2. Process text (if any)
        if text is not None and len(text) > 0:
            self._process_text(text)
        else:
            self._process_text(aoc_21.from_text(ITEMS_TEXT))
            self._process_text(aoc_21.from_text(NOTHING_TEXT))
Exemplo n.º 2
0
    def test_text_init(self):
        "Test the Pword object creation from text"

        # 1. Create Pword object from text
        myobj = pword.Pword(text=aoc_21.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 8)

        # 3. Check methods
        self.assertEqual(myobj.execute('swap position 4 with position 0', 'abcde'), 'ebcda')
        self.assertEqual(myobj.execute('swap letter d with letter b', 'ebcda'), 'edcba')
        self.assertEqual(myobj.execute('reverse positions 0 through 4', 'edcba'), 'abcde')
        self.assertEqual(myobj.execute('rotate left 1 step', 'abcde'), 'bcdea')
        self.assertEqual(myobj.execute('move position 1 to position 4', 'bcdea'), 'bdeac')
        self.assertEqual(myobj.execute('move position 3 to position 0', 'bdeac'), 'abdec')
        self.assertEqual(myobj.execute('rotate based on position of letter b', 'abdec'), 'ecabd')
        self.assertEqual(myobj.execute('rotate based on position of letter d', 'ecabd'), 'decab')

        self.assertEqual(myobj.unexecute('swap position 4 with position 0', 'ebcda'), 'abcde')
        self.assertEqual(myobj.unexecute('swap letter d with letter b', 'edcba'), 'ebcda')
        self.assertEqual(myobj.unexecute('reverse positions 0 through 4', 'abcde'), 'edcba')
        self.assertEqual(myobj.unexecute('rotate left 1 step', 'bcdea'), 'abcde')
        self.assertEqual(myobj.unexecute('move position 1 to position 4', 'bdeac'), 'bcdea')
        self.assertEqual(myobj.unexecute('move position 3 to position 0', 'abdec'), 'bdeac')
        self.assertEqual(myobj.unexecute('rotate based on position of letter b', 'ecabd'), 'abdec')
        self.assertEqual(myobj.unexecute('rotate based on position of letter d', 'decab'), 'ecabd')
Exemplo n.º 3
0
    def test_text_init(self):
        "Test the Fractal object creation from text"

        # 1. Create Fractal object from text
        myobj = fractal.Fractal(text=aoc_21.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 2)
        self.assertEqual(myobj.art, fractal.INITIAL_PATTERN)
        self.assertEqual(myobj.art_size, fractal.INITIAL_SIZE)
        self.assertEqual(len(myobj.rules), 2)
        self.assertEqual(len(myobj.rules[2]), 1)
        self.assertEqual(len(myobj.rules[3]), 1)

        # 3. Check methods
        self.assertEqual(myobj.pixels(), PIXELS_ZERO)
        self.assertEqual(len(list(myobj.squares())), 1)
        self.assertEqual(list(myobj.squares())[0], fractal.INITIAL_PATTERN)
        self.assertEqual(myobj.transform(fractal.INITIAL_PATTERN),
                         ENANCEMENT_ONE)

        # 4. Do one enhancement step
        #print("Step one")
        myobj.step()
        self.assertEqual(myobj.art, ENANCEMENT_ONE)
        self.assertEqual(myobj.art_size, SIZE_ONE)
        self.assertEqual(myobj.pixels(), PIXELS_ONE)

        # 5. And then another enhancement step
        #print("Step two")
        myobj.step()
        self.assertEqual(myobj.art, ENANCEMENT_TWO)
        self.assertEqual(myobj.art_size, SIZE_TWO)
        self.assertEqual(myobj.pixels(), PIXELS_TWO)
Exemplo n.º 4
0
    def test_text_init(self):
        "Test the Player object creation from text"

        # 1. Create Player object from text
        myobj = player.Player(text=aoc_21.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 3)
        self.assertEqual(myobj.name, 'noone')
        self.assertEqual(len(myobj.attributes), 3)
        self.assertEqual(myobj.attributes['hitpoints'], 12)
        self.assertEqual(myobj.attributes['damage'], 7)
        self.assertEqual(myobj.attributes['armor'], 2)

        # 3. Check methods
        self.assertEqual(myobj.is_dead(), False)
        self.assertEqual(myobj['hitpoints'], 12)
        self.assertEqual(myobj['damage'], 7)
        self.assertEqual(myobj['armor'], 2)
        myobj['hitpoints'] = 0
        self.assertEqual(myobj.attributes['hitpoints'], 0)
        self.assertEqual(myobj['hitpoints'], 0)
        self.assertEqual(myobj.is_dead(), True)
        myobj['hitpoints'] = 12
        myobj.defend(5)
        self.assertEqual(myobj['hitpoints'], 9)
        self.assertEqual(myobj.is_dead(), False)
Exemplo n.º 5
0
    def test_part_two(self):
        "Test part two example of Pword object"

        # 1. Create Pword object from text
        myobj = pword.Pword(part2=True, text=aoc_21.from_text(PART_TWO_TEXT))

        # 2. Check the part two result
        self.assertEqual(myobj.part_two(verbose=False, word=PART_TWO_WORD), PART_TWO_RESULT)
Exemplo n.º 6
0
    def test_part_one(self):
        "Test part one example of Pword object"

        # 1. Create Pword object from text
        myobj = pword.Pword(text=aoc_21.from_text(PART_ONE_TEXT))

        # 2. Check the part one result
        self.assertEqual(myobj.part_one(verbose=False, word=PART_ONE_WORD), PART_ONE_RESULT)
Exemplo n.º 7
0
    def test_part_one(self):
        "Test part one example of Game object"

        # 1. Create Game object from text
        myobj = game.Game(text=aoc_21.from_text(PART_ONE_TEXT))

        # 2. Check the part one result
        self.assertEqual(myobj.part_one(verbose=False), PART_ONE_RESULT)
Exemplo n.º 8
0
    def test_part_two(self):
        "Test part two example of Game object"

        # 1. Create Game object from text
        myobj = game.Game(part2=True, text=aoc_21.from_text(PART_TWO_TEXT))

        # 2. Check the part two result
        self.assertEqual(myobj.part_two(verbose=False), PART_TWO_RESULT)
Exemplo n.º 9
0
    def test_part_one(self):
        "Test part one example of Fractal object"

        # 1. Create Spinlock object from text
        myobj = fractal.Fractal(text=aoc_21.from_text(PART_ONE_TEXT))

        # 2. Check the part one result
        self.assertEqual(myobj.part_one(verbose=False, limit=2),
                         PART_ONE_RESULT)
Exemplo n.º 10
0
    def test_text_init(self):
        "Test the Rpgsim object creation from text"

        # 1. Create Rpgsim object from text
        myobj = rpgsim.Rpgsim(text=aoc_21.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 0)
Exemplo n.º 11
0
    def test_part_two(self):
        "Test part two example of Fractal object"

        # 1. Create Spinlock object from text
        myobj = fractal.Fractal(part2=True,
                                text=aoc_21.from_text(PART_TWO_TEXT))

        # 2. Check the part two
        self.assertEqual(myobj.part_two(verbose=False, limit=2),
                         PART_TWO_RESULT)
Exemplo n.º 12
0
    def test_text_init(self):
        "Test the Shopping object creation from text"

        # 1. Create Shopping object from text
        myobj = shopping.Shopping(text=aoc_21.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 4)
        self.assertEqual(len(myobj.labels), 4)
        self.assertEqual(len(myobj.mapping), 3)
        self.assertEqual(myobj.mapping['soy'], set(['sqjhc', 'fvjkl']))
Exemplo n.º 13
0
    def test_text_init(self):
        "Test the Game object creation from text"

        # 1. Create Game object from text
        myobj = game.Game(text=aoc_21.from_text(EXAMPLE_TEXT))

        # 2. Make sure it has the expected values
        self.assertEqual(myobj.part2, False)
        self.assertEqual(len(myobj.text), 2)
        self.assertEqual(len(myobj.track), 10)
        self.assertEqual(myobj.track[0], 1)
        self.assertEqual(myobj.track[-1], 10)
        self.assertEqual(len(myobj.players), 2)
        self.assertEqual(myobj.players[0].number, 1)
        self.assertEqual(myobj.players[0].position, 3)
        self.assertEqual(myobj.players[0].score, 0)
        self.assertEqual(myobj.players[1].number, 2)
        self.assertEqual(myobj.players[1].position, 7)
        self.assertEqual(myobj.players[1].score, 0)
        self.assertEqual(myobj.die.sides, 100)
        self.assertEqual(myobj.goal, 1000)
        self.assertEqual(myobj.times, 3)

        # 3. Check methods
        myobj.full_turn()
        self.assertEqual(myobj.players[0].number, 1)
        self.assertEqual(myobj.players[0].position, 9)
        self.assertEqual(myobj.players[0].score, 10)
        self.assertEqual(myobj.players[1].number, 2)
        self.assertEqual(myobj.players[1].position, 2)
        self.assertEqual(myobj.players[1].score, 3)
        myobj.full_turn()
        self.assertEqual(myobj.players[0].number, 1)
        self.assertEqual(myobj.players[0].position, 3)
        self.assertEqual(myobj.players[0].score, 14)
        self.assertEqual(myobj.players[1].number, 2)
        self.assertEqual(myobj.players[1].position, 5)
        self.assertEqual(myobj.players[1].score, 9)