Added the first days

This commit is contained in:
Julien Dessaux 2021-12-07 14:26:36 +01:00
parent 7ebc2cd003
commit 6ed18289c8
26 changed files with 6168 additions and 0 deletions

4
2021/01/first.b98 Normal file
View file

@ -0,0 +1,4 @@
&:>#;&-:v@,a.$$;
: 0
^ -<w-\1+\
^<

39
2021/01/first.go Normal file
View file

@ -0,0 +1,39 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
scanner.Scan()
prev, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatalf("%+v", err)
}
result := 0
for scanner.Scan() {
i, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatalf("%+v", err)
}
if i > prev {
result++
}
prev = i
}
fmt.Println(result)
}

2000
2021/01/input Normal file

File diff suppressed because it is too large Load diff

4
2021/01/second.b98 Normal file
View file

@ -0,0 +1,4 @@
v > v
>&00p&10p&20p>#;&:00gw;@,a.; v
^ >\1+\ v
^p02p01g02p00g01<

57
2021/01/second.go Normal file
View file

@ -0,0 +1,57 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
scanner.Scan()
A, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatalf("%+v", err)
}
scanner.Scan()
B, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatalf("%+v", err)
}
A += B
scanner.Scan()
C, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatalf("%+v", err)
}
B += C
A += C
result := 0
for scanner.Scan() {
D, err := strconv.Atoi(scanner.Text())
if err != nil {
log.Fatalf("%+v", err)
}
C += D
B += D
if B > A {
result++
}
A = B
B = C
C = D
}
fmt.Println(result)
}

5
2021/02/first.b98 Normal file
View file

@ -0,0 +1,5 @@
v pos in 0,0, depth in 1,0
>00p10p>#;~:'f-#v_$&00g+00p v@,a.*g01g00;
>'d-#v_$&10g+10pv
>&10g\-10p v
^ ;,a.g01.g00; ~ < # we do not forget to strip the \n from the input

42
2021/02/first.go Normal file
View file

@ -0,0 +1,42 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
pos := 0
depth := 0
for scanner.Scan() {
elts := strings.Split(scanner.Text(), " ")
i, err := strconv.Atoi(elts[1])
if err != nil {
log.Fatalf("%+v", err)
}
switch elts[0] {
case "forward":
pos += i
case "down":
depth += i
case "up":
depth -= i
default:
panic(elts[0])
}
}
fmt.Println(pos * depth)
}

1000
2021/02/input Normal file

File diff suppressed because it is too large Load diff

5
2021/02/second.b98 Normal file
View file

@ -0,0 +1,5 @@
v pos in 0,0, depth in 1,0, aim in 2,0
>00p10p20p>#;~:'f-#v_$&:00g+00p v@,a.*g01g00;
>'d-#v_$&20g+20pv>20g*10g+10pv
>&20g\-20p v <
^ ;,a.g01.g00; ~ << # we do not forget to strip the \n from the input

44
2021/02/second.go Normal file
View file

@ -0,0 +1,44 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
aim := 0
pos := 0
depth := 0
for scanner.Scan() {
elts := strings.Split(scanner.Text(), " ")
i, err := strconv.Atoi(elts[1])
if err != nil {
log.Fatalf("%+v", err)
}
switch elts[0] {
case "forward":
pos += i
depth += i * aim
case "down":
aim += i
case "up":
aim -= i
default:
panic(elts[0])
}
}
fmt.Println(pos * depth)
}

42
2021/03/first.go Normal file
View file

@ -0,0 +1,42 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
bits := make([]int, 12)
for scanner.Scan() {
num := scanner.Text()
for n := 0; n < 12; n++ {
if num[n] == '1' {
bits[n]++
}
}
}
gamma := 0
epsilon := 0
for n := 0; n < 12; n++ {
gamma *= 2
epsilon *= 2
if bits[n] > 500 {
gamma++
} else {
epsilon++
}
}
fmt.Println(gamma * epsilon)
}

1000
2021/03/input Normal file

File diff suppressed because it is too large Load diff

96
2021/03/second.go Normal file
View file

@ -0,0 +1,96 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func findBitCount(data []string) []int {
bits := make([]int, 12)
for _, line := range data {
for n := 0; n < 12; n++ {
if line[n] == '1' {
bits[n]++
}
}
}
return bits
}
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
data := make([]string, 0)
for scanner.Scan() {
data = append(data, scanner.Text())
}
datacsr := make([]string, len(data))
copy(datacsr, data)
var ogr int
for n := 0; n < 12; n++ {
if len(data) == 1 {
break
}
bits := findBitCount(data)
var i byte
if 2*bits[n] >= len(data) {
i = '1'
} else {
i = '0'
}
newdata := make([]string, 0)
for j := 0; j < len(data); j++ {
if data[j][n] == i {
newdata = append(newdata, data[j])
}
}
data = newdata
}
for n := 0; n < 12; n++ {
ogr *= 2
if data[0][n] == '1' {
ogr++
}
}
data = datacsr
var csr int
for n := 0; n < 12; n++ {
if len(data) == 1 {
break
}
bits := findBitCount(data)
var i byte
if 2*bits[n] < len(data) {
i = '1'
} else {
i = '0'
}
newdata := make([]string, 0)
for j := 0; j < len(data); j++ {
if data[j][n] == i {
newdata = append(newdata, data[j])
}
}
data = newdata
}
for n := 0; n < 12; n++ {
csr *= 2
if data[0][n] == '1' {
csr++
}
}
fmt.Println(ogr * csr)
}

123
2021/04/first.go Normal file
View file

@ -0,0 +1,123 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type grille struct {
lines [][]int
columns [][]int
}
var (
best = 100000000
score = 0
)
// winning func
func win(g *grille, n int, iter int) {
sum := 0
for i := 0; i < 5; i++ {
for j := 0; j < len(g.lines[i]); j++ {
sum += g.lines[i][j]
}
}
if iter < best {
best = iter
score = sum * n
//fmt.Println(iter, n, score, g)
}
}
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
scanner.Scan()
tirage := make([]int, 0)
tirageStr := strings.Split(scanner.Text(), ",")
// lets process the tirage
for _, v := range tirageStr {
n, err := strconv.Atoi(v)
if err != nil {
log.Fatalf("%+v", err)
}
tirage = append(tirage, n)
}
for scanner.Scan() {
// we just scanned the new line
// lets init the grille
g := new(grille)
g.lines = make([][]int, 5)
g.columns = make([][]int, 5)
for i := 0; i < 5; i++ {
g.lines[i] = make([]int, 5)
g.columns[i] = make([]int, 5)
}
// lets populate the grille
for i := 0; i < 5; i++ {
scanner.Scan()
numbers := strings.Fields(scanner.Text())
for j := 0; j < 5; j++ {
n, err := strconv.Atoi(numbers[j])
if err != nil {
log.Fatalf("%+v", err)
}
g.lines[i][j] = n
g.columns[j][i] = n
}
}
// lets process the tirage
out:
for iter, n := range tirage {
if iter >= best {
//fmt.Println(iter, n, g)
break out
}
// remove the number
for i := 0; i < 5; i++ {
for j := 0; j < len(g.lines[i]); j++ {
if g.lines[i][j] == n {
if len(g.lines[i]) == 1 {
g.lines[i] = []int{}
win(g, n, iter)
break out
}
if j < len(g.lines[i])-1 {
g.lines[i][j] = g.lines[i][len(g.lines[i])-1]
}
g.lines[i] = g.lines[i][:len(g.lines[i])-1]
}
}
}
for i := 0; i < 5; i++ {
for j := 0; j < len(g.columns[i]); j++ {
if g.columns[i][j] == n {
if len(g.columns[i]) == 1 {
g.columns[i] = []int{}
win(g, n, iter)
break out
}
if j < len(g.columns[i])-1 {
g.columns[i][j] = g.columns[i][len(g.columns[i])-1]
}
g.columns[i] = g.columns[i][:len(g.columns[i])-1]
}
}
}
}
}
fmt.Println(score)
}

601
2021/04/input Normal file
View file

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

119
2021/04/second.go Normal file
View file

@ -0,0 +1,119 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
type grille struct {
lines [][]int
columns [][]int
}
var (
best = -1
score = 0
)
// winning func
func win(g *grille, n int, iter int) {
sum := 0
for i := 0; i < 5; i++ {
for j := 0; j < len(g.lines[i]); j++ {
sum += g.lines[i][j]
}
}
if iter > best {
best = iter
score = sum * n
//fmt.Println(iter, n, score, g)
}
}
func main() {
f, err := os.Open("input")
if err != nil {
log.Fatalf("%+v", err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
scanner.Scan()
tirage := make([]int, 0)
tirageStr := strings.Split(scanner.Text(), ",")
// lets process the tirage
for _, v := range tirageStr {
n, err := strconv.Atoi(v)
if err != nil {
log.Fatalf("%+v", err)
}
tirage = append(tirage, n)
}
for scanner.Scan() {
// we just scanned the new line
// lets init the grille
g := new(grille)
g.lines = make([][]int, 5)
g.columns = make([][]int, 5)
for i := 0; i < 5; i++ {
g.lines[i] = make([]int, 5)
g.columns[i] = make([]int, 5)
}
// lets populate the grille
for i := 0; i < 5; i++ {
scanner.Scan()
numbers := strings.Fields(scanner.Text())
for j := 0; j < 5; j++ {
n, err := strconv.Atoi(numbers[j])
if err != nil {
log.Fatalf("%+v", err)
}
g.lines[i][j] = n
g.columns[j][i] = n
}
}
// lets process the tirage
out:
for iter, n := range tirage {
// remove the number
for i := 0; i < 5; i++ {
for j := 0; j < len(g.lines[i]); j++ {
if g.lines[i][j] == n {
if len(g.lines[i]) == 1 {
g.lines[i] = []int{}
win(g, n, iter)
break out
}
if j < len(g.lines[i])-1 {
g.lines[i][j] = g.lines[i][len(g.lines[i])-1]
}
g.lines[i] = g.lines[i][:len(g.lines[i])-1]
}
}
}
for i := 0; i < 5; i++ {
for j := 0; j < len(g.columns[i]); j++ {
if g.columns[i][j] == n {
if len(g.columns[i]) == 1 {
g.columns[i] = []int{}
win(g, n, iter)
break out
}
if j < len(g.columns[i])-1 {
g.columns[i][j] = g.columns[i][len(g.columns[i])-1]
}
g.columns[i] = g.columns[i][:len(g.columns[i])-1]
}
}
}
}
}
fmt.Println(score)
}

3
2021/05/README.md Normal file
View file

@ -0,0 +1,3 @@
I went a little overboard with a parsing tutorial.
I wanted to manually write a parser from quite some time now and seized this opportunity. It would have been trivial with just a scanner or even a regex, but hey learning!

10
2021/05/example 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

48
2021/05/first.go Normal file
View file

@ -0,0 +1,48 @@
package main
import (
"bufio"
"fmt"
"os"
"git.adyxax.org/aoc/2021/05/line"
)
func main() {
matrix := make([][]int, 1000)
for i := 0; i < 1000; i++ {
matrix[i] = make([]int, 1000)
}
parser := line.NewParser(bufio.NewReader(os.Stdin))
for {
l, err := parser.Parse()
if err != nil {
break
}
if l.X1 == l.X2 {
if l.Y1 > l.Y2 {
l.Y1, l.Y2 = l.Y2, l.Y1
}
for i := l.Y1; i <= l.Y2; i++ {
matrix[l.X1][i]++
}
} else if l.Y1 == l.Y2 {
if l.X1 > l.X2 {
l.X1, l.X2 = l.X2, l.X1
}
for i := l.X1; i <= l.X2; i++ {
matrix[i][l.Y1]++
}
}
}
score := 0
for i := 0; i < 1000; i++ {
for j := 0; j < 1000; j++ {
if matrix[i][j] >= 2 {
score++
}
}
}
fmt.Println(score)
}

60
2021/05/first_v2.go Normal file
View file

@ -0,0 +1,60 @@
package main
import (
"bufio"
"fmt"
"os"
"git.adyxax.org/aoc/2021/05/line"
)
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func computeDelta(a, b int) int {
if a < b {
return 1
} else if a > b {
return -1
}
return 0
}
func main() {
matrix := make([][]int, 1000)
for i := 0; i < 1000; i++ {
matrix[i] = make([]int, 1000)
}
parser := line.NewParser(bufio.NewReader(os.Stdin))
for {
l, err := parser.Parse()
if err != nil {
break
}
length := abs(l.X2 - l.X1)
if length == 0 {
length = abs(l.Y2 - l.Y1)
}
dx := computeDelta(l.X1, l.X2)
dy := computeDelta(l.Y1, l.Y2)
if dx == 0 || dy == 0 {
for i := 0; i <= length; i++ {
matrix[l.X1+i*dx][l.Y1+i*dy]++
}
}
}
score := 0
for i := 0; i < 1000; i++ {
for j := 0; j < 1000; j++ {
if matrix[i][j] >= 2 {
score++
}
}
}
fmt.Println(score)
}

3
2021/05/go.mod Normal file
View file

@ -0,0 +1,3 @@
module git.adyxax.org/aoc/2021/05
go 1.17

500
2021/05/input Normal file
View file

@ -0,0 +1,500 @@
223,805 -> 223,548
609,164 -> 609,503
461,552 -> 796,552
207,361 -> 207,34
503,879 -> 503,946
937,52 -> 937,268
560,652 -> 118,652
771,103 -> 85,789
119,156 -> 947,984
356,634 -> 607,634
348,812 -> 873,287
409,490 -> 726,490
298,790 -> 298,454
407,543 -> 820,130
206,89 -> 591,89
164,709 -> 976,709
208,921 -> 208,131
515,209 -> 515,745
876,639 -> 281,44
270,453 -> 727,910
190,417 -> 190,755
522,726 -> 903,726
390,651 -> 603,864
707,549 -> 926,330
471,869 -> 471,561
970,735 -> 401,735
612,624 -> 612,88
844,879 -> 844,453
400,38 -> 400,350
832,225 -> 984,225
971,642 -> 42,642
70,862 -> 447,485
183,79 -> 183,708
598,700 -> 598,287
635,195 -> 39,195
587,362 -> 349,362
108,88 -> 965,945
700,299 -> 165,299
295,824 -> 785,334
211,284 -> 390,105
288,326 -> 672,710
595,231 -> 595,679
671,576 -> 813,718
14,845 -> 784,75
700,129 -> 43,129
83,913 -> 889,107
830,596 -> 322,596
391,450 -> 391,779
384,32 -> 384,430
311,948 -> 938,321
460,288 -> 460,392
924,602 -> 924,595
703,458 -> 703,475
335,953 -> 335,195
692,314 -> 927,314
131,433 -> 131,737
590,771 -> 965,771
650,13 -> 963,13
586,904 -> 658,976
238,824 -> 782,824
366,45 -> 691,370
428,758 -> 201,758
240,545 -> 30,545
396,154 -> 332,154
549,307 -> 233,307
187,240 -> 851,904
151,135 -> 937,921
342,850 -> 342,156
695,200 -> 695,754
385,880 -> 893,372
986,966 -> 813,966
727,661 -> 727,402
316,937 -> 316,797
422,235 -> 422,282
965,684 -> 882,684
266,222 -> 419,69
649,843 -> 635,857
618,84 -> 126,576
588,822 -> 588,636
569,142 -> 569,607
899,479 -> 488,890
986,52 -> 369,52
987,478 -> 551,914
867,951 -> 973,845
90,401 -> 304,401
60,836 -> 798,836
143,675 -> 686,675
743,974 -> 743,305
981,899 -> 551,469
705,430 -> 493,430
301,366 -> 823,366
978,712 -> 617,712
426,805 -> 426,345
532,855 -> 532,54
612,143 -> 612,133
57,52 -> 955,950
880,50 -> 16,914
89,908 -> 89,214
487,867 -> 586,867
181,285 -> 181,470
526,666 -> 86,226
117,704 -> 117,961
289,101 -> 983,795
586,429 -> 442,429
442,869 -> 734,869
564,479 -> 564,382
447,486 -> 62,101
42,218 -> 509,218
21,890 -> 843,68
84,978 -> 921,141
590,960 -> 590,934
54,949 -> 967,36
799,39 -> 767,39
979,232 -> 979,628
489,482 -> 339,482
759,473 -> 290,942
960,958 -> 32,30
134,180 -> 134,864
972,981 -> 13,22
106,385 -> 11,385
849,454 -> 447,454
477,385 -> 955,863
853,180 -> 922,180
509,274 -> 751,32
905,295 -> 779,295
661,629 -> 104,629
935,117 -> 93,959
165,372 -> 746,953
988,141 -> 122,141
625,621 -> 625,406
24,710 -> 465,710
417,468 -> 851,34
365,285 -> 572,285
217,164 -> 217,214
943,439 -> 465,439
80,102 -> 80,717
869,19 -> 54,834
399,480 -> 399,458
644,826 -> 644,911
651,189 -> 651,687
671,946 -> 332,607
531,417 -> 657,417
847,350 -> 847,112
315,733 -> 871,177
749,118 -> 692,118
55,616 -> 55,894
570,307 -> 633,307
12,964 -> 883,93
84,299 -> 84,185
49,187 -> 903,187
592,40 -> 842,40
639,381 -> 802,544
59,61 -> 836,61
968,51 -> 266,753
883,373 -> 883,130
612,45 -> 406,45
206,698 -> 206,823
385,685 -> 385,46
656,338 -> 73,921
256,794 -> 365,903
671,247 -> 248,247
722,509 -> 635,422
460,783 -> 615,783
946,980 -> 946,129
343,780 -> 343,723
218,371 -> 218,856
363,809 -> 143,589
434,739 -> 889,739
75,71 -> 975,971
57,253 -> 582,778
976,237 -> 976,148
386,866 -> 386,544
901,797 -> 901,630
976,706 -> 195,706
264,420 -> 272,428
693,72 -> 693,379
888,832 -> 888,490
363,900 -> 363,350
25,312 -> 25,58
292,307 -> 481,307
715,393 -> 976,132
641,450 -> 96,450
650,38 -> 432,38
339,97 -> 476,97
916,24 -> 13,927
933,934 -> 34,35
971,367 -> 971,919
726,310 -> 477,559
12,984 -> 986,10
318,531 -> 318,72
604,979 -> 12,387
890,39 -> 890,213
944,954 -> 33,43
507,830 -> 284,607
724,111 -> 724,242
425,912 -> 425,445
371,903 -> 371,634
415,314 -> 415,509
884,849 -> 884,454
726,647 -> 447,926
588,463 -> 588,426
807,453 -> 807,593
32,449 -> 975,449
593,757 -> 593,607
521,850 -> 521,139
843,478 -> 843,317
408,834 -> 408,455
65,241 -> 864,241
532,138 -> 613,138
477,239 -> 477,676
92,400 -> 92,935
268,104 -> 300,104
942,20 -> 93,869
294,134 -> 695,134
748,477 -> 748,311
581,879 -> 481,879
292,57 -> 874,639
829,787 -> 944,787
130,780 -> 442,780
754,435 -> 956,435
306,659 -> 306,491
252,612 -> 646,612
846,949 -> 846,924
197,888 -> 145,836
156,790 -> 151,790
903,305 -> 671,73
195,79 -> 195,40
781,67 -> 781,635
742,743 -> 742,280
297,42 -> 618,42
237,151 -> 156,151
851,930 -> 47,126
425,368 -> 659,134
57,890 -> 898,49
86,62 -> 86,445
133,499 -> 133,604
202,567 -> 872,567
749,578 -> 749,804
379,379 -> 147,379
510,474 -> 510,388
184,115 -> 738,115
904,613 -> 550,613
755,649 -> 755,305
461,306 -> 461,547
469,124 -> 542,124
736,218 -> 736,968
307,662 -> 307,188
360,970 -> 58,668
36,267 -> 214,267
980,330 -> 22,330
222,972 -> 222,178
846,774 -> 714,774
798,837 -> 789,837
567,258 -> 567,502
325,582 -> 325,976
138,386 -> 138,691
326,878 -> 326,386
790,276 -> 811,276
517,522 -> 81,86
493,567 -> 406,567
522,370 -> 13,370
31,697 -> 607,121
297,524 -> 297,320
790,681 -> 753,681
90,961 -> 901,150
262,46 -> 262,68
18,26 -> 977,985
782,381 -> 956,381
353,740 -> 353,595
32,448 -> 941,448
405,254 -> 686,254
853,57 -> 297,613
555,209 -> 439,209
765,679 -> 142,56
175,903 -> 175,685
693,653 -> 845,653
394,108 -> 394,901
351,108 -> 335,108
841,83 -> 841,716
525,608 -> 525,496
874,32 -> 874,214
354,760 -> 44,760
249,330 -> 864,945
553,377 -> 553,944
903,374 -> 335,374
387,34 -> 387,86
380,331 -> 380,124
618,520 -> 797,520
718,169 -> 703,169
355,184 -> 851,184
582,570 -> 582,313
312,952 -> 312,460
269,70 -> 269,197
701,907 -> 701,768
645,417 -> 645,548
931,532 -> 367,532
497,361 -> 497,348
563,642 -> 976,642
376,504 -> 376,448
538,945 -> 538,773
594,886 -> 594,281
879,558 -> 192,558
985,68 -> 66,987
599,420 -> 599,41
296,318 -> 296,132
330,619 -> 302,619
245,137 -> 918,810
823,798 -> 556,531
64,201 -> 723,860
955,365 -> 955,829
372,976 -> 255,859
804,962 -> 168,962
200,442 -> 200,97
965,964 -> 870,869
534,158 -> 128,564
380,739 -> 577,542
740,391 -> 740,651
167,177 -> 619,177
215,449 -> 215,330
494,612 -> 19,137
458,634 -> 458,257
884,817 -> 393,326
407,291 -> 19,679
627,173 -> 627,570
53,93 -> 552,592
809,363 -> 119,363
588,418 -> 588,764
807,131 -> 807,834
616,61 -> 514,61
553,642 -> 236,325
959,553 -> 683,553
36,754 -> 36,830
533,293 -> 144,293
950,780 -> 396,780
949,878 -> 14,878
453,180 -> 989,180
22,46 -> 670,694
479,206 -> 479,552
22,53 -> 599,53
254,964 -> 884,334
578,813 -> 100,813
945,247 -> 778,80
312,978 -> 312,518
882,225 -> 882,967
581,683 -> 293,395
107,540 -> 534,967
382,946 -> 28,946
864,743 -> 246,743
538,558 -> 733,753
811,436 -> 814,436
982,33 -> 65,950
785,829 -> 945,829
322,471 -> 346,471
904,528 -> 904,669
231,471 -> 772,471
773,490 -> 669,386
867,482 -> 417,32
352,856 -> 352,478
723,355 -> 619,355
667,922 -> 667,247
642,386 -> 241,386
594,35 -> 594,580
916,723 -> 793,723
73,774 -> 269,970
43,273 -> 148,168
744,637 -> 825,637
98,30 -> 98,383
130,277 -> 802,277
167,122 -> 672,627
871,866 -> 564,559
923,475 -> 539,859
422,714 -> 422,946
667,950 -> 667,640
758,181 -> 12,927
129,927 -> 129,288
485,661 -> 402,661
114,573 -> 974,573
674,779 -> 851,779
977,184 -> 977,143
229,937 -> 229,138
520,887 -> 520,512
918,329 -> 918,990
732,41 -> 521,41
399,245 -> 883,729
824,618 -> 356,618
215,218 -> 845,848
704,34 -> 307,431
124,166 -> 696,738
692,749 -> 839,749
790,637 -> 790,598
697,396 -> 669,396
419,140 -> 113,446
426,738 -> 171,738
489,494 -> 190,793
320,301 -> 320,398
275,809 -> 275,717
537,703 -> 465,703
536,450 -> 560,450
153,927 -> 914,166
246,692 -> 485,453
26,179 -> 26,554
487,678 -> 487,696
807,719 -> 224,719
605,920 -> 899,920
112,262 -> 112,765
752,898 -> 752,429
861,103 -> 861,477
628,505 -> 628,248
556,293 -> 556,276
826,682 -> 273,129
685,324 -> 685,692
544,410 -> 544,678
796,633 -> 796,950
753,843 -> 753,936
817,40 -> 817,600
137,941 -> 677,401
563,457 -> 599,457
251,644 -> 251,67
170,792 -> 805,792
171,486 -> 171,877
337,481 -> 268,412
43,158 -> 44,158
148,610 -> 863,610
332,765 -> 202,765
307,637 -> 334,637
557,380 -> 231,54
858,76 -> 150,784
477,329 -> 319,329
306,608 -> 306,38
245,42 -> 245,929
15,786 -> 745,786
946,321 -> 841,321
837,281 -> 837,762
847,391 -> 847,840
304,52 -> 304,299
938,122 -> 877,122
214,347 -> 862,347
494,540 -> 751,540
184,29 -> 913,758
904,12 -> 15,901
573,23 -> 158,23
926,603 -> 643,603
105,506 -> 518,506
551,917 -> 983,917
708,33 -> 831,33
347,173 -> 218,44
933,175 -> 933,781
902,556 -> 902,812
556,485 -> 252,789
823,807 -> 368,352
217,744 -> 217,470
795,455 -> 795,783
170,944 -> 926,188
55,655 -> 258,655
158,57 -> 959,858
714,823 -> 714,550
238,18 -> 388,18
980,985 -> 12,17
360,596 -> 770,596
846,684 -> 220,58
552,107 -> 552,974
228,552 -> 354,552
421,41 -> 421,103
674,475 -> 912,475
455,626 -> 455,683
952,841 -> 946,841
920,792 -> 381,253
786,918 -> 786,175
889,859 -> 889,24
178,604 -> 573,209
71,621 -> 550,621
38,36 -> 922,920
104,690 -> 575,690
252,883 -> 894,241
627,107 -> 417,107
768,913 -> 13,158
708,337 -> 708,407
156,941 -> 156,297
814,653 -> 814,829
234,920 -> 896,920
652,170 -> 128,170
765,825 -> 347,825
681,901 -> 681,112
410,301 -> 979,301
462,681 -> 462,726
117,957 -> 117,693
479,948 -> 698,948
839,965 -> 97,223
102,189 -> 102,366
93,798 -> 819,72
27,336 -> 27,655
161,635 -> 527,269
140,272 -> 140,336
884,915 -> 41,72
575,563 -> 155,563
387,601 -> 387,597
355,186 -> 782,613
866,435 -> 816,435
96,161 -> 764,161
971,29 -> 21,979

239
2021/05/line/line.go Normal file
View file

@ -0,0 +1,239 @@
package line
import (
"bufio"
"bytes"
"fmt"
"io"
"strconv"
)
type Line struct {
X1, Y1, X2, Y2 int
}
// Token represents a lexical token.
type Token int
const (
// Special tokens
ILLEGAL Token = iota
EOF
WHITESPACE
// Literals
INT // integers
// Misc characters
COMMA // ,
ARROW // ->
)
func isWhitespace(ch rune) bool {
return ch == ' ' || ch == '\t' || ch == '\n'
}
func isDigit(ch rune) bool {
return (ch >= '0' && ch <= '9')
}
var eof = rune(0)
// Scanner represents a lexical scanner.
type Scanner struct {
r *bufio.Reader
}
// NewScanner returns a new instance of Scanner.
func NewScanner(r io.Reader) *Scanner {
return &Scanner{r: bufio.NewReader(r)}
}
// read reads the next rune from the bufferred reader.
// Returns the rune(0) if an error occurs (or io.EOF is returned).
func (s *Scanner) read() rune {
ch, _, err := s.r.ReadRune()
if err != nil {
return eof
}
return ch
}
// unread places the previously read rune back on the reader.
func (s *Scanner) unread() { _ = s.r.UnreadRune() }
// Scan returns the next token and literal value.
func (s *Scanner) Scan() (tok Token, lit string) {
// Read the next rune.
ch := s.read()
// If we see whitespace then consume all contiguous whitespace.
// If we see a digit then consume as an int.
if isWhitespace(ch) {
return s.scanWhitespace(ch)
} else if isDigit(ch) {
return s.scanInt(ch)
}
// Otherwise read the individual character.
switch ch {
case eof:
return EOF, ""
case ',':
return COMMA, string(ch)
case '-':
return s.scanArrow(ch)
}
return ILLEGAL, string(ch)
}
// scanWhitespace consumes the current rune and all contiguous whitespace.
func (s *Scanner) scanWhitespace(read rune) (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(read)
// Read every subsequent whitespace character into the buffer.
// Non-whitespace characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isWhitespace(ch) {
s.unread()
break
} else {
buf.WriteRune(ch)
}
}
return WHITESPACE, buf.String()
}
// scanInt consumes the current rune and all contiguous digit runes.
func (s *Scanner) scanInt(read rune) (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(read)
// Read every subsequent ident character into the buffer.
// Non-ident characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if !isDigit(ch) {
s.unread()
break
} else {
_, _ = buf.WriteRune(ch)
}
}
// Otherwise return as a regular identifier.
return INT, buf.String()
}
func (s *Scanner) scanArrow(read rune) (tok Token, lit string) {
// Create a buffer and read the current character into it.
var buf bytes.Buffer
buf.WriteRune(read)
// Read every subsequent ident character into the buffer.
// Non-ident characters and EOF will cause the loop to exit.
for {
if ch := s.read(); ch == eof {
break
} else if ch == '>' {
_, _ = buf.WriteRune(ch)
return ARROW, buf.String()
} else {
_, _ = buf.WriteRune(ch)
break
}
}
// Otherwise return as a regular identifier.
return ILLEGAL, buf.String()
}
// Parser represents a parser.
type Parser struct {
s *Scanner
buf struct {
tok Token // last read token
lit string // last read literal
n int // buffer size (max=1)
}
}
// NewParser returns a new instance of Parser.
func NewParser(r io.Reader) *Parser {
return &Parser{s: NewScanner(r)}
}
// scan returns the next token from the underlying scanner.
// If a token has been unscanned then read that instead.
func (p *Parser) scan() (tok Token, lit string) {
// If we have a token on the buffer, then return it.
if p.buf.n != 0 {
p.buf.n = 0
return p.buf.tok, p.buf.lit
}
// Otherwise read the next token from the scanner.
tok, lit = p.s.Scan()
// Save it to the buffer in case we unscan later.
p.buf.tok, p.buf.lit = tok, lit
return
}
// unscan pushes the previously read token back onto the buffer.
func (p *Parser) unscan() { p.buf.n = 1 }
// scanIgnoreWhitespace scans the next non-whitespace token.
func (p *Parser) scanIgnoreWhitespace() (tok Token, lit string) {
tok, lit = p.scan()
if tok == WHITESPACE {
tok, lit = p.scan()
}
return
}
func (p *Parser) Parse() (*Line, error) {
l := &Line{}
if tok, lit := p.scanIgnoreWhitespace(); tok != INT {
return nil, fmt.Errorf("found %q, expected INT", lit)
} else {
l.X1, _ = strconv.Atoi(lit)
}
if tok, lit := p.scanIgnoreWhitespace(); tok != COMMA {
return nil, fmt.Errorf("found %q, expected COMMA", lit)
}
if tok, lit := p.scanIgnoreWhitespace(); tok != INT {
return nil, fmt.Errorf("found %q, expected INT", lit)
} else {
l.Y1, _ = strconv.Atoi(lit)
}
if tok, lit := p.scanIgnoreWhitespace(); tok != ARROW {
return nil, fmt.Errorf("found %q, expected ARROW", lit)
}
if tok, lit := p.scanIgnoreWhitespace(); tok != INT {
return nil, fmt.Errorf("found %q, expected INT", lit)
} else {
l.X2, _ = strconv.Atoi(lit)
}
if tok, lit := p.scanIgnoreWhitespace(); tok != COMMA {
return nil, fmt.Errorf("found %q, expected COMMA", lit)
}
if tok, lit := p.scanIgnoreWhitespace(); tok != INT {
return nil, fmt.Errorf("found %q, expected INT", lit)
} else {
l.Y2, _ = strconv.Atoi(lit)
}
if tok, lit := p.scan(); tok != WHITESPACE {
return nil, fmt.Errorf("found %q, expected WHITESPACE", lit)
}
return l, nil
}

61
2021/05/second.go Normal file
View file

@ -0,0 +1,61 @@
package main
import (
"bufio"
"fmt"
"os"
"git.adyxax.org/aoc/2021/05/line"
)
func main() {
matrix := make([][]int, 1000)
for i := 0; i < 1000; i++ {
matrix[i] = make([]int, 1000)
}
parser := line.NewParser(bufio.NewReader(os.Stdin))
for {
l, err := parser.Parse()
if err != nil {
break
}
if l.X1 == l.X2 {
if l.Y1 > l.Y2 {
l.Y1, l.Y2 = l.Y2, l.Y1
}
for i := l.Y1; i <= l.Y2; i++ {
matrix[l.X1][i]++
}
} else if l.Y1 == l.Y2 {
if l.X1 > l.X2 {
l.X1, l.X2 = l.X2, l.X1
}
for i := l.X1; i <= l.X2; i++ {
matrix[i][l.Y1]++
}
} else {
if l.X1 > l.X2 {
l.X1, l.X2, l.Y1, l.Y2 = l.X2, l.X1, l.Y2, l.Y1
}
if l.Y1 < l.Y2 {
for i := 0; i <= l.X2-l.X1; i++ {
matrix[l.X1+i][l.Y1+i]++
}
} else {
for i := 0; i <= l.X2-l.X1; i++ {
matrix[l.X1+i][l.Y1-i]++
}
}
}
}
score := 0
for i := 0; i < 1000; i++ {
for j := 0; j < 1000; j++ {
if matrix[i][j] >= 2 {
score++
}
}
}
fmt.Println(score)
}

58
2021/05/second_v2.go Normal file
View file

@ -0,0 +1,58 @@
package main
import (
"bufio"
"fmt"
"os"
"git.adyxax.org/aoc/2021/05/line"
)
func abs(a int) int {
if a < 0 {
return -a
}
return a
}
func computeDelta(a, b int) int {
if a < b {
return 1
} else if a > b {
return -1
}
return 0
}
func main() {
matrix := make([][]int, 1000)
for i := 0; i < 1000; i++ {
matrix[i] = make([]int, 1000)
}
parser := line.NewParser(bufio.NewReader(os.Stdin))
for {
l, err := parser.Parse()
if err != nil {
break
}
length := abs(l.X2 - l.X1)
if length == 0 {
length = abs(l.Y2 - l.Y1)
}
dx := computeDelta(l.X1, l.X2)
dy := computeDelta(l.Y1, l.Y2)
for i := 0; i <= length; i++ {
matrix[l.X1+i*dx][l.Y1+i*dy]++
}
}
score := 0
for i := 0; i < 1000; i++ {
for j := 0; j < 1000; j++ {
if matrix[i][j] >= 2 {
score++
}
}
}
fmt.Println(score)
}

5
README.md Normal file
View file

@ -0,0 +1,5 @@
This repository contains my solutions to the advent of code puzzles available at https://adventofcode.com/
I do not strive for elegance or anything, I just solve a puzzle (often brute forcing it) and if it executes fast enough I move on to the next one.
If the puzzle suits my fancy I implement a [Funge-98](https://github.com/catseye/Funge-98/blob/master/doc/funge98.markdown) solution for fun that I run using my own interpreter [gofunge98]*(https://git.adyxax.org/adyxax/gofunge98).