complete day 3
This commit is contained in:
		
							parent
							
								
									10e59bd5ba
								
							
						
					
					
						commit
						c9ca250a9a
					
				
					 2 changed files with 34 additions and 0 deletions
				
			
		
							
								
								
									
										13
									
								
								03-a.kts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								03-a.kts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,13 @@ | |||
| import java.util.Scanner | ||||
| import java.util.regex.Pattern | ||||
| 
 | ||||
| val pattern: Pattern = Pattern.compile("mul\\([0-9]+,[0-9]+\\)") | ||||
| val scanner = Scanner(System.`in`) | ||||
| 
 | ||||
| var sum = 0 | ||||
| while (true) { | ||||
|     val next: String = scanner.findWithinHorizon(pattern, 0) ?: break | ||||
| 
 | ||||
|     sum += next.replace("mul(", "").replace(")", "").split(",").map { it.toInt() }.let { (a, b) -> a*b } | ||||
| } | ||||
| println(sum) | ||||
							
								
								
									
										21
									
								
								03-b.kts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								03-b.kts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,21 @@ | |||
| import java.util.Scanner | ||||
| import java.util.regex.Pattern | ||||
| 
 | ||||
| val pattern: Pattern = Pattern.compile("mul\\([0-9]+,[0-9]+\\)|do\\(\\)|don't\\(\\)") | ||||
| 
 | ||||
| val scanner = Scanner(System.`in`) | ||||
| 
 | ||||
| var sum = 0 | ||||
| var enabled = true | ||||
| while (true) { | ||||
|     val next: String = scanner.findWithinHorizon(pattern, 0) ?: break | ||||
| 
 | ||||
|     when (next) { | ||||
|         "do()" -> enabled = true | ||||
|         "don't()" -> enabled = false | ||||
|         else -> if (enabled) { | ||||
|             sum += next.replace("mul(", "").replace(")", "").split(",").map { it.toInt() }.let { (a, b) -> a*b } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| println(sum) | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue