aoc

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

2.py (1058B) [raw]


      1 import sys
      2 
      3 def bad_level(report):
      4     delta = None
      5     prev = None
      6     safe = True
      7     for i in range(len(report)):
      8         n = report[i]
      9         if prev:
     10             d2 = n - prev
     11             if not delta:
     12                 delta = d2
     13             if abs(d2) > 3 or d2 == 0 or (delta and delta * d2 < 0):
     14                 return i
     15         prev = n
     16     return -1
     17 
     18 total = 0
     19 for l in sys.stdin:
     20     report = [int(x) for x in l.split()]
     21     t = total
     22 
     23     bad = bad_level(report)
     24     if bad == -1:
     25         total += 1
     26     else:
     27         # it could be that the bad index is any of:
     28         #  - the result returned by bad_level (1 10 2 3) -- bad causes spike
     29         #  - the index before that (bad - 1)  (10 1 2 3) -- prev bad causes spike
     30         #  - the index before that (bad - 2)  (1  2 1 0) -- sign change
     31         for i in [0, 1, 2]:
     32             if bad - i >= 0:
     33                 subrep = [report[j] for j in range(len(report)) if j != bad - i]
     34                 if bad_level(subrep) == -1:
     35                     total += 1
     36                     break
     37 
     38 print(total)