complete day 7
This commit is contained in:
		
							parent
							
								
									64dadfa0da
								
							
						
					
					
						commit
						7891c409bb
					
				
					 2 changed files with 62 additions and 0 deletions
				
			
		
							
								
								
									
										30
									
								
								07-a.kts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								07-a.kts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,30 @@ | |||
| import java.math.BigInteger | ||||
| import java.util.* | ||||
| 
 | ||||
| val scanner = Scanner(System.`in`) | ||||
| 
 | ||||
| var sum: BigInteger = BigInteger.ZERO | ||||
| while (scanner.hasNextLine()) { | ||||
|     val line = scanner.nextLine() | ||||
|     val numbersInLine = line.split("[: ]+".toRegex()).map(String::toBigInteger) | ||||
|     val target = numbersInLine.first() | ||||
|     val numbers = numbersInLine.drop(1) | ||||
|     val possibleResults = findAllPossibleResult(setOf(numbers.first()), numbers.drop(1)) | ||||
|     if (possibleResults.contains(target)) { | ||||
|         sum += target | ||||
|     } | ||||
| } | ||||
| println(sum) | ||||
| 
 | ||||
| fun findAllPossibleResult(partialResults: Set<BigInteger>, numbers: List<BigInteger>): Set<BigInteger> { | ||||
|     if (numbers.isEmpty()) return partialResults | ||||
| 
 | ||||
|     val head = numbers.first() | ||||
|     val tail = numbers.drop(1) | ||||
|     val newResults = partialResults.fold(HashSet<BigInteger>()) { r, p -> | ||||
|         r.add(p.plus(head)) | ||||
|         r.add(p.times(head)) | ||||
|         r | ||||
|     } | ||||
|     return findAllPossibleResult(newResults, tail) | ||||
| } | ||||
							
								
								
									
										32
									
								
								07-b.kts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								07-b.kts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,32 @@ | |||
| import java.math.BigInteger | ||||
| import java.util.* | ||||
| 
 | ||||
| val scanner = Scanner(System.`in`) | ||||
| 
 | ||||
| var sum: BigInteger = BigInteger.ZERO | ||||
| while (scanner.hasNextLine()) { | ||||
|     val line = scanner.nextLine() | ||||
|     val numbersInLine = line.split("[: ]+".toRegex()).map(String::toBigInteger) | ||||
|     val target = numbersInLine.first() | ||||
|     val numbers = numbersInLine.drop(1) | ||||
|     val possibleResults = findAllPossibleResult(setOf(numbers.first()), numbers.drop(1)) | ||||
|     if (possibleResults.contains(target)) { | ||||
|         sum += target | ||||
| //        println(line) | ||||
|     } | ||||
| } | ||||
| println(sum) | ||||
| 
 | ||||
| fun findAllPossibleResult(partialResults: Set<BigInteger>, numbers: List<BigInteger>): Set<BigInteger> { | ||||
|     if (numbers.isEmpty()) return partialResults | ||||
| 
 | ||||
|     val head = numbers.first() | ||||
|     val tail = numbers.drop(1) | ||||
|     val newResults = partialResults.fold(HashSet<BigInteger>()) { r, p -> | ||||
|         r.add("$p$head".toBigInteger()) | ||||
|         r.add(p.plus(head)) | ||||
|         r.add(p.times(head)) | ||||
|         r | ||||
|     } | ||||
|     return findAllPossibleResult(newResults, tail) | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue