Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1d80fe5

Browse filesBrowse files
authored
Merge pull request #100 from tlarsen0/feature/day04_2-2025-01-25
Final configuration and code to Advent of Code 2024 Day 4, Part 2.
2 parents 9e83594 + c4744b7 commit 1d80fe5
Copy full SHA for 1d80fe5

File tree

8 files changed

+241
-0
lines changed
Filter options

8 files changed

+241
-0
lines changed

‎2024/day04_2/.idea/inspectionProfiles/Project_Default.xml

Copy file name to clipboardExpand all lines: 2024/day04_2/.idea/inspectionProfiles/Project_Default.xml
+6Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎2024/day04_2/.idea/kotlinc.xml

Copy file name to clipboardExpand all lines: 2024/day04_2/.idea/kotlinc.xml
+10Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎2024/day04_2/.idea/libraries/KotlinJavaRuntime.xml

Copy file name to clipboardExpand all lines: 2024/day04_2/.idea/libraries/KotlinJavaRuntime.xml
+17Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎2024/day04_2/.idea/misc.xml

Copy file name to clipboardExpand all lines: 2024/day04_2/.idea/misc.xml
+6Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎2024/day04_2/.idea/modules.xml

Copy file name to clipboardExpand all lines: 2024/day04_2/.idea/modules.xml
+8Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎2024/day04_2/.idea/vcs.xml

Copy file name to clipboardExpand all lines: 2024/day04_2/.idea/vcs.xml
+6Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎2024/day04_2/day04_2.iml

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/resources" type="java-resource" />
7+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/testResources" type="java-test-resource" />
10+
</content>
11+
<orderEntry type="inheritedJdk" />
12+
<orderEntry type="sourceFolder" forTests="false" />
13+
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
14+
</component>
15+
</module>

‎2024/day04_2/src/Main.kt

Copy file name to clipboard
+173Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import java.io.File
2+
import kotlin.math.max
3+
4+
5+
fun main(args: Array<String>) {
6+
println("AOC 2024, Day 4, Part 2 starting!!!!")
7+
8+
val theData = WordSearchData()
9+
var yPosition = 0
10+
File(args[0]).forEachLine {
11+
var xPosition = 0
12+
for(c in it.toCharArray()) {
13+
val newWordSearchCharacter = WordSearchCharacter(c, xPosition, yPosition)
14+
theData.characters.add(newWordSearchCharacter)
15+
xPosition++
16+
}
17+
theData.maxX = xPosition
18+
yPosition++
19+
}
20+
theData.maxY = yPosition
21+
22+
val answer = theData.searchForXmas()
23+
24+
println("Word search grid: ${theData.maxX} x ${theData.maxY}")
25+
println("Number of matches: $answer")
26+
27+
println("AOC 2024, Day 4, Part 2 completed!!!")
28+
}
29+
30+
class WordSearchData {
31+
val characters = ArrayList<WordSearchCharacter>()
32+
33+
var maxX: Int = 0
34+
set(value) {
35+
field = max(field, value)
36+
}
37+
var maxY: Int = 0
38+
set(value) {
39+
field = max(field, value)
40+
}
41+
42+
enum class Direction {
43+
UP_LEFT, UP, UP_RIGHT, LEFT, RIGHT, DOWN_LEFT, DOWN, DOWN_RIGHT
44+
}
45+
46+
fun searchForXmas(): Int {
47+
var matches = 0
48+
// All characters in word search space
49+
for (wordSearchCharacter in characters) {
50+
// Look for matches to on characters XMAS
51+
52+
if ((wordSearchCharacter.theChar == 'A') && searchForXMAS(wordSearchCharacter)) {
53+
println("Found X of MAS at ${wordSearchCharacter.xPosition}, ${wordSearchCharacter.yPosition}")
54+
matches++
55+
}
56+
}
57+
58+
return matches
59+
}
60+
61+
62+
private fun searchForXMAS(character: WordSearchCharacter): Boolean {
63+
64+
return checkPattern1(character) || checkPattern2(character) || checkPattern3(character) || checkPattern4(character)
65+
}
66+
67+
// Pattern 1
68+
// M . M
69+
// . A .
70+
// S . S
71+
private fun checkPattern1(character: WordSearchCharacter):Boolean {
72+
return searchForMAS(character, 'M', Direction.UP_LEFT, 1) &&
73+
searchForMAS(character, 'M', Direction.UP_RIGHT, 1) &&
74+
searchForMAS(character, 'S', Direction.DOWN_LEFT, 1) &&
75+
searchForMAS(character, 'S', Direction.DOWN_RIGHT, 1)
76+
}
77+
78+
// Pattern 2
79+
// M . S
80+
// . A .
81+
// M . S
82+
private fun checkPattern2(character: WordSearchCharacter):Boolean {
83+
return searchForMAS(character, 'M', Direction.UP_LEFT, 1) &&
84+
searchForMAS(character, 'S', Direction.UP_RIGHT, 1) &&
85+
searchForMAS(character, 'M', Direction.DOWN_LEFT, 1) &&
86+
searchForMAS(character, 'S', Direction.DOWN_RIGHT, 1)
87+
}
88+
89+
// Pattern 3
90+
// S . S
91+
// . A .
92+
// M . M
93+
private fun checkPattern3(character: WordSearchCharacter):Boolean {
94+
return searchForMAS(character, 'S', Direction.UP_LEFT, 1) &&
95+
searchForMAS(character, 'S', Direction.UP_RIGHT, 1) &&
96+
searchForMAS(character, 'M', Direction.DOWN_LEFT, 1) &&
97+
searchForMAS(character, 'M', Direction.DOWN_RIGHT, 1)
98+
}
99+
100+
// Pattern 4
101+
// S . M
102+
// . A .
103+
// S . M
104+
private fun checkPattern4(character: WordSearchCharacter):Boolean {
105+
return searchForMAS(character, 'S', Direction.UP_LEFT, 1) &&
106+
searchForMAS(character, 'M', Direction.UP_RIGHT, 1) &&
107+
searchForMAS(character, 'S', Direction.DOWN_LEFT, 1) &&
108+
searchForMAS(character, 'M', Direction.DOWN_RIGHT, 1)
109+
}
110+
111+
private fun searchForMAS(character: WordSearchCharacter, lookFor: Char, direction: Direction, step: Int): Boolean {
112+
113+
114+
var nextX = character.xPosition
115+
var nextY = character.yPosition
116+
when (direction) {
117+
// search up left : x - 1, y - 1
118+
Direction.UP_LEFT -> {
119+
nextX -= step
120+
nextY -= step
121+
}
122+
// search up : x, y - 1
123+
Direction.UP -> {
124+
nextY -= step
125+
}
126+
// search up right : x + 1, y - 1
127+
Direction.UP_RIGHT -> {
128+
nextX += step
129+
nextY -= step
130+
}
131+
// search left : x - 1, y
132+
Direction.LEFT -> {
133+
nextX -= step
134+
}
135+
// search right : x + 1, y
136+
Direction.RIGHT -> {
137+
nextX += step
138+
}
139+
// search down left : x - 1, y + 1
140+
Direction.DOWN_LEFT -> {
141+
nextX -= step
142+
nextY += step
143+
}
144+
// search down : x, y + 1
145+
Direction.DOWN -> {
146+
nextY += step
147+
}
148+
// search down right: x + 1, y + 1
149+
Direction.DOWN_RIGHT -> {
150+
nextX += step
151+
nextY += step
152+
}
153+
}
154+
if (positionCheck(nextX, nextY)) {
155+
val nextChar =
156+
characters.find { allCharacters -> allCharacters.xPosition == nextX && allCharacters.yPosition == nextY }
157+
return nextChar?.theChar == lookFor
158+
}
159+
160+
// If code arrives here, then false
161+
return false
162+
}
163+
164+
private fun positionCheck(x: Int, y: Int): Boolean {
165+
if ((x < 0) || (y < 0)) return false
166+
167+
if ((x > maxX) || (y > maxY)) return false
168+
169+
return true
170+
}
171+
}
172+
173+
data class WordSearchCharacter (val theChar:Char, val xPosition:Int, val yPosition:Int)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.