Skip to content

Commit 021bc67

Browse files
authored
Merge pull request devcordde#36 from NyCodeGHG/master
Kotlin solution for day 6
2 parents 2f9e268 + cb01ca3 commit 021bc67

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

Day-06/kotlin/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.txt

Day-06/kotlin/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Kotlin Solution
2+
I recommend to use [kscript](https://github.com/holgerbrandl/kscript) by [@holgerbrandl](https://github.com/holgerbrandl) to execute the script file.

Day-06/kotlin/solution.kts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import java.nio.file.Files
2+
import java.nio.file.Paths
3+
4+
val answers = ArrayList<Answer>()
5+
6+
var answer = Answer()
7+
val lines = Files.readAllLines(Paths.get("input.txt"))
8+
9+
var index = 0
10+
var lastLineCount = 0
11+
12+
while (index < lines.size) {
13+
val line = lines[index]
14+
15+
if (line.isBlank()) {
16+
index++
17+
continue
18+
}
19+
20+
answer.people = ++lastLineCount
21+
answer.questions.addAll(line.toCharArray().toMutableSet())
22+
23+
if (++index < lines.size) {
24+
if (lines[index].isBlank()) {
25+
answers.add(answer)
26+
answer = Answer()
27+
lastLineCount = 0
28+
}
29+
} else {
30+
answers.add(answer)
31+
answer = Answer()
32+
lastLineCount = 0
33+
}
34+
}
35+
36+
println(answers.map { it.questions.size }.sum())
37+
38+
val groups = ArrayList<Group>()
39+
var group = Group()
40+
index = 0
41+
lastLineCount = 0
42+
43+
while (index < lines.size) {
44+
val line = lines[index]
45+
46+
if (line.isBlank()) {
47+
index++
48+
continue
49+
}
50+
51+
group.answers.put(++lastLineCount, Answer(line.toCharArray().toMutableSet(), lastLineCount))
52+
53+
if (++index < lines.size) {
54+
if (lines[index].isBlank()) {
55+
groups.add(group)
56+
group = Group()
57+
lastLineCount = 0
58+
}
59+
} else {
60+
groups.add(group)
61+
group = Group()
62+
lastLineCount = 0
63+
}
64+
}
65+
66+
println(groups.map { aGroup ->
67+
val questions = aGroup.answers.map { it.value.questions }.flatten().toMutableSet()
68+
questions.removeIf { char ->
69+
aGroup.answers.filter { !it.value.questions.contains(char) }.isNotEmpty()
70+
}
71+
questions.size
72+
}.sum())
73+
74+
data class Group(val answers: MutableMap<Int, Answer> = mutableMapOf())
75+
76+
data class Answer(val questions: MutableSet<Char> = mutableSetOf<Char>(), var people: Int = 0)

0 commit comments

Comments
 (0)