Skip to content

Commit 62bb0d4

Browse files
committed
Day 14
1 parent bee8bc4 commit 62bb0d4

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

2024/02/main.py

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1-
with open('test.txt') as file:
2-
data = file.read().strip().splitlines()
3-
data = [[int(y) for y in x.split()] for x in data]
1+
with open('input.txt') as file:
2+
data = [[int(y) for y in x.split()] for x in file.read().strip().splitlines()]
43

5-
for row in data:
6-
increasing = None
4+
5+
def is_safe(row):
6+
last = None
77
increment = None
88
for each in row:
9-
if increasing is None and increment is None:
9+
if last is None:
10+
last = each
11+
continue
12+
elif increment is None:
13+
inc = each - last
14+
increment = inc > 0
15+
if not 0 < abs(inc) < 4:
16+
return False
17+
else:
18+
inc = each - last
19+
if (inc > 0) != increment or not 0 < abs(inc) < 4:
20+
return False
21+
last = each
22+
return True
23+
24+
25+
total = 0
26+
save_with_removals = 0
27+
for row in data:
28+
if is_safe(row):
29+
total += 1
30+
else:
31+
for i in range(len(row)):
32+
if is_safe([row[y] for y in range(len(row)) if y != i]):
33+
save_with_removals += 1
34+
break
35+
36+
print(total)
37+
print(total + save_with_removals)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy