def demo(): # pragma: no cover """Run the demo.""" if len(sys.argv) == 1: runs = 5 else: runs = int(sys.argv[1]) for i in range(0, runs + 1): kwpbar.pbar(i, runs) time.sleep(1) kwpbar.erase_pbar()
#!/usr/bin/python3 import numpy import re import kwpbar R = re.compile('(turn on|toggle|turn off) (\d+),(\d+) through (\d+),(\d+)') a = numpy.zeros((1000, 1000), numpy.int16) kwpbar.pbar(0, 300) with open('06-input.txt') as fh: for nl, l in enumerate(fh, 1): m = R.match(l) action = m.groups()[0] x1, y1, x2, y2 = map(int, m.groups()[1:]) for x in range(x1, x2 + 1): for y in range(y1, y2 + 1): if action == 'turn on': a[x][y] += 1 elif action == 'turn off': a[x][y] -= 1 elif action == 'toggle': a[x][y] += 2 # cap at zero if a[x][y] < 0: a[x][y] = 0 kwpbar.pbar(nl, 300) print() print(a.sum())
# Find neighbors, in a very cheap way. neighbors = [ (rownum-1, colnum), (rownum+1, colnum), (rownum, colnum-1), (rownum, colnum+1), (rownum-1, colnum-1), (rownum+1, colnum+1), (rownum-1, colnum+1), (rownum+1, colnum-1) ] # limit to numbers 0-100 neighbors = [(i, j) for i, j in neighbors if i in range(100) and j in range(100)] neighbors_on = 0 for nR, nC in neighbors: neighbors_on += NOW[nR][nC] # Determine new status of the current cell. if cell_on: NEXT[rownum][colnum] = (neighbors_on == 2 or neighbors_on == 3) else: NEXT[rownum][colnum] = (neighbors_on == 3) kwpbar.pbar((iteration * 100) + rownum, 100 * 100) NOW = NEXT # Part 2: four lights are stuck NOW[0][0] = NOW[99][0] = NOW[0][99] = NOW[99][99] = True kwpbar.erase_pbar() print(numpy.count_nonzero(NOW))
def test_fail3(): with pytest.raises(ValueError): kwpbar.pbar(-2, 1)
def test_fail1(): with pytest.raises(ValueError): kwpbar.pbar(1, 0)
def test_erasing(): """Test progressbar erasing.""" kwpbar.pbar(2, 3) kwpbar.erase_pbar()
def test_printing(): """Test progressbar printing.""" kwpbar.pbar(0, 2) kwpbar.pbar(1, 2) kwpbar.pbar(2, 2)
# Read ingredient information with open('15-input.txt') as fh: for l in fh: data = r.match(l.strip()) i = Ingredient(*data.groups()) INGREDIENTS.append(i) best_score = 0 #best_set = [] tested = 0 pbar_max = 176851 for cookie in itertools.combinations_with_replacement(INGREDIENTS, 100): kwpbar.pbar(tested, pbar_max) capacity = 0 durability = 0 flavor = 0 texture = 0 calories = 0 for i in cookie: capacity += i.capacity durability += i.durability flavor += i.flavor texture += i.texture calories += i.calories tested += 1 if capacity < 0 or durability < 0 or flavor < 0 or texture < 0: