mirror of
https://github.com/Findus23/AdventOfCode2019.git
synced 2024-08-27 19:52:12 +02:00
add day 4
This commit is contained in:
parent
65cd33567a
commit
353b96999b
2 changed files with 95 additions and 0 deletions
55
python/4/day4.py
Normal file
55
python/4/day4.py
Normal file
|
@ -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())
|
40
python/4/test_day4.py
Normal file
40
python/4/test_day4.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue