aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

commit 1cdb49fe5bab260c9e2ca565f0b03a9a8eecac16 (patch)
parent 12204b1a35de45a1388599afe5608ec3cc37af35
Author: Alex Karle <alex@alexkarle.com>
Date:   Tue,  3 Dec 2024 02:44:10 +0100

2024: Golf day 2 pt 2 a bit

I'm fairly convinced this is true in the general case?

Diffstat:
M2024/02/2.py | 38+++++++++++++++++++-------------------
1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/2024/02/2.py b/2024/02/2.py @@ -1,38 +1,38 @@ import sys -def is_safe(report): +def bad_level(report): delta = None prev = None safe = True - for n in report: + for i in range(len(report)): + n = report[i] if prev: d2 = n - prev if not delta: delta = d2 if abs(d2) > 3 or d2 == 0 or (delta and delta * d2 < 0): - safe = False - break + return i prev = n - return safe + return -1 total = 0 for l in sys.stdin: report = [int(x) for x in l.split()] + t = total - safe = is_safe(report) - if not safe: - # feels like there should be a faster method than brute forcing - # every combination? Escaping me right now. - # The catch is that in cases like 1 3 2 3 4, we want to drop - # the first 3, but it doesn't _look_ bad until we see the 2. - # When we see the bad 2, we would have to try to drop the 2 - for i in range(len(report)): - subrep = [report[j] for j in range(len(report)) if j != i] - safe = is_safe(subrep) - if safe: - break - - if safe: + bad = bad_level(report) + if bad == -1: total += 1 + else: + # it could be that the bad index is any of: + # - the result returned by bad_level (1 10 2 3) -- bad causes spike + # - the index before that (bad - 1) (10 1 2 3) -- prev bad causes spike + # - the index before that (bad - 2) (1 2 1 0) -- sign change + for i in [0, 1, 2]: + if bad - i >= 0: + subrep = [report[j] for j in range(len(report)) if j != bad - i] + if bad_level(subrep) == -1: + total += 1 + break print(total)