AdventOfCode/2019/day4.py

35 lines
1.1 KiB
Python
Raw Normal View History

2019-12-04 13:41:36 +01:00
def main():
with open("day4.txt", "rt") as input_file:
first_line = input_file.readlines()[0]
lower_bound, upper_bound = tuple(first_line.strip().split("-"))
solve(int(lower_bound), int(upper_bound))
def solve(lower_bound, upper_bound):
validity_map = map(lambda v: int(is_valid(v)), range(lower_bound, upper_bound))
print("Num valids:", sum(validity_map))
def is_valid(value):
return has_adjacents(value) and is_increasing(value)
def has_adjacents(value):
digits = [get_digit(value, index) for index in range(6)]
2019-12-15 16:50:02 +01:00
adjacent_digits = []
2019-12-04 13:41:36 +01:00
for i in range(6 - 1):
if digits[i] == digits[i + 1]:
2019-12-15 16:50:02 +01:00
adjacent_digits.append(digits[i])
return any(sum(a == d for d in adjacent_digits) == 1 for a in set(adjacent_digits))
2019-12-04 13:41:36 +01:00
def is_increasing(value):
digits = [get_digit(value, index) for index in range(6)]
for i in range(6 - 1):
if digits[i + 1] < digits[i]:
return False
return True
def get_digit(value, index):
return int(str(value)[index])
2019-12-15 16:50:02 +01:00
2019-12-04 13:41:36 +01:00
if __name__ == "__main__":
main()