Solve day04 part 1

This commit is contained in:
Thom Dickson 2021-12-04 17:51:51 -05:00
parent f3fc4fbf6b
commit b51951a442
Signed by: boots
GPG Key ID: 40BE2AF8EBF8D2BB
2 changed files with 669 additions and 0 deletions

603
day04.in Normal file
View File

@ -0,0 +1,603 @@
31,88,35,24,46,48,95,42,18,43,71,32,92,62,97,63,50,2,60,58,74,66,15,87,57,34,14,3,54,93,75,22,45,10,56,12,83,30,8,76,1,78,82,39,98,37,19,26,81,64,55,41,16,4,72,5,52,80,84,67,21,86,23,91,0,68,36,13,44,20,69,40,90,96,27,77,38,49,94,47,9,65,28,59,79,6,29,61,53,11,17,73,99,25,89,51,7,33,85,70
50 83 3 31 16
47 9 94 10 86
61 22 53 46 74
77 41 79 55 62
97 78 43 73 40
99 96 20 35 21
38 17 48 69 68
9 51 32 52 11
67 8 42 89 27
39 62 66 72 43
33 16 4 78 31
96 66 13 55 18
47 89 83 99 85
50 43 39 34 98
81 65 7 23 17
24 13 57 84 50
83 86 98 92 7
28 31 85 21 12
37 48 43 47 67
19 27 1 20 16
38 75 3 14 4
8 86 98 94 83
60 46 63 85 20
69 26 73 40 29
48 84 33 18 74
13 33 37 45 22
19 28 61 58 69
42 14 23 39 88
92 81 54 99 52
57 3 34 29 62
19 71 46 13 81
99 34 8 7 89
72 56 38 22 27
52 2 44 12 4
53 86 45 95 39
67 12 16 60 47
79 21 99 15 59
81 13 64 83 4
85 48 17 29 66
41 97 80 51 68
72 19 67 6 9
63 80 78 97 43
53 73 91 44 47
3 54 41 61 70
69 36 57 55 45
97 7 39 48 10
77 42 65 89 79
24 58 23 37 15
26 71 41 18 87
50 88 98 43 1
76 50 48 10 77
27 13 18 35 24
31 72 41 64 2
16 43 36 81 26
66 51 30 34 74
93 99 19 72 58
7 76 80 94 23
87 59 30 77 49
53 88 51 4 36
90 38 64 70 46
36 38 45 13 68
12 35 57 64 29
71 74 15 0 49
77 21 27 84 65
22 23 60 17 10
84 31 99 93 98
71 73 48 38 83
12 74 34 57 45
2 9 76 79 77
0 51 72 33 29
65 43 15 53 89
75 55 99 59 48
6 85 68 30 39
5 0 47 81 95
96 31 23 87 73
23 88 98 43 56
4 89 53 34 41
33 37 24 27 19
22 83 72 75 31
68 95 77 1 49
14 48 20 73 11
27 1 5 61 60
96 99 9 64 29
42 92 59 95 81
69 97 78 86 16
69 10 41 13 90
75 95 99 72 29
7 85 42 77 16
88 19 2 45 64
14 0 83 43 70
74 85 45 50 6
92 19 43 97 65
56 11 77 5 28
16 10 54 44 63
3 93 75 12 51
40 43 92 21 90
5 62 74 34 25
88 47 65 37 83
15 6 10 99 89
78 56 35 75 9
96 74 41 81 31
35 94 48 44 21
99 11 32 15 43
91 34 85 23 7
54 77 89 13 26
18 58 19 28 69
74 41 91 88 15
11 86 44 99 45
79 93 80 55 4
70 56 37 84 78
48 95 57 70 84
31 73 35 77 68
4 53 32 63 13
46 3 71 88 37
72 65 36 50 49
75 9 36 94 3
71 62 65 0 15
18 31 57 35 38
6 16 22 34 95
66 29 52 73 68
83 54 24 26 96
36 2 3 34 95
16 77 11 56 91
80 10 93 42 59
88 47 76 55 79
51 76 4 75 7
17 98 78 12 66
1 31 52 30 45
74 29 6 87 90
32 9 88 13 34
97 92 40 73 76
21 15 34 35 45
1 27 48 78 46
95 43 17 16 20
62 28 52 56 68
16 86 55 23 30
20 73 83 89 35
42 38 87 59 69
3 79 85 43 78
84 19 18 17 33
10 43 3 68 56
16 52 45 77 25
75 73 66 46 82
41 80 99 11 93
71 79 37 5 84
9 76 96 14 52
67 74 86 32 4
6 28 31 27 23
56 58 25 69 38
82 91 26 15 57
96 62 34 67 53
99 5 27 45 63
80 38 0 71 43
75 49 33 36 2
15 21 54 20 81
96 59 72 6 38
60 70 76 82 46
47 53 51 64 98
44 25 69 81 33
73 52 10 74 55
52 25 99 11 60
56 63 39 43 2
34 45 59 8 30
51 92 90 86 98
19 80 47 69 13
11 98 55 6 39
70 26 99 57 75
52 41 81 3 5
96 92 94 35 46
24 78 40 58 95
81 87 93 88 29
61 2 11 72 31
60 76 19 36 58
71 43 69 94 45
99 9 62 48 30
84 87 15 67 54
13 81 97 8 92
43 60 5 19 0
91 20 69 1 29
23 7 74 28 53
73 68 24 64 47
81 35 23 95 39
51 69 94 37 21
97 48 66 91 55
56 18 49 9 86
67 96 91 73 44
77 10 50 81 19
63 55 46 95 97
8 69 40 70 61
31 20 92 98 72
36 81 69 98 59
39 15 96 9 23
14 84 88 89 90
45 34 22 64 50
86 32 53 77 55
20 62 23 29 77
13 0 14 92 42
5 88 8 1 16
80 79 84 49 40
46 96 71 76 25
17 65 37 3 35
23 22 95 91 36
61 11 51 64 85
81 75 53 88 62
59 14 29 73 57
91 87 11 35 98
34 1 28 27 10
92 40 64 24 43
55 49 42 0 36
93 19 45 21 71
82 86 14 10 43
44 87 62 85 38
31 67 3 68 64
56 36 79 78 58
21 95 35 90 18
90 60 20 27 80
39 30 12 83 96
49 4 11 98 76
74 37 54 26 19
35 43 92 62 34
36 23 45 24 63
66 34 32 67 30
26 0 5 69 50
21 80 96 38 93
49 46 61 41 16
52 97 64 34 74
28 46 31 56 75
44 35 63 77 8
7 68 71 18 38
61 91 49 26 15
83 80 10 38 45
81 99 30 3 63
57 96 82 55 76
75 41 86 94 46
59 42 40 68 48
48 43 92 50 21
37 56 8 38 94
73 74 35 3 52
7 29 82 98 86
57 79 22 1 14
53 46 4 76 28
30 80 13 69 86
54 70 40 77 71
58 24 59 37 91
45 51 43 90 74
5 33 59 78 84
1 90 49 72 27
76 12 31 86 11
74 18 52 47 19
17 16 34 25 82
41 42 21 31 44
70 10 8 16 55
82 60 77 89 43
38 4 58 90 94
74 71 93 88 61
60 95 12 74 56
82 3 48 22 27
67 49 4 42 39
18 35 43 87 45
76 63 54 21 19
35 89 76 86 32
49 9 0 91 99
87 26 97 22 44
21 19 48 84 33
98 30 50 90 53
62 77 8 16 96
73 65 39 79 78
12 55 86 99 60
9 22 71 98 2
24 70 75 50 41
46 55 77 38 26
70 19 72 88 23
91 84 56 51 99
49 69 90 48 14
93 76 63 92 71
16 76 31 17 24
14 95 34 12 75
37 50 74 73 41
68 56 58 23 84
63 26 55 15 54
35 65 20 19 61
56 3 40 66 26
36 44 13 18 78
8 12 9 48 51
0 93 53 71 95
6 63 5 47 48
81 86 43 73 69
55 83 36 4 33
23 96 88 38 32
52 85 60 53 2
27 88 14 49 89
17 75 34 87 96
76 48 95 60 98
46 22 29 30 6
3 94 63 77 83
63 98 18 73 80
37 56 95 60 53
6 97 59 17 55
20 74 24 96 79
19 31 61 0 38
93 52 54 25 51
97 94 76 31 82
53 74 87 65 89
22 62 92 15 73
17 95 1 32 43
5 44 76 22 33
16 91 48 42 29
10 13 25 69 51
97 7 64 60 88
32 86 74 39 68
15 60 30 58 32
2 92 49 70 1
29 90 85 93 59
88 95 61 55 57
19 8 97 10 45
49 83 66 38 97
68 81 69 92 47
70 32 98 4 63
37 25 84 80 54
31 56 51 74 57
86 75 61 68 26
82 81 25 69 44
62 70 23 37 43
29 98 39 54 33
87 93 15 79 58
50 54 78 51 91
71 70 27 28 76
49 1 48 11 83
98 4 56 86 67
44 23 16 17 94
84 78 3 44 96
59 86 70 80 48
93 88 52 43 61
95 66 46 62 58
5 25 6 85 99
66 40 33 10 52
38 30 99 79 60
75 72 59 2 53
20 83 43 76 44
48 46 63 15 84
54 80 53 36 95
59 41 5 82 52
55 56 22 33 15
37 10 81 79 27
42 98 83 23 28
94 26 80 60 62
91 57 58 59 39
38 29 41 86 88
11 46 66 73 95
78 63 12 40 89
57 77 46 88 69
45 89 71 43 35
56 52 30 29 8
68 39 64 66 28
10 47 80 7 19
57 37 63 90 88
47 10 22 58 46
95 71 24 60 23
0 45 75 50 77
73 26 36 7 79
26 79 66 87 72
94 29 17 57 81
64 91 28 27 89
95 25 4 31 86
85 34 6 21 76
70 35 89 57 42
34 54 64 71 61
11 97 92 22 10
0 81 78 7 53
63 65 39 2 25
11 5 24 28 10
63 35 69 49 65
42 4 60 57 6
1 2 22 81 66
70 9 86 50 64
9 73 85 6 43
74 24 30 76 89
38 67 60 42 78
34 22 20 69 92
71 79 35 17 0
66 61 87 49 7
60 25 39 69 27
41 76 59 95 45
16 99 64 34 1
74 62 9 75 18
24 15 47 80 0
99 92 29 67 64
94 27 85 97 19
55 75 46 91 52
32 8 76 61 14
95 10 21 53 63
94 90 56 13 71
76 42 17 35 65
31 29 57 8 64
77 30 16 79 61
85 57 3 67 31
62 46 55 63 18
95 37 71 0 24
23 32 12 96 89
29 17 79 82 6
21 53 44 78 99
73 98 85 41 8
39 19 28 27 81
75 38 37 74 66
47 46 6 29 14
58 13 76 91 23
1 99 81 69 86
45 36 22 53 16
30 71 89 18 49
87 95 60 75 98
30 61 64 54 80
22 47 84 16 8
83 18 65 70 11
81 23 98 26 82
45 69 6 53 68
38 29 43 78 85
67 39 99 98 52
76 82 51 3 72
46 19 65 93 34
90 0 7 20 74
85 6 67 50 45
75 79 32 2 94
22 60 95 34 78
90 3 58 98 61
63 26 76 42 89
28 64 47 36 5
76 41 26 79 10
14 56 92 95 22
32 54 13 98 19
45 11 69 71 20
90 46 64 38 73
48 49 28 45 98
77 30 35 81 78
32 92 19 34 12
69 74 6 89 61
36 10 29 33 37
64 7 81 31 79
56 15 28 51 78
2 92 50 9 23
48 73 32 4 39
86 82 78 41 21
22 66 65 0 47
46 43 29 77 45
37 88 49 90 19
40 10 96 13 38
96 30 45 80 77
27 82 83 64 22
24 56 11 20 51
55 54 2 59 14
76 67 90 93 46
11 50 90 29 33
92 81 8 19 47
25 66 74 22 73
28 3 97 40 67
53 71 48 49 57
26 78 35 27 66
98 10 88 43 86
93 30 75 46 56
23 92 34 4 85
28 38 42 3 39
28 96 83 99 97
61 41 73 48 23
44 7 89 49 60
39 76 85 26 9
82 53 98 2 15
84 57 27 91 69
20 43 13 9 61
28 18 17 71 6
48 58 55 96 24
56 95 34 33 15
24 49 88 55 75
39 95 59 80 51
35 0 56 7 25
9 1 77 64 18
50 34 54 57 99
60 78 56 14 90
44 30 48 15 12
22 54 2 33 79
34 4 76 93 29
38 58 35 18 5
81 22 3 41 80
0 77 72 87 30
97 99 38 69 13
91 71 24 56 9
36 44 21 79 53
88 31 62 15 77
25 39 37 53 20
44 0 48 4 47
29 73 49 8 72
68 79 84 56 41
86 48 70 56 67
68 7 73 55 10
38 82 65 22 62
51 2 34 17 53
47 0 28 39 83
27 18 39 0 48
84 74 64 80 60
28 96 37 65 57
53 79 89 32 14
55 63 50 7 62
EOF

66
day04.py Normal file
View File

@ -0,0 +1,66 @@
def check_win(board):
rows = [set(row) for row in board]
cols = [set(col) for col in
[[row[i] for row in board] for i in range(len(board[0]))]]
for row in rows:
if len(row) == 1 and row.issubset([1]):
return True
for col in cols:
if len(col) == 1 and col.issubset([1]):
return True
return False
def calc_score(board, checked, last_pick):
sum = 0
for i in range(len(board)):
for j in range(len(board[i])):
if checked[i][j] == 0:
sum += board[i][j]
return sum * last_pick
def get_win_board(boards, checked):
for board in range(len(boards)):
if check_win(checked[board]):
print("board", board, "won")
return board
return None
def part1():
with open("day04.in", "r") as f:
input_seq = [int(x) for x in f.readline().rstrip().split(",")]
f.readline()
boards = []
line = f.readline()
while line != "EOF\n":
board = []
while line != "\n":
board.append([int(x) for x in line.rstrip().split()])
line = f.readline()
boards.append(board)
line = f.readline()
checked = [[[0 for i in row] for row in board] for board in boards]
score = 0
for num in input_seq:
for board in range(len(boards)):
for row in range(len(boards[board])):
for cell in range(len(boards[board][row])):
if boards[board][row][cell] == num:
checked[board][row][cell] = 1
win_board = get_win_board(boards, checked)
if win_board is not None:
score = calc_score(boards[win_board], checked[win_board], num)
break
print("Winning score:", score)
def main():
print("=== PART 1 ===")
part1()
if __name__ == "__main__":
main()