From 353b96999bec4b6314d87a579a166a4a2b64665a Mon Sep 17 00:00:00 2001 From: Lukas Winkler Date: Thu, 5 Dec 2019 15:58:22 +0100 Subject: [PATCH] add day 4 --- python/4/day4.py | 55 +++++++++++++++++++++++++++++++++++++++++++ python/4/test_day4.py | 40 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 python/4/day4.py create mode 100644 python/4/test_day4.py diff --git a/python/4/day4.py b/python/4/day4.py new file mode 100644 index 0000000..e5c6d64 --- /dev/null +++ b/python/4/day4.py @@ -0,0 +1,55 @@ +from collections import Counter + +INPUT = "245182-790572" + + +def increasing_digit(number: int) -> bool: + prev = 0 + for char in str(number): + if prev > int(char): + return False + else: + prev = int(char) + return True + + +def has_adjacent_digits(number: int) -> bool: + prev = None + for char in str(number): + if prev == char and prev is not None: + return True + else: + prev = char + return False + + +def has_adjacent_digits_that_arent_part_of_a_larger_group(number: int) -> bool: + stats = Counter(str(number)) + for key, count in stats.items(): + if count == 2: + return True + return False + + +def part1() -> int: + lower, upper = map(int, INPUT.split("-")) + validcount = 0 + for pw in range(lower, upper + 1): + if increasing_digit(pw) and has_adjacent_digits(pw): + validcount += 1 + + return validcount + +def part2() -> int: + lower, upper = map(int, INPUT.split("-")) + validcount = 0 + for pw in range(lower, upper + 1): + if increasing_digit(pw) and has_adjacent_digits_that_arent_part_of_a_larger_group(pw): + validcount += 1 + + return validcount + + +if __name__ == '__main__': + print(part1()) + print(part2()) diff --git a/python/4/test_day4.py b/python/4/test_day4.py new file mode 100644 index 0000000..3374533 --- /dev/null +++ b/python/4/test_day4.py @@ -0,0 +1,40 @@ +from day4 import increasing_digit, has_adjacent_digits, part1, has_adjacent_digits_that_arent_part_of_a_larger_group, \ + part2 + + +def test_increasing_digit(): + assert increasing_digit(1234567) + assert not increasing_digit(1236567) + assert increasing_digit(1234447) + + +def test_has_adjacent_digits(): + assert has_adjacent_digits(11345345) + assert has_adjacent_digits(343244) + assert not has_adjacent_digits(2454367) + + +def test_example_codes(): + assert has_adjacent_digits(111111) and increasing_digit(111111) + assert has_adjacent_digits(223450) and not increasing_digit(223450) + assert not has_adjacent_digits(123789) and increasing_digit(123789) + + +def test_has_adjacent_digits_that_arent_part_of_a_larger_group(): + assert has_adjacent_digits_that_arent_part_of_a_larger_group(11345345) + assert has_adjacent_digits_that_arent_part_of_a_larger_group(343244) + assert not has_adjacent_digits_that_arent_part_of_a_larger_group(24544467) + + +def test_more_example_codes(): + assert has_adjacent_digits_that_arent_part_of_a_larger_group(112233) + assert not has_adjacent_digits_that_arent_part_of_a_larger_group(123444) + assert has_adjacent_digits_that_arent_part_of_a_larger_group(111122) + + +def test_part1(): + assert part1() == 1099 + + +def test_part2(): + assert part2() == 710