File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change 1+ * .txt 
Original file line number Diff line number Diff line change 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.
Original file line number Diff line number Diff line change 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 )
                                 You can’t perform that action at this time. 
               
                  
0 commit comments