day 8 puzzle 1
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								progress.png
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								progress.png
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 113 KiB After Width: | Height: | Size: 114 KiB | 
| @@ -11,4 +11,6 @@ | ||||
| 6: | ||||
|   stars: 2 | ||||
| 7: | ||||
|   stars: 2 | ||||
|   stars: 2 | ||||
| 8: | ||||
|   stars: 1 | ||||
							
								
								
									
										12
									
								
								res/examples/day8.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								res/examples/day8.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,12 @@ | ||||
| ............ | ||||
| ........0... | ||||
| .....0...... | ||||
| .......0.... | ||||
| ....0....... | ||||
| ......A..... | ||||
| ............ | ||||
| ............ | ||||
| ........A... | ||||
| .........A.. | ||||
| ............ | ||||
| ............ | ||||
							
								
								
									
										51
									
								
								src/day8/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								src/day8/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,51 @@ | ||||
| #import "/src/utils.typ": * | ||||
|  | ||||
| #let in-grid(w, h, x, y) = { | ||||
|   return 0 <= x and x < w and 0 <= y and y < h | ||||
| } | ||||
|  | ||||
| #let solve(input) = { | ||||
|   let by-freq = (:) | ||||
|   let antinodes = () | ||||
|  | ||||
|   let grid = input.split("\n").map(l => l.clusters()) | ||||
|   let w = grid.first().len() | ||||
|   let h = grid.len() | ||||
|  | ||||
|   let in-grid = in-grid.with(w, h) | ||||
|  | ||||
|   for y in range(h) { | ||||
|     for x in range(w) { | ||||
|       let c = grid.at(y).at(x) | ||||
|       if c == "." { | ||||
|         continue | ||||
|       } | ||||
|       if c not in by-freq { | ||||
|         by-freq.insert(c, ()) | ||||
|       } | ||||
|  | ||||
|       for (x2, y2) in by-freq.at(c) { | ||||
|         let (dx, dy) = (x2 - x, y2 - y) | ||||
|         let node1 = (x - dx, y - dy) | ||||
|         let node2 = (x2 + dx, y2 + dy) | ||||
|  | ||||
|         if in-grid(..node1) and node1 not in antinodes { | ||||
|           antinodes.push(node1) | ||||
|         } | ||||
|  | ||||
|         if in-grid(..node2) and node2 not in antinodes { | ||||
|           antinodes.push(node2) | ||||
|         } | ||||
|       } | ||||
|       by-freq.at(c).push((x, y)) | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   return antinodes.len() | ||||
| } | ||||
|  | ||||
| #show-puzzle( | ||||
|   8, 1, | ||||
|   solve, | ||||
|   example: 14 | ||||
| ) | ||||
							
								
								
									
										0
									
								
								src/day8/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/day8/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user