-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday04.py
executable file
·61 lines (40 loc) · 1.16 KB
/
day04.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
import sys
def grid_char(r, c):
global GRID, WIDTH, HEIGHT
if 0 <= r < HEIGHT and 0 <= c < WIDTH:
return GRID[r][c]
return ''
def count_xmas(r, c):
global GRID
if GRID[r][c] != 'X':
return 0
deltas = ((0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1))
count = 0
for dr, dc in deltas:
rr, cc = r, c
for i in range(3):
rr += dr
cc += dc
if grid_char(rr, cc) != 'MAS'[i]:
break
else:
count += 1
return count
def check_xmas(r, c):
global GRID
if GRID[r][c] != 'A':
return False
a = grid_char(r - 1, c - 1) + grid_char(r + 1, c + 1)
if a != 'MS' and a != 'SM':
return False
b = grid_char(r + 1, c - 1) + grid_char(r - 1, c + 1)
return b == 'MS' or b == 'SM'
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
GRID = fin.read().splitlines()
HEIGHT, WIDTH = len(GRID), len(GRID[0])
total1 = sum(count_xmas(r, c) for r in range(HEIGHT) for c in range(WIDTH))
print('Part 1:', total1)
total2 = sum(check_xmas(r, c) for r in range(1, HEIGHT - 1) for c in range(1, WIDTH - 1))
print('Part 2:', total2)