You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
66 lines
1.8 KiB
import unittest
|
|
import os
|
|
|
|
class Day4(unittest.TestCase):
|
|
def num_to_array(self, num):
|
|
arr = []
|
|
while num != 0:
|
|
arr.append(num % 10)
|
|
num = num //10
|
|
arr.reverse()
|
|
return arr
|
|
|
|
def check_num(self, num):
|
|
numarr = self.num_to_array(num)
|
|
last = numarr[0]
|
|
adj = False
|
|
for p in range(1,6):
|
|
val = numarr[p]
|
|
if last > val:
|
|
return False
|
|
if last == val:
|
|
adj = True
|
|
last = val
|
|
return adj
|
|
|
|
def check_num_grp(self, num):
|
|
numarr = self.num_to_array(num)
|
|
last = numarr[0]
|
|
adj = False
|
|
candidate = False
|
|
finished = False
|
|
for p in range(1,6):
|
|
val = numarr[p]
|
|
if last > val:
|
|
return False
|
|
if last == val:
|
|
if adj:
|
|
candidate = False
|
|
else:
|
|
candidate = True
|
|
adj = True
|
|
else:
|
|
adj = False
|
|
if candidate:
|
|
finished = True
|
|
last = val
|
|
return finished or candidate
|
|
|
|
def test_day4a(self):
|
|
counter = 0
|
|
for num in range(168630, 718099):
|
|
if self.check_num(num) == True:
|
|
counter += 1
|
|
self.assertEqual(counter, 1686)
|
|
|
|
def test_day4b(self):
|
|
self.assertTrue(self.check_num_grp(112233))
|
|
self.assertFalse(self.check_num_grp(123444))
|
|
counter = 0
|
|
for num in range(168630,718099):
|
|
if self.check_num_grp(num) == True:
|
|
counter += 1
|
|
self.assertEqual(counter, 1145)
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |