-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4.py
More file actions
48 lines (41 loc) · 1.52 KB
/
day4.py
File metadata and controls
48 lines (41 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
with open("inputs/day4.txt", "r") as inputFile:
lines = inputFile.read().split("\n")
# part 1
horiz, verts = 0, 0
cols = [""]*len(lines)
for i, row in enumerate(lines):
horiz += row.count("XMAS") + row.count("SAMX")
for j, c in enumerate(row):
cols[j] += c
for col in cols:
verts += col.count("XMAS") + col.count("SAMX")
diags = 0
for lineIndex, line in enumerate(lines):
if lineIndex > len(lines)-4:
break
for colIndex in range(len(line) - 3):
if lines[lineIndex][colIndex] in "SX":
downRight = "".join(lines[lineIndex+n][colIndex+n] for n in range(4))
if downRight in {"XMAS", "SAMX"}:
diags += 1
for colIndex in range(3, len(line)):
if lines[lineIndex][colIndex] in "SX":
downLeft = "".join(lines[lineIndex+n][colIndex-n] for n in range(4))
if downLeft in {"XMAS", "SAMX"}:
diags += 1
print(horiz + verts + diags)
# part 2
xmases = 0
for rowIndex, line in enumerate(lines):
for colIndex, c in enumerate(line):
if c == "A":
try:
surrounds = lines[rowIndex-1][colIndex-1] + \
lines[rowIndex-1][colIndex+1] + \
lines[rowIndex+1][colIndex-1] + \
lines[rowIndex+1][colIndex+1]
if surrounds in {"SSMM", "MMSS", "MSMS", "SMSM"}:
xmases += 1
except IndexError:
pass
print(xmases)