complete day 5
This commit is contained in:
		
							parent
							
								
									cbbd95f7ab
								
							
						
					
					
						commit
						8c28519674
					
				
					 2 changed files with 70 additions and 0 deletions
				
			
		
							
								
								
									
										39
									
								
								05-a.kts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								05-a.kts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,39 @@ | ||||||
|  | import java.util.Scanner | ||||||
|  | 
 | ||||||
|  | val scanner = Scanner(System.`in`) | ||||||
|  | 
 | ||||||
|  | val rules = HashMap<Int, MutableSet<Int>>() | ||||||
|  | 
 | ||||||
|  | while (scanner.hasNextLine()) { | ||||||
|  |     val line = scanner.nextLine() | ||||||
|  |     if (line == "") { | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     val (before, after) = line.split("|").map(String::toInt) | ||||||
|  |     if (!rules.contains(before)) { | ||||||
|  |         rules[before] = HashSet<Int>() | ||||||
|  |     } | ||||||
|  |     rules[before]!!.add(after) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | var sum = 0 | ||||||
|  | while (scanner.hasNextLine()) { | ||||||
|  |     val line = scanner.nextLine() | ||||||
|  |     val pages = line.split(",").map(String::toInt) | ||||||
|  | 
 | ||||||
|  |     val result = pages.fold(Pair(HashSet<Int>(), true)) { (appeared, isSafe), page -> | ||||||
|  |         val shouldBeAfter = rules[page]?.toSet() ?: emptySet() | ||||||
|  |         val violateRule = appeared.intersect(shouldBeAfter) | ||||||
|  |         val stillSafe = isSafe && violateRule.isEmpty() | ||||||
|  | 
 | ||||||
|  |         appeared.add(page) | ||||||
|  |         Pair(appeared, stillSafe) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (result.second) { | ||||||
|  |         val pagesArray = pages.toIntArray() | ||||||
|  |         sum += pagesArray[pagesArray.size / 2] | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | println(sum) | ||||||
							
								
								
									
										31
									
								
								05-b.kts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								05-b.kts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,31 @@ | ||||||
|  | import java.util.Scanner | ||||||
|  | 
 | ||||||
|  | val scanner = Scanner(System.`in`) | ||||||
|  | 
 | ||||||
|  | val rules = HashMap<Int, MutableSet<Int>>() | ||||||
|  | 
 | ||||||
|  | while (scanner.hasNextLine()) { | ||||||
|  |     val line = scanner.nextLine() | ||||||
|  |     if (line == "") { | ||||||
|  |         break; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     val (before, after) = line.split("|").map(String::toInt) | ||||||
|  |     if (!rules.contains(before)) { | ||||||
|  |         rules[before] = HashSet<Int>() | ||||||
|  |     } | ||||||
|  |     rules[before]!!.add(after) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | var sum = 0 | ||||||
|  | while (scanner.hasNextLine()) { | ||||||
|  |     val line = scanner.nextLine() | ||||||
|  |     val pages = line.split(",").map(String::toInt) | ||||||
|  |     val fixed = pages.sortedWith { l, r -> if (l == r) 0 else if (rules[l]?.contains(r) == true) -1 else 1 } | ||||||
|  | 
 | ||||||
|  |     if ("$pages" != "$fixed") { | ||||||
|  |         val fixedArray = fixed.toIntArray() | ||||||
|  |         sum += fixedArray[fixedArray.size / 2] | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | println(sum) | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue