def main(): input_data = get_input_data(3) n_trees = count_trees_in_path(input_data) logger.info(f'Solution 3a: {n_trees}') n_trees_list = [count_trees_in_path(input_data, x, y) for x, y in SLOPE_INCREMENTS] n_trees_product = multiply(n_trees_list) logger.info(f'Solution 3b: {n_trees_product}')
def testMultiply(self): nonechk = True self.result = math.multiply(10, 9) if self.result > 100: nonechk = None # 결과 값이 None 여부 확인 self.assertIsNone(nonechk)
def multiply_corner_tile_ids(): tiled_image = parse_tiled_image() high = sqrt(len(tiled_image)) - 1 return multiply([ tiled_image[0, 0].id, tiled_image[0, high].id, tiled_image[high, 0].id, tiled_image[high, high].id ])
infile = open('day1-input', 'r') # our report is a list of all of the numbers in the file, with line breaks removed report = [int(line.strip()) for line in infile] infile.close() # part 1 # I iterate through the cartesian product of the report with itself, # which is to say, I iterate through every possible pairing of a number # another in the report, and check that they sum to 2020, if so, return # their (arithmetic) product and exit the loop for pair in product(report, repeat = 2): if sum(pair) == 2020: print(multiply(pair)) break # part 2 # same as part 1 but with 3 copies of report for trio in product(report, repeat = 3): if sum(trio) == 2020: print(multiply(trio)) break # for fun, here's a function that'll do the above for any number of elements n def tuple_sum_to(l: Iterable, n: int, target) -> Union[tuple, None]: for t in product(l, repeat = n): if sum(t) == target: return t
def rod_weight(): math.multiply(float(rod_mass * g)) return value
def multiply_corner_tile_ids(): tiles = list(parse_tiles(input_)) matched = get_matched_tiles(tiles) assert len(matched[N_CORNER]) == 4 return multiply(tile.id for tile, _ in matched[N_CORNER])
def attributeBreak(): import math math.multiply(4,5)
j -= 1 # subtract something elif current < goal: i += 1 # add something else: break if j < 0 or i >= len(input): raise Exception() return input[i], input[j] def sum_three(goal): """ finds three numbers in the input that are equal to the goal when summed. """ for number in input: sub_goal = goal - number try: a, b = sum_two(sub_goal) except: continue return number, a, b raise Exception() solve_1 = lambda: multiply(sum_two(2020)) solve_2 = lambda: multiply(sum_three(2020))
def test_multiply0(self): self.assertEqual(multiply(3,5), 15)
def test_numbers_3_4(self): self.assertEqual(multiply(3, 4), 12)
def test_strings_a_3(self): self.assertEqual(multiply('a', 3), 'aaa')