master
George Wensor 3 years ago
parent cf61ecf2ab
commit 7c828a4104

1
.gitignore vendored

@ -1,2 +1 @@
.vscode
*.txt

File diff suppressed because it is too large Load Diff

@ -0,0 +1,16 @@
const fs = require("fs")
const input = fs.readFileSync("input.txt", "utf-8", err => {
err && console.error(err)
}).split("\n").map(num => parseInt(num))
let partOne = 0, partTwo = 0
for (let i = 0; i < input.length; i++){
if (input[i - 1] && input[i] > input[i - 1])
partOne++
if (input[i - 3] && input[i] > input[i - 3])
partTwo++
}
console.log(partOne, partTwo)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,23 @@
const fs = require("fs")
const input = fs.readFileSync("input.txt", "utf-8", err => {
err && console.error(err)
}).split("\n").map(line => line.split(" "))
let horizontalPosition = 0, depth = 0, aim = 0
for (let [direction, distance] of input){
distance = parseInt(distance)
switch(direction){
case "forward":
horizontalPosition += distance
depth += aim * distance
break
case "up":
aim -= distance
break
case "down":
aim += distance
}
}
console.log(horizontalPosition * aim, horizontalPosition * depth)

File diff suppressed because it is too large Load Diff

@ -0,0 +1,92 @@
package main
import (
"bufio"
"log"
"os"
"sort"
"strconv"
)
const bitSize = 40
func bitComplement(num uint64) uint64 {
return ^num & (1<<bitSize - 1)
}
func partOne(counts *[bitSize]int, total *int) (uint64, uint64) {
averageThreshold := *total / 2
var gammaRate uint64 = 0
for index, count := range counts {
if count > averageThreshold {
var add uint64 = 1 << (bitSize - 1 - index)
gammaRate += add
}
}
epsilonRate := bitComplement(gammaRate)
return gammaRate, epsilonRate
}
func reduce(slice []string, depth int, max bool) int64 {
var s []string
half := len(slice) / 2
if half == 0 {
ret, _ := strconv.ParseInt(slice[0], 2, 64)
return ret
}
maxDigit := slice[half][depth]
for _, str := range slice {
if max && str[depth] == maxDigit {
s = append(s, str)
}
if !max && str[depth] != maxDigit {
s = append(s, str)
}
}
return reduce(s, depth+1, max)
}
func partTwo(s []string) (int64, int64) {
sort.Strings(s)
return reduce(s, 0, true), reduce(s, 0, false)
}
func solve() {
file, err := os.Open("bigboi.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var counts [bitSize]int
var s []string
total := 0
for scanner.Scan() {
line := scanner.Text()
for index, character := range line {
if character == '1' {
counts[index]++
}
}
s = append(s, line)
total++
}
partOne(&counts, &total)
//fmt.Println(int64(g) * int64(e))
partTwo(s)
//fmt.Println(o * c)
}
func main() {
solve()
}

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

@ -0,0 +1,51 @@
const fs = require("fs")
const input = fs.readFileSync("input.txt", "utf-8", err => {
err && console.error(err)
}).split("\n\n")
const draws = input[0].split(",")
const boards = []
// parse and set up 5x5 boards
for (let i = 1; i < input.length; i++){
const str = input[i]
const b = [...str.matchAll(/[0-9]+/g)].flat()
const board = []
while (b.length) {
board.push(b.splice(0, 5))
}
boards.push(board)
}
const total = boards.length
// draw numbers and find winners
for (let drawCount = 5; drawCount < draws.length; drawCount++){
const drawSet = new Set(draws.slice(0, drawCount))
for (const [boardIndex, board] of boards.entries()){
for (const row of board){
checkWin(row, drawSet, board, boardIndex, drawCount, boards.length == total, boards.length == 1)
}
for (let colNum = 0; colNum < 5; colNum++){
const column = []
for (let j = 0; j < 5; j++){
column.push(board[j][colNum])
}
checkWin(column, drawSet, board, boardIndex, drawCount, boards.length == total, boards.length == 1)
}
}
}
function /* keith */ checkWin(line, drawSet, board, boardIndex, drawCount, isFirst, isLast) {
const matches = line.filter(num => drawSet.has(num))
if (matches.length == 5){
const win = board.flat().filter(num => !drawSet.has(num)).reduce((a,b) => parseInt(a) + parseInt(b))
if (isFirst || isLast){
console.log(`${isFirst ? "First winner score:" : "Last Winner score:"}`, win * draws[drawCount-1])
}
boards.splice(boardIndex, 1)
}
}

@ -0,0 +1,500 @@
409,872 -> 409,963
149,412 -> 281,280
435,281 -> 435,362
52,208 -> 969,208
427,265 -> 884,265
779,741 -> 779,738
949,41 -> 13,977
145,690 -> 145,180
513,665 -> 513,869
405,174 -> 405,612
943,504 -> 93,504
230,808 -> 570,468
69,278 -> 69,30
35,336 -> 911,336
812,83 -> 812,197
981,962 -> 29,10
863,709 -> 371,709
301,963 -> 955,309
187,101 -> 187,227
85,762 -> 85,301
587,362 -> 652,427
73,359 -> 73,139
124,449 -> 124,380
432,659 -> 30,659
96,728 -> 756,68
957,215 -> 957,868
64,779 -> 64,692
315,403 -> 854,942
890,663 -> 213,663
606,864 -> 264,864
81,446 -> 81,890
171,463 -> 395,463
766,639 -> 912,785
10,163 -> 10,835
65,906 -> 258,906
975,364 -> 408,364
595,728 -> 910,728
29,274 -> 705,274
42,965 -> 50,965
815,588 -> 959,732
293,344 -> 484,344
579,220 -> 579,949
832,951 -> 72,951
56,296 -> 56,730
318,589 -> 181,589
32,33 -> 983,984
355,794 -> 448,887
362,25 -> 696,25
374,207 -> 144,207
851,66 -> 851,709
404,98 -> 414,98
274,601 -> 787,601
898,100 -> 223,775
883,638 -> 73,638
12,989 -> 942,59
30,590 -> 146,474
46,711 -> 693,64
295,925 -> 295,854
744,89 -> 773,89
954,55 -> 33,976
647,361 -> 130,361
985,410 -> 606,410
377,884 -> 297,884
215,961 -> 981,195
176,422 -> 176,759
522,216 -> 151,216
372,149 -> 115,406
80,92 -> 218,230
350,754 -> 967,754
481,944 -> 711,714
176,795 -> 845,126
944,568 -> 944,21
86,302 -> 628,302
312,691 -> 312,640
662,874 -> 168,874
336,98 -> 336,842
289,69 -> 811,591
824,777 -> 256,209
374,311 -> 374,905
663,254 -> 137,780
183,248 -> 780,845
860,32 -> 157,32
955,794 -> 804,794
461,179 -> 96,544
780,264 -> 475,264
205,583 -> 205,645
707,364 -> 470,364
735,364 -> 551,180
127,479 -> 680,479
305,162 -> 305,85
348,349 -> 348,806
892,94 -> 88,898
340,593 -> 416,593
872,175 -> 93,954
389,750 -> 174,750
372,661 -> 949,661
121,256 -> 352,487
636,204 -> 99,741
388,328 -> 400,316
664,333 -> 549,333
89,92 -> 884,887
184,373 -> 552,741
464,949 -> 823,590
69,713 -> 767,713
302,33 -> 302,160
502,110 -> 83,529
431,331 -> 210,331
236,588 -> 931,588
931,315 -> 406,315
154,272 -> 49,272
723,159 -> 368,159
397,615 -> 402,620
431,796 -> 134,499
378,837 -> 378,670
582,28 -> 582,789
374,967 -> 445,896
723,874 -> 30,874
847,767 -> 920,767
761,101 -> 761,338
711,833 -> 419,833
953,335 -> 953,479
840,614 -> 410,614
483,485 -> 757,759
694,472 -> 694,930
508,275 -> 754,275
815,419 -> 700,534
399,843 -> 399,804
909,771 -> 95,771
796,893 -> 332,429
833,488 -> 833,899
478,240 -> 478,718
343,720 -> 343,285
320,204 -> 320,213
410,794 -> 240,964
848,227 -> 214,227
49,919 -> 880,88
273,648 -> 187,648
746,313 -> 746,590
715,527 -> 307,935
630,771 -> 630,815
780,670 -> 710,670
811,789 -> 964,789
397,267 -> 397,769
496,510 -> 744,758
314,62 -> 89,62
217,417 -> 217,865
680,862 -> 680,739
150,902 -> 816,236
504,120 -> 45,120
79,43 -> 967,931
584,197 -> 532,197
893,717 -> 489,313
609,759 -> 684,684
146,24 -> 386,24
704,645 -> 866,645
24,329 -> 44,309
417,741 -> 176,982
12,48 -> 12,138
522,359 -> 937,359
165,269 -> 55,159
207,537 -> 610,940
364,213 -> 323,213
144,750 -> 144,26
622,840 -> 488,706
909,201 -> 794,201
959,185 -> 579,565
848,720 -> 848,322
81,159 -> 81,652
98,174 -> 841,917
364,437 -> 364,568
884,90 -> 973,90
962,931 -> 60,29
915,967 -> 681,733
853,859 -> 853,809
630,362 -> 630,161
848,455 -> 672,279
50,212 -> 419,212
415,719 -> 413,719
818,274 -> 813,274
206,779 -> 206,197
251,729 -> 715,729
981,873 -> 452,344
945,708 -> 704,467
875,38 -> 76,837
163,799 -> 435,799
300,821 -> 913,208
978,15 -> 39,954
724,167 -> 191,167
342,600 -> 342,405
190,17 -> 945,772
844,659 -> 176,659
591,135 -> 50,135
252,353 -> 252,527
389,906 -> 389,861
504,387 -> 39,852
108,567 -> 108,148
60,600 -> 60,658
903,968 -> 115,180
633,34 -> 325,342
186,536 -> 708,14
588,276 -> 74,790
596,837 -> 596,909
619,278 -> 737,160
368,953 -> 245,830
112,496 -> 112,801
181,583 -> 243,521
522,293 -> 616,293
389,166 -> 418,166
792,940 -> 622,940
159,953 -> 769,343
857,231 -> 857,553
339,176 -> 339,463
35,412 -> 35,883
478,694 -> 478,325
741,257 -> 741,785
154,130 -> 810,786
869,81 -> 869,80
118,815 -> 918,815
941,954 -> 941,940
987,51 -> 56,982
243,571 -> 243,385
36,138 -> 36,24
28,971 -> 975,24
945,842 -> 759,842
474,470 -> 784,470
918,520 -> 512,114
836,606 -> 668,774
557,918 -> 422,918
925,889 -> 875,939
14,317 -> 467,770
638,312 -> 440,312
139,353 -> 212,353
690,339 -> 622,339
904,863 -> 307,266
302,939 -> 148,939
493,409 -> 93,809
185,871 -> 546,871
25,355 -> 889,355
355,832 -> 273,832
918,56 -> 38,936
950,189 -> 81,189
311,469 -> 289,469
567,269 -> 963,269
958,306 -> 958,852
847,753 -> 803,753
359,983 -> 332,956
885,128 -> 164,128
479,841 -> 350,841
850,280 -> 163,967
784,822 -> 555,822
607,29 -> 245,391
293,280 -> 293,685
273,716 -> 273,327
974,155 -> 974,415
419,34 -> 419,898
543,344 -> 543,126
137,163 -> 737,163
252,158 -> 899,805
581,36 -> 857,36
588,728 -> 588,511
320,303 -> 706,303
180,914 -> 939,155
547,676 -> 547,535
974,961 -> 97,84
179,757 -> 789,757
450,706 -> 450,452
595,598 -> 526,598
184,42 -> 184,43
221,963 -> 738,963
79,976 -> 79,109
638,793 -> 638,986
98,81 -> 534,517
700,334 -> 700,201
533,265 -> 76,265
131,839 -> 728,839
120,78 -> 815,773
455,825 -> 165,825
521,258 -> 978,258
425,931 -> 966,931
358,754 -> 506,902
126,228 -> 851,228
393,114 -> 29,114
58,615 -> 425,615
384,607 -> 581,804
140,939 -> 140,673
372,400 -> 795,400
115,165 -> 84,165
788,275 -> 544,275
126,329 -> 725,329
182,541 -> 99,541
410,709 -> 974,709
681,202 -> 687,208
495,832 -> 733,594
259,781 -> 444,596
49,442 -> 49,243
988,986 -> 18,16
224,624 -> 224,32
755,41 -> 185,611
512,168 -> 492,168
55,891 -> 762,184
162,845 -> 162,19
976,771 -> 449,244
398,789 -> 398,336
796,28 -> 796,326
589,735 -> 589,668
382,303 -> 10,675
650,968 -> 140,458
784,265 -> 245,804
961,980 -> 346,365
685,360 -> 567,360
60,847 -> 749,158
828,80 -> 46,862
96,760 -> 96,340
22,372 -> 878,372
26,973 -> 928,71
86,697 -> 86,790
684,896 -> 684,638
644,370 -> 644,177
915,854 -> 134,73
453,678 -> 453,394
716,594 -> 160,38
757,62 -> 518,301
977,928 -> 977,53
705,858 -> 328,858
352,527 -> 759,120
221,365 -> 677,821
237,815 -> 829,815
756,615 -> 756,129
681,948 -> 28,295
712,731 -> 120,731
141,292 -> 141,338
874,781 -> 874,414
197,892 -> 972,117
673,779 -> 113,219
469,240 -> 887,240
546,367 -> 270,367
115,356 -> 264,505
587,969 -> 942,969
697,332 -> 432,597
251,742 -> 251,402
655,258 -> 875,38
806,801 -> 438,801
776,525 -> 296,45
437,137 -> 437,507
310,870 -> 875,305
92,877 -> 949,20
96,695 -> 269,868
917,601 -> 917,347
598,172 -> 91,679
407,743 -> 818,743
74,932 -> 720,286
574,967 -> 747,794
989,982 -> 22,15
128,514 -> 590,976
46,86 -> 851,891
976,914 -> 87,25
948,659 -> 948,40
442,504 -> 442,398
310,824 -> 509,625
946,338 -> 433,851
555,241 -> 531,265
336,13 -> 336,48
688,880 -> 394,880
105,143 -> 823,861
761,330 -> 228,863
405,132 -> 853,580
780,886 -> 326,432
471,45 -> 471,600
810,971 -> 810,418
591,58 -> 591,821
494,133 -> 616,11
519,746 -> 519,230
696,879 -> 107,879
21,24 -> 937,940
18,822 -> 637,203
159,468 -> 570,468
635,850 -> 635,408
446,469 -> 337,469
962,690 -> 962,28
925,958 -> 40,73
123,183 -> 545,183
26,767 -> 26,548
898,101 -> 249,750
85,706 -> 85,446
224,763 -> 15,763
503,655 -> 847,655
659,905 -> 650,905
596,462 -> 851,207
421,230 -> 884,230
550,18 -> 524,18
437,375 -> 715,375
210,202 -> 210,348
263,298 -> 263,948
132,352 -> 749,969
582,454 -> 582,771
724,99 -> 724,388
16,528 -> 16,317
22,949 -> 22,987
457,328 -> 457,327
279,130 -> 279,234
56,338 -> 368,650
448,295 -> 533,295
898,90 -> 60,928
116,619 -> 208,527
614,481 -> 614,152
113,412 -> 543,412
854,305 -> 854,748
225,825 -> 115,935
976,365 -> 522,365
547,394 -> 547,82
800,695 -> 800,111
36,10 -> 973,947
557,441 -> 527,441
62,798 -> 708,798
582,585 -> 816,585
599,33 -> 70,562
971,863 -> 971,150
408,273 -> 535,273
797,215 -> 692,215
537,922 -> 519,922
299,302 -> 504,302
88,361 -> 88,249
430,311 -> 544,311
902,127 -> 902,791
346,986 -> 346,307
523,534 -> 285,296
600,628 -> 676,628
373,689 -> 945,117
265,189 -> 343,189
391,632 -> 391,728
500,516 -> 712,304
250,891 -> 41,891
591,539 -> 591,577
968,250 -> 265,250
80,32 -> 80,988
668,385 -> 323,385
407,103 -> 865,103
755,190 -> 693,190
754,113 -> 754,392
957,261 -> 967,251
881,734 -> 803,812
234,912 -> 234,326
711,893 -> 711,29
513,479 -> 782,479
111,224 -> 675,224
125,645 -> 125,502
591,983 -> 591,726
462,390 -> 85,390
854,275 -> 649,480
253,792 -> 23,792
940,975 -> 447,975
604,297 -> 604,386
172,217 -> 633,217
159,941 -> 159,569
468,72 -> 468,787
931,935 -> 36,40
114,166 -> 360,412
539,740 -> 349,930
314,342 -> 65,93
905,650 -> 817,650
786,546 -> 173,546
209,967 -> 209,142
679,64 -> 68,675
472,70 -> 840,70
750,753 -> 124,753
79,490 -> 652,490
655,114 -> 655,575
984,18 -> 984,287
458,41 -> 961,544
358,208 -> 358,679
977,830 -> 283,136
250,800 -> 187,800
64,516 -> 64,809
196,235 -> 781,235
727,988 -> 891,824
437,158 -> 437,549
565,231 -> 565,368
293,411 -> 29,147
100,905 -> 969,36
59,615 -> 920,615
844,883 -> 844,173
966,980 -> 25,39
802,923 -> 163,284
980,24 -> 730,24
357,474 -> 32,799
735,23 -> 735,566
970,426 -> 970,42
194,542 -> 194,328
372,69 -> 630,69
779,295 -> 239,835
793,381 -> 650,238
366,579 -> 366,762
502,354 -> 530,326
432,125 -> 432,795
162,220 -> 162,239
631,944 -> 63,944
261,526 -> 330,457
913,445 -> 913,958
786,160 -> 333,613
919,123 -> 88,954

@ -0,0 +1,109 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
)
type Point struct {
x int
y int
}
type Line struct {
start Point
end Point
}
func createLine(s []string) *Line {
l := &Line{start: Point{}, end: Point{}}
l.start.x, _ = strconv.Atoi(s[0])
l.start.y, _ = strconv.Atoi(s[1])
l.end.x, _ = strconv.Atoi(s[2])
l.end.y, _ = strconv.Atoi(s[3])
return l
}
func (l *Line) Walk() []Point {
var p []Point
xstart, xend := minMax(l.start.x, l.end.x)
ystart, yend := minMax(l.start.y, l.end.y)
// diagonal line
if xend-xstart == yend-ystart {
for x, y := l.start.x, l.start.y; ; {
p = append(p, Point{x: x, y: y})
if x == l.end.x {
break
}
if x > l.end.x {
x--
} else {
x++
}
if y > l.end.y {
y--
} else {
y++
}
}
fmt.Print()
} else {
// horizontal line
for x := xstart; x <= xend && l.start.y == l.end.y; x++ {
p = append(p, Point{x: x, y: l.start.y})
}
// vertical line
for y := ystart; y <= yend && l.start.x == l.end.x; y++ {
p = append(p, Point{x: l.start.x, y: y})
}
}
return p
}
func minMax(a int, b int) (min int, max int) {
if a > b {
min, max = b, a
} else {
min, max = a, b
}
return
}
func main() {
file, err := os.Open("input.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
r := regexp.MustCompile("[0-9]+")
floorMap := make(map[Point]int)
for scanner.Scan() {
text := scanner.Text()
coords := r.FindAllString(text, -1)
line := createLine(coords)
points := line.Walk()
for _, p := range points {
floorMap[p]++
}
}
var overlapCount int
for _, v := range floorMap {
if v > 1 {
overlapCount++
}
}
fmt.Println(overlapCount)
}

@ -1,6 +1,23 @@
#include "boilerplate.hh"
// newline separated split
// newline separated split, int
std::vector<int> readFileInt(std::string file){
std::vector<int> outputVec;
std::ifstream inputFile(file);
if (!inputFile.is_open()){
std::cerr << "File " << file << "not found";
exit(1);
}
std::string line;
while(getline(inputFile, line)){
outputVec.push_back(stoi(line));
}
return outputVec;
}
// newline separated split, string
std::vector<std::string> readFile(std::string file){
std::vector<std::string> outputVec;

@ -9,6 +9,7 @@
using InputArrays = std::vector<std::vector<std::string>>;
InputArrays readFile(std::string, char);
std::vector<int> readFileInt(std::string);
std::vector<std::string> readFile(std::string);
std::vector<std::string> ssplit(std::string, char);

Loading…
Cancel
Save