diff options
author | William Carroll <wpcarro@gmail.com> | 2020-01-15T14·21+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-01-15T14·21+0000 |
commit | 456d358cd7b0701b0ca2b8571d61f255d3da1488 (patch) | |
tree | cb4aac65aa1c6600f6ef773ffd58cd8579bb5084 /advent-of-code/day_4.py | |
parent | d480b6f08b8a6b32c58e5bbdf2f193432c50fb97 (diff) |
Upload my 2019 Advent of Code attempts
Well, unexpectedly (perhaps naively so), I only made it to Day 7. I created these before I stumbled upon the idea of the mono-repository; otherwise, I like to think I would have more granular commits introducing this work.
Diffstat (limited to 'advent-of-code/day_4.py')
-rw-r--r-- | advent-of-code/day_4.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/advent-of-code/day_4.py b/advent-of-code/day_4.py new file mode 100644 index 000000000000..adef73b452dc --- /dev/null +++ b/advent-of-code/day_4.py @@ -0,0 +1,35 @@ +import re + +start = 134792 +end = 675810 + + +def satisfies(x): + x = str(x) + result = False + double, not_decreasing = False, False + + # double and *only* double exists + for i in range(len(x) - 1): + # double and left-of-a is BOL or !x + # and right-of-b is EOL or !x + a, b = x[i], x[i + 1] + bol = i - 1 < 0 + eol = i + 2 >= len(x) + if a == b and (bol or x[i - 1] != a) and (eol or x[i + 2] != a): + double = True + break + + # not_decreasing + prev = int(x[0]) + for a in x[1:]: + a = int(a) + if prev > a: + return False + prev = a + not_decreasing = True + + return double and not_decreasing + + +print(len([x for x in range(start, end + 1) if satisfies(x)])) |