2021 Day 4 & Day 5 Complete

This commit is contained in:
Brian Buller 2021-12-05 11:29:45 -06:00
parent f3fe5c523b
commit 70569e043a
6 changed files with 1530 additions and 0 deletions

601
2021/day04/input Normal file
View File

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

197
2021/day04/main.go Normal file
View File

@ -0,0 +1,197 @@
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
boards := buildBoards(inp[1:])
draws := parseDraws(inp[0])
//part1(boards, draws)
part2(boards, draws)
}
func part1(boards []Board, draws []int) {
var winners []Board
for i := range draws {
for b := range boards {
if boards[b].Mark(draws[i]) {
winners = append(winners, boards[b])
}
}
if len(winners) > 0 {
break
}
}
for w := range winners {
fmt.Println("# Part 1")
fmt.Println(winners[w])
fmt.Println(winners[w].Score())
fmt.Println()
}
}
func part2(boards []Board, draws []int) {
var winners []Board
for i := range draws {
for b := range boards {
if boards[b].Mark(draws[i]) {
winners = append(winners, boards[b])
}
}
// Remove all winners from boards
for i := range winners {
for b := range boards {
if winners[i].String() == boards[b].String() {
boards = append(boards[:b], boards[b+1:]...)
break
}
}
}
if len(boards) == 0 {
break
}
}
fmt.Println("# Part 2")
fmt.Println("Last Winner")
fmt.Println(winners[len(winners)-1])
fmt.Println(winners[len(winners)-1].Score())
fmt.Println()
}
func parseDraws(inp string) []int {
var ret []int
pts := strings.Split(inp, ",")
for i := range pts {
ret = append(ret, h.Atoi(pts[i]))
}
return ret
}
type Spot struct {
value int
marked bool
}
type Board struct {
spots map[h.Coordinate]Spot
draws []int
}
func NewBoard(inp []string) Board {
b := Board{
spots: make(map[h.Coordinate]Spot),
}
for y := range inp {
row := strings.Fields(strings.TrimSpace(inp[y]))
for x := range row {
v := h.Atoi(row[x])
b.spots[h.Coordinate{X: x, Y: y}] = Spot{value: v}
}
}
return b
}
func buildBoards(inp []string) []Board {
var boards []Board
var boardStr []string
for i := range inp {
if inp[i] == "" {
// start/end of a board
if len(boardStr) == 5 {
boards = append(boards, NewBoard(boardStr))
}
boardStr = []string{}
} else {
boardStr = append(boardStr, inp[i])
}
}
if len(boardStr) == 5 {
boards = append(boards, NewBoard(boardStr))
}
return boards
}
// Mark adds a number to the draws and returns whether this board has won
func (b *Board) Mark(v int) bool {
b.draws = append(b.draws, v)
for k := range b.spots {
s := b.spots[k]
if b.spots[k].value == v {
s.marked = true
b.spots[k] = s
return b.Won()
}
}
return b.Won()
}
func (b *Board) Won() bool {
for y := 0; y < 5; y++ {
if b.CheckRowForWin(y) {
return true
}
}
for x := 0; x < 5; x++ {
if b.CheckColForWin(x) {
return true
}
}
return false
}
func (b *Board) CheckRowForWin(y int) bool {
for x := 0; x < 5; x++ {
if b.spots[c(x, y)].marked == false {
return false
}
}
return true
}
func (b *Board) CheckColForWin(x int) bool {
for y := 0; y < 5; y++ {
if b.spots[c(x, y)].marked == false {
return false
}
}
return true
}
func (b *Board) Score() int {
var unmarked int
for y := 0; y < 5; y++ {
for x := 0; x < 5; x++ {
if b.spots[c(x, y)].marked == false {
unmarked += b.spots[c(x, y)].value
}
}
}
return unmarked * b.draws[len(b.draws)-1]
}
func (b Board) String() string {
var ret string
for y := 0; y < 5; y++ {
for x := 0; x < 5; x++ {
c := c(x, y)
marked := " "
if b.spots[c].marked {
marked = "X"
}
ret = fmt.Sprintf("%s%s: %2d[%v] ", ret, c, b.spots[c].value, marked)
}
ret = ret + "\n"
}
return ret
}
func c(x, y int) h.Coordinate {
return h.Coordinate{X: x, Y: y}
}

19
2021/day04/testinput Normal file
View File

@ -0,0 +1,19 @@
7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19
3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6
14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7

500
2021/day05/input Normal file
View File

@ -0,0 +1,500 @@
644,38 -> 644,265
941,468 -> 941,89
807,552 -> 618,363
896,510 -> 896,744
227,909 -> 227,745
24,66 -> 946,988
563,529 -> 563,270
894,707 -> 359,172
146,253 -> 146,569
544,683 -> 140,683
755,612 -> 755,463
394,320 -> 891,320
68,616 -> 68,628
801,770 -> 801,942
650,290 -> 580,360
376,18 -> 181,213
285,391 -> 805,911
57,153 -> 501,153
686,301 -> 686,316
81,971 -> 683,369
75,456 -> 75,355
769,59 -> 769,81
49,958 -> 49,718
492,754 -> 492,256
606,133 -> 391,133
134,683 -> 134,656
195,463 -> 658,926
982,498 -> 982,927
841,122 -> 889,74
968,891 -> 968,189
104,941 -> 104,868
801,450 -> 272,979
212,749 -> 212,366
880,928 -> 880,757
503,483 -> 283,263
792,924 -> 792,540
58,87 -> 905,934
275,661 -> 36,422
475,654 -> 466,645
34,946 -> 969,11
725,664 -> 725,150
837,680 -> 837,952
109,987 -> 644,452
860,984 -> 860,691
672,800 -> 107,235
216,888 -> 301,888
399,676 -> 718,357
957,544 -> 784,544
677,743 -> 633,743
491,866 -> 77,452
967,949 -> 45,27
659,699 -> 659,27
987,116 -> 696,116
465,847 -> 309,847
353,19 -> 353,627
265,713 -> 592,386
541,765 -> 461,765
21,409 -> 895,409
950,149 -> 766,149
856,889 -> 895,889
335,962 -> 485,812
425,774 -> 400,749
71,932 -> 989,14
484,974 -> 44,974
86,797 -> 86,470
876,962 -> 876,384
45,631 -> 169,507
161,789 -> 523,427
791,640 -> 791,581
415,170 -> 835,590
299,275 -> 988,964
105,233 -> 231,359
202,226 -> 202,958
814,717 -> 618,521
185,442 -> 559,68
26,149 -> 665,149
673,369 -> 247,795
171,963 -> 171,46
689,801 -> 94,206
619,243 -> 619,637
426,245 -> 660,11
47,503 -> 47,179
341,363 -> 487,217
371,774 -> 371,871
781,794 -> 781,180
391,632 -> 92,632
517,150 -> 517,715
903,10 -> 18,895
34,500 -> 426,500
82,955 -> 886,151
142,297 -> 142,527
60,965 -> 36,965
250,807 -> 372,685
227,393 -> 610,776
269,893 -> 269,556
969,223 -> 611,223
255,92 -> 194,92
220,233 -> 958,233
144,209 -> 144,979
48,413 -> 48,195
209,151 -> 648,590
867,648 -> 871,644
499,555 -> 807,555
571,729 -> 865,435
683,151 -> 485,151
803,26 -> 803,691
533,921 -> 300,688
625,695 -> 483,553
719,370 -> 139,950
981,17 -> 16,982
367,617 -> 367,636
249,644 -> 835,644
260,825 -> 260,519
275,144 -> 275,315
767,314 -> 962,119
76,625 -> 76,166
403,680 -> 313,680
977,21 -> 16,982
578,753 -> 271,753
212,358 -> 477,623
596,220 -> 868,220
67,797 -> 935,797
181,107 -> 181,379
741,332 -> 419,10
732,827 -> 732,989
835,111 -> 436,510
480,328 -> 480,682
327,673 -> 327,70
911,547 -> 833,625
944,509 -> 874,579
169,340 -> 731,902
156,842 -> 825,173
976,40 -> 51,965
199,416 -> 916,416
51,970 -> 792,229
14,577 -> 501,577
246,464 -> 246,289
641,464 -> 100,464
984,11 -> 43,952
548,36 -> 156,428
519,799 -> 519,43
332,364 -> 332,774
85,123 -> 85,753
778,12 -> 40,750
507,56 -> 507,840
973,632 -> 670,632
895,928 -> 37,70
661,784 -> 661,718
114,25 -> 938,849
428,752 -> 428,543
357,270 -> 957,870
27,70 -> 899,942
85,780 -> 747,780
717,565 -> 951,565
191,748 -> 651,748
679,301 -> 679,417
150,266 -> 150,184
774,964 -> 64,254
34,248 -> 34,279
782,610 -> 802,610
109,146 -> 822,859
825,848 -> 825,666
251,718 -> 615,718
912,722 -> 289,722
729,579 -> 729,889
214,756 -> 214,716
58,497 -> 58,927
62,55 -> 742,55
110,429 -> 110,154
55,794 -> 638,794
182,96 -> 676,96
632,334 -> 202,764
36,541 -> 36,397
191,819 -> 191,277
971,491 -> 839,491
849,561 -> 538,250
176,523 -> 10,689
162,638 -> 717,638
132,843 -> 646,329
873,67 -> 873,389
167,631 -> 167,473
49,337 -> 465,337
550,429 -> 438,429
305,720 -> 476,720
547,636 -> 547,902
21,627 -> 49,627
286,70 -> 989,70
87,930 -> 896,121
659,916 -> 200,916
234,589 -> 234,308
530,962 -> 90,962
366,478 -> 676,788
284,520 -> 284,546
580,74 -> 146,508
561,977 -> 561,237
85,150 -> 804,869
740,850 -> 159,269
458,705 -> 458,639
969,563 -> 381,563
139,439 -> 139,263
135,971 -> 908,198
440,632 -> 102,632
446,549 -> 446,734
24,49 -> 212,237
692,882 -> 527,882
156,895 -> 156,239
359,246 -> 359,603
12,349 -> 604,941
375,161 -> 732,161
674,287 -> 322,287
902,957 -> 328,383
352,185 -> 352,766
222,205 -> 222,419
769,815 -> 769,739
83,147 -> 880,147
136,148 -> 136,279
807,241 -> 439,241
464,240 -> 478,254
37,839 -> 646,839
351,316 -> 639,604
449,492 -> 449,125
69,892 -> 933,28
565,161 -> 598,161
147,802 -> 905,44
809,861 -> 38,90
34,80 -> 34,538
896,405 -> 582,405
474,201 -> 893,201
289,64 -> 975,750
378,785 -> 375,782
709,472 -> 357,472
17,14 -> 987,984
625,46 -> 647,46
20,84 -> 914,978
364,811 -> 397,811
395,726 -> 628,726
831,89 -> 593,89
370,562 -> 827,562
278,371 -> 609,40
904,529 -> 391,16
232,829 -> 232,63
927,121 -> 194,854
547,280 -> 547,895
292,904 -> 292,897
99,525 -> 99,868
371,44 -> 643,44
942,74 -> 986,118
708,179 -> 110,179
485,637 -> 713,637
477,512 -> 102,137
636,657 -> 727,657
686,564 -> 256,564
636,169 -> 377,169
627,943 -> 627,703
987,795 -> 987,758
364,827 -> 642,827
613,694 -> 864,945
910,521 -> 777,654
119,394 -> 22,394
594,439 -> 515,360
177,11 -> 177,73
456,628 -> 456,467
246,214 -> 594,562
47,790 -> 47,491
788,21 -> 788,343
136,847 -> 136,543
958,302 -> 733,302
50,981 -> 923,108
298,179 -> 298,921
962,644 -> 93,644
988,671 -> 988,258
646,140 -> 646,852
721,264 -> 721,563
972,407 -> 684,119
245,422 -> 401,422
16,494 -> 16,16
564,72 -> 361,72
436,390 -> 166,120
511,571 -> 241,571
259,215 -> 259,975
648,841 -> 898,841
918,881 -> 438,401
458,561 -> 752,855
791,192 -> 319,192
383,929 -> 217,929
733,26 -> 527,26
620,160 -> 620,734
818,181 -> 61,181
39,21 -> 927,909
952,208 -> 952,749
194,55 -> 304,55
519,673 -> 519,875
730,919 -> 733,919
963,269 -> 933,299
707,112 -> 178,112
924,349 -> 264,349
910,94 -> 96,94
747,289 -> 89,947
164,956 -> 164,655
264,300 -> 753,789
207,672 -> 207,488
243,838 -> 812,269
455,320 -> 425,320
59,964 -> 59,841
350,373 -> 526,549
604,683 -> 604,83
537,281 -> 537,933
737,634 -> 737,28
92,909 -> 725,276
859,335 -> 859,487
605,495 -> 371,495
783,155 -> 783,930
388,591 -> 388,133
374,634 -> 472,634
963,914 -> 64,15
57,435 -> 140,435
759,619 -> 105,619
326,501 -> 326,821
942,136 -> 414,136
490,376 -> 490,260
377,59 -> 377,773
894,169 -> 99,964
350,511 -> 726,511
787,728 -> 787,750
688,11 -> 688,68
107,514 -> 183,514
861,12 -> 22,851
149,49 -> 149,713
826,737 -> 890,737
299,307 -> 299,549
251,648 -> 862,37
898,85 -> 412,85
936,168 -> 845,168
278,677 -> 302,677
493,878 -> 493,133
539,205 -> 539,522
412,495 -> 412,929
334,605 -> 334,817
68,462 -> 462,856
234,134 -> 234,942
293,823 -> 678,823
852,874 -> 158,874
859,612 -> 435,612
10,876 -> 613,273
370,13 -> 118,13
616,774 -> 14,172
193,543 -> 81,543
784,179 -> 14,949
324,533 -> 153,533
15,976 -> 976,15
943,52 -> 20,975
100,34 -> 671,605
140,552 -> 140,27
448,497 -> 186,497
734,355 -> 734,933
544,131 -> 259,131
193,84 -> 663,84
190,949 -> 833,306
430,34 -> 328,34
21,46 -> 483,508
621,202 -> 98,202
196,874 -> 889,181
106,217 -> 850,961
701,891 -> 488,678
21,597 -> 894,597
249,21 -> 249,614
887,808 -> 887,948
255,711 -> 913,53
173,447 -> 173,78
956,224 -> 956,747
513,882 -> 48,417
772,591 -> 930,591
976,98 -> 86,988
264,67 -> 264,931
987,982 -> 48,43
494,514 -> 494,416
867,785 -> 556,474
350,902 -> 257,902
978,977 -> 978,735
312,240 -> 371,240
890,112 -> 890,662
753,327 -> 427,653
83,888 -> 927,44
142,480 -> 12,480
965,39 -> 85,919
158,926 -> 158,801
439,22 -> 439,860
10,26 -> 765,781
775,838 -> 775,732
871,607 -> 871,288
337,850 -> 337,533
89,581 -> 89,582
524,977 -> 524,88
473,283 -> 639,449
23,324 -> 23,372
671,486 -> 278,879
266,98 -> 989,98
51,947 -> 634,947
47,18 -> 47,43
95,239 -> 609,753
953,34 -> 71,916
243,685 -> 243,427
642,537 -> 304,199
545,41 -> 914,41
112,860 -> 112,159
12,12 -> 989,989
622,907 -> 648,907
230,983 -> 230,941
111,868 -> 852,127
315,895 -> 799,411
771,533 -> 584,720
65,492 -> 657,492
820,315 -> 820,627
710,554 -> 829,554
100,967 -> 877,190
530,733 -> 530,202
880,28 -> 83,825
914,448 -> 494,28
886,344 -> 966,344
782,923 -> 857,848
597,632 -> 597,946
366,789 -> 366,96
568,834 -> 187,834
451,687 -> 224,687
123,12 -> 914,803
734,42 -> 526,42
844,116 -> 844,521
744,976 -> 744,792
337,837 -> 617,837
848,139 -> 848,571
853,264 -> 112,264
986,965 -> 92,71
888,892 -> 53,57
150,342 -> 150,516
457,264 -> 796,264
544,595 -> 544,406
243,118 -> 482,357
138,804 -> 138,646
99,80 -> 99,34
442,883 -> 442,165
428,72 -> 428,826
450,815 -> 901,364
852,516 -> 852,384
13,969 -> 972,10
921,50 -> 32,939
969,470 -> 969,539
691,238 -> 763,238
171,733 -> 823,81
722,963 -> 21,262
849,203 -> 849,151
12,99 -> 884,971
903,240 -> 148,240
633,140 -> 633,202
194,393 -> 747,393
748,43 -> 126,665
182,155 -> 182,298
668,814 -> 668,306
619,384 -> 255,384
166,341 -> 795,970
898,870 -> 57,29
976,148 -> 976,244
386,525 -> 662,249
719,67 -> 131,655
419,847 -> 188,847
953,823 -> 743,823
307,299 -> 307,78
948,253 -> 756,253
926,938 -> 46,58
982,618 -> 652,618
539,742 -> 539,177
531,974 -> 531,781
880,922 -> 283,325
707,257 -> 707,673
327,308 -> 930,911
526,170 -> 980,624
445,653 -> 445,986
842,737 -> 754,737
847,982 -> 16,151
137,426 -> 292,271
126,556 -> 50,556
168,766 -> 168,767
152,46 -> 363,46
50,381 -> 760,381
43,943 -> 43,374
45,964 -> 956,53
37,403 -> 576,403
130,317 -> 576,763
882,840 -> 179,137
572,608 -> 572,274
874,977 -> 32,135
794,56 -> 730,120
812,841 -> 812,440
111,66 -> 137,92
255,287 -> 255,787
121,137 -> 121,63
81,811 -> 81,586
303,365 -> 910,972
177,134 -> 962,919
405,41 -> 405,645
219,934 -> 441,934

203
2021/day05/main.go Normal file
View File

@ -0,0 +1,203 @@
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
var minX, maxX, minY, maxY int
func main() {
maxX = h.MIN_INT
minX = h.MAX_INT
maxY = h.MIN_INT
minY = h.MAX_INT
inp := h.StdinToStringSlice()
lines := parseLines(inp)
fmt.Println("# Part 1")
findDangerSpots(filterStraight(lines))
fmt.Println("# Part 2")
findDangerSpots(lines)
}
var ventMap map[h.Coordinate]int
var dangerSpots []h.Coordinate
func dingMap(c h.Coordinate) {
s := ventMap[c]
s++
if s == 2 {
dangerSpots = append(dangerSpots, c)
}
ventMap[c] = s
}
func findDangerSpots(lines []Line) {
ventMap = make(map[h.Coordinate]int)
for i := range lines {
updateMinMax(lines[i])
}
for _, l := range lines {
// Find all points on this line
// All lines are 0 or 45 deg, so slope should always be 0,1,-1,INF,-INF
if l.start.X == l.end.X { // INF, -INF: vertical
for y := l.lowestY(); y <= l.highestY(); y++ {
dingMap(h.Coordinate{X: l.start.X, Y: y})
}
} else if l.start.Y == l.end.Y { //0: horizontal
for x := l.start.X; x <= l.end.X; x++ {
dingMap(h.Coordinate{Y: l.start.Y, X: x})
}
} else { // 1 or -1
y := l.start.Y
x := l.start.X
dingMap(h.Coordinate{Y: y, X: x})
diffY := -1
if l.start.Y < l.end.Y {
diffY = 1
}
for x != l.end.X && y != l.end.Y {
x += 1
y += diffY
dingMap(h.Coordinate{Y: y, X: x})
}
}
}
fmt.Println("Danger Spots:", len(dangerSpots))
}
type Line struct {
start h.Coordinate
end h.Coordinate
stX float64
stY float64
edX float64
edY float64
slope float64
}
func (l *Line) lowestY() int {
if l.start.Y < l.end.Y {
return l.start.Y
}
return l.end.Y
}
func (l *Line) highestY() int {
if l.start.Y > l.end.Y {
return l.start.Y
}
return l.end.Y
}
func (l *Line) intersects(c h.Coordinate) bool {
ptX := float64(c.X)
ptY := float64(c.Y)
if (c.X == l.start.X && c.Y == l.start.Y) || (c.X == l.end.X && c.Y == l.end.Y) {
return true
}
if c.X < l.start.X || c.X > l.end.X {
return false
}
if (c.Y < l.start.Y && c.Y < l.end.Y) || (c.Y > l.start.Y && c.Y > l.end.Y) {
return false
}
// Check if our slope is infinite
if l.start.X == l.end.X {
if l.start.Y < l.end.Y {
return c.X == l.start.X && c.Y >= l.start.Y && c.Y <= l.end.Y
} else {
return c.X == l.start.X && c.Y <= l.start.Y && c.Y >= l.end.Y
}
}
return slope(ptX, ptY, l.stX, l.stY) == l.slope
}
func (l Line) String() string {
return fmt.Sprintf("%d,%d -> %d,%d; [%f]", l.start.X, l.start.Y, l.end.X, l.end.Y, l.slope)
}
func LineFromString(str string) Line {
start := h.Coordinate{}
end := h.Coordinate{}
r := strings.NewReader(str)
_, err := fmt.Fscanf(r, "%d,%d -> %d,%d", &start.X, &start.Y, &end.X, &end.Y)
if err != nil {
panic(err)
}
var l Line
if start.X < end.X {
l = Line{
start: start,
end: end,
}
} else {
l = Line{
start: end,
end: start,
}
}
l.stX = float64(l.start.X)
l.stY = float64(l.start.Y)
l.edX = float64(l.end.X)
l.edY = float64(l.end.Y)
l.slope = slope(l.stX, l.stY, l.edX, l.edY)
return l
}
func updateMinMax(line Line) {
if line.start.X < minX {
minX = line.start.X
}
if line.start.X > maxX {
maxX = line.start.X
}
if line.end.X < minX {
minX = line.end.X
}
if line.end.X > maxX {
maxX = line.end.X
}
if line.start.Y < minY {
minY = line.start.Y
}
if line.start.Y > maxY {
maxY = line.start.Y
}
if line.end.Y < minY {
minY = line.end.Y
}
if line.end.Y > maxY {
maxY = line.end.Y
}
}
func parseLines(inp []string) []Line {
var ret []Line
for i := range inp {
ret = append(ret, LineFromString(inp[i]))
}
return ret
}
func filterStraight(lines []Line) []Line {
var ret []Line
for i := range lines {
if lines[i].start.X == lines[i].end.X || lines[i].start.Y == lines[i].end.Y {
ret = append(ret, lines[i])
}
}
return ret
}
func slope(x1, y1, x2, y2 float64) float64 {
return (y1 - y2) / (x1 - x2)
}

10
2021/day05/testinput Normal file
View File

@ -0,0 +1,10 @@
0,9 -> 5,9
8,0 -> 0,8
9,4 -> 3,4
2,2 -> 2,1
7,0 -> 7,4
6,4 -> 2,0
0,9 -> 2,9
3,4 -> 1,4
0,0 -> 8,8
5,5 -> 8,2