Skip to content

Commit 26debe6

Browse files
committed
[AoC Day 13] Solution 1
I'm a bit behind and this one looked fun so skipped to it.
1 parent fdbcf5a commit 26debe6

File tree

2 files changed

+601
-0
lines changed

2 files changed

+601
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.rox.adventofcode.y2022
2+
3+
import java.rmi.UnexpectedException
4+
import java.util.*
5+
6+
private val inputSample = """
7+
[1,1,3,1,1]
8+
[1,1,5,1,1]
9+
10+
[[1],[2,3,4]]
11+
[[1],4]
12+
13+
[9]
14+
[[8,7,6]]
15+
16+
[[4,4],4,4]
17+
[[4,4],4,4,4]
18+
19+
[7,7,7,7]
20+
[7,7,7]
21+
22+
[]
23+
[3]
24+
25+
[[[]]]
26+
[[]]
27+
28+
[1,[2,[3,[4,[5,6,7]]]],8,9]
29+
[1,[2,[3,[4,[5,6,0]]]],8,9]
30+
""".trimIndent()
31+
32+
fun main() {
33+
println("Sample Input A: ${solutionA(inputSample)}")
34+
//println("Sample Input B: ${solutionB(inputSample)}")
35+
//println("Part A: ${solutionA(puzzleInputFromFile("src/main/kotlin/com/rox/adventofcode/y202X/DayX.input"))}")
36+
//println("Part B: ${solutionB(puzzleInputFromFile("src/main/kotlin/com/rox/adventofcode/y202X/DayX.input"))}")
37+
}
38+
39+
/**
40+
*
41+
*
42+
* Answer: ???
43+
*/
44+
private fun solutionA(input: String): Any {
45+
val packetPairs = input.split("\n\n")
46+
47+
for (pairIndex in packetPairs.indices){
48+
val (packetA,packetB) = packetPairs[pairIndex].split('\n')
49+
50+
val leftGroup = parseSignal(packetA)
51+
val rightGroup = parseSignal(packetB)
52+
53+
val ordered = areOrdered(leftGroup, rightGroup)
54+
55+
//add pairIndex to sum
56+
println("")
57+
}
58+
59+
return input
60+
}
61+
62+
fun areOrdered(leftGroup: Group, rightGroup: Group): Any {
63+
val i = 0
64+
when (leftGroup.signalGroup[i].getType()){
65+
//both values are lists, compare the first value of each list, then the second value, and so on. If the left list runs out of items first, the inputs are in the right order. If the right list runs out of items first, the inputs are not in the right order.
66+
SignalEntryType.GROUP -> {
67+
val leftSignal = leftGroup.signalGroup[i].asGroup()
68+
val rightSignal = rightGroup.signalGroup[i].asGroup()
69+
false
70+
}
71+
//both values are integers, the lower integer should come first
72+
//exactly one value is an integer, convert the integer to a list which contains that integer as its only value, then retry the comparison
73+
SignalEntryType.DATA -> {
74+
if (rightGroup.signalGroup[i].getType() == SignalEntryType.GROUP) {
75+
val leftSignal = leftGroup.signalGroup[i].asGroup()
76+
}else{
77+
val leftSignal = leftGroup.signalGroup[1].asData()
78+
}
79+
false
80+
}
81+
}
82+
83+
84+
return false
85+
}
86+
87+
private fun parseSignal(packetA: String): Group {
88+
val groups = Stack<Group>()
89+
var tmpContent = ""
90+
for (a_i in packetA.indices) {
91+
when (packetA[a_i]) {
92+
'[' -> groups.push(Group())
93+
']' -> {
94+
if (a_i + 1 < packetA.length) {
95+
val completedGroup = groups.pop()
96+
groups.peek().addSignalEntry(completedGroup)
97+
}
98+
}
99+
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0' -> {
100+
//read all content
101+
tmpContent += packetA[a_i]
102+
//create Data
103+
groups.peek().addSignalEntry(Data(tmpContent))
104+
}
105+
',' -> {
106+
tmpContent = ""
107+
//do nothing, next item...
108+
}
109+
}
110+
}
111+
return groups.pop().asGroup()
112+
}
113+
114+
enum class SignalEntryType {
115+
GROUP,
116+
DATA
117+
}
118+
119+
interface SignalEntry {
120+
fun getType() : SignalEntryType
121+
fun asGroup(): Group
122+
fun asData(): Data
123+
}
124+
125+
data class Group(val signalGroup: MutableList<SignalEntry> = mutableListOf()) : SignalEntry {
126+
override fun getType() = SignalEntryType.GROUP
127+
override fun asGroup() = this
128+
override fun asData() = throw UnexpectedException("Group cannot be read as data")
129+
130+
fun addSignalEntry(signalEntry: SignalEntry){
131+
signalGroup.add(signalEntry)
132+
}
133+
}
134+
135+
136+
data class Data(var content: String = "") : SignalEntry{
137+
override fun getType() = SignalEntryType.DATA
138+
override fun asGroup() = Group(listOf(this).toMutableList())
139+
override fun asData() = this
140+
}
141+
142+
143+
/**
144+
*
145+
* Answer: ???
146+
*/
147+
private fun solutionB(input: String): Any {
148+
val rows = input.split('\n')
149+
150+
return input
151+
}
152+

0 commit comments

Comments
 (0)