day 2 puzzle 1
This commit is contained in:
		
							
								
								
									
										35
									
								
								src/day2/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/day2/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| #import "/src/utils.typ": * | ||||
|  | ||||
| #let solve(input) = { | ||||
|   let safe-cnt = 0 | ||||
|   for line in input.split("\n") { | ||||
|     let nums = line.split(" ").map(n => int(n)) | ||||
|  | ||||
|     let increasing | ||||
|     let safe = true | ||||
|     for i in range(nums.len() - 1) { | ||||
|       let d = nums.at(i + 1) - nums.at(i) | ||||
|       let abs-d = calc.abs(d) | ||||
|       if abs-d < 1 or abs-d > 3 { | ||||
|         safe = false | ||||
|         break | ||||
|       } | ||||
|       if i == 0 { | ||||
|         increasing = d > 0 | ||||
|       } else if (d > 0) != increasing { | ||||
|         safe = false | ||||
|         break | ||||
|       } | ||||
|     } | ||||
|     if safe { | ||||
|       safe-cnt += 1 | ||||
|     } | ||||
|   } | ||||
|   return safe-cnt | ||||
| } | ||||
|  | ||||
| #show-puzzle( | ||||
|   2, 1, | ||||
|   solve, | ||||
|   example: 2 | ||||
| ) | ||||
							
								
								
									
										0
									
								
								src/day2/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/day2/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user