-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.kt
117 lines (113 loc) · 5.3 KB
/
04.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.File
import kotlin.system.measureTimeMillis
fun part01(input: String) : Int {
val lines = input.trim().lines()
var amount = 0
for ((lineIndex, line) in lines.withIndex()) {
for ((charIndex, char) in line.withIndex()) {
if (char == 'X') {
// horizontal forwards
if (charIndex + 3 < line.length) {
if(line[charIndex + 1] == 'M' &&
line[charIndex + 2] == 'A' &&
line[charIndex + 3] == 'S')
amount++
}
// horizontal backwards
if(charIndex - 3 >= 0) {
if(line[charIndex - 1] == 'M' &&
line[charIndex - 2] == 'A' &&
line[charIndex - 3] == 'S')
amount++
}
// vertical downwards
if(lineIndex + 3 < lines.size) {
if(lines[lineIndex + 1][charIndex] == 'M' &&
lines[lineIndex + 2][charIndex] == 'A' &&
lines[lineIndex + 3][charIndex] == 'S')
amount++
}
// vertical upwards
if(lineIndex - 3 >= 0) {
if(lines[lineIndex - 1][charIndex] == 'M' &&
lines[lineIndex - 2][charIndex] == 'A' &&
lines[lineIndex - 3][charIndex] == 'S')
amount++
}
// diagonal downwards-backwards (bottom-left)
if(lineIndex + 3 < lines.size && charIndex - 3 >= 0) {
if(lines[lineIndex + 1][charIndex - 1] == 'M' &&
lines[lineIndex + 2][charIndex - 2] == 'A' &&
lines[lineIndex + 3][charIndex - 3] == 'S')
amount++
}
// diagonal downwards-forwards (bottom-right)
if(lineIndex + 3 < lines.size && charIndex + 3 < line.length) {
if(lines[lineIndex + 1][charIndex + 1] == 'M' &&
lines[lineIndex + 2][charIndex + 2] == 'A' &&
lines[lineIndex + 3][charIndex + 3] == 'S')
amount++
}
// diagonal upwards-forwards (top-right)
if(lineIndex - 3 >= 0 && charIndex + 3 < line.length) {
if(lines[lineIndex - 1][charIndex + 1] == 'M' &&
lines[lineIndex - 2][charIndex + 2] == 'A' &&
lines[lineIndex - 3][charIndex + 3] == 'S')
amount++
}
// diagonal upwards-backwards (top-left)
if(lineIndex - 3 >= 0 && charIndex - 3 >= 0) {
if(lines[lineIndex - 1][charIndex - 1] == 'M' &&
lines[lineIndex - 2][charIndex - 2] == 'A' &&
lines[lineIndex - 3][charIndex - 3] == 'S')
amount++
}
}
}
}
return amount
}
fun part02(input: String) : Int {
val lines = input.trim().lines()
var amount = 0
for ((lineIndex, line) in lines.withIndex()) {
for ((charIndex, char) in line.withIndex()) {
if (char == 'A' &&
lineIndex - 1 >= 0 && lineIndex + 1 < lines.size &&
charIndex - 1 >= 0 && charIndex + 1 < line.length
) {
if(
((lines[lineIndex-1][charIndex-1] == 'M') && (lines[lineIndex+1][charIndex-1] == 'M') && (lines[lineIndex-1][charIndex+1] == 'S') && (lines[lineIndex+1][charIndex+1] == 'S')) ||
((lines[lineIndex-1][charIndex-1] == 'M') && (lines[lineIndex+1][charIndex-1] == 'S') && (lines[lineIndex-1][charIndex+1] == 'M') && (lines[lineIndex+1][charIndex+1] == 'S')) ||
((lines[lineIndex-1][charIndex-1] == 'S') && (lines[lineIndex+1][charIndex-1] == 'S') && (lines[lineIndex-1][charIndex+1] == 'M') && (lines[lineIndex+1][charIndex+1] == 'M')) ||
((lines[lineIndex-1][charIndex-1] == 'S') && (lines[lineIndex+1][charIndex-1] == 'M') && (lines[lineIndex-1][charIndex+1] == 'S') && (lines[lineIndex+1][charIndex+1] == 'M'))
) {
amount++
}
}
}
}
return amount
}
fun main() {
var input = ""
var part01 = 0
var part02 = 0
val elapsedLoadFile = measureTimeMillis {
input = File("./src/04.input.txt")
.readText(Charsets.UTF_8)
}
val elapsedPart01 = measureTimeMillis {
part01 = part01(input)
}
val elapsedPart02 = measureTimeMillis {
part02 = part02(input)
}
val title = "Advent Of Code 2024 - Day 04"
println(ANSI.GREEN + title + ANSI.RESET + "\n" +
(ANSI.RED + "-" + ANSI.RESET).repeat(title.length))
println("Loaded input in " + ANSI.YELLOW + elapsedLoadFile + ANSI.RESET + " ms\n" +
"First Part: " + ANSI.BLUE + part01 + ANSI.RESET + " (" + ANSI.YELLOW + elapsedPart01 + ANSI.RESET + " ms)\n" +
"Second Part: " + ANSI.BLUE + part02 + ANSI.RESET + " (" + ANSI.YELLOW + elapsedPart02 + ANSI.RESET + " ms)\n" +
"Finished in " + ANSI.YELLOW + (elapsedLoadFile + elapsedPart01 + elapsedPart02) + ANSI.RESET + " ms")
}