Compare commits
	
		
			2 Commits
		
	
	
		
			a35be7ec92
			...
			242eaeb634
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 242eaeb634 | |||
| 2cbd13d224 | 
							
								
								
									
										
											BIN
										
									
								
								progress.png
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								progress.png
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 120 KiB | 
| @@ -16,3 +16,5 @@ | |||||||
|   stars: 2 |   stars: 2 | ||||||
| 9: | 9: | ||||||
|   stars: 2 |   stars: 2 | ||||||
|  | 10: | ||||||
|  |   stars: 2 | ||||||
							
								
								
									
										8
									
								
								res/examples/day10.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								res/examples/day10.txt
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | 89010123 | ||||||
|  | 78121874 | ||||||
|  | 87430965 | ||||||
|  | 96549874 | ||||||
|  | 45678903 | ||||||
|  | 32019012 | ||||||
|  | 01329801 | ||||||
|  | 10456732 | ||||||
							
								
								
									
										68
									
								
								src/day10/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/day10/puzzle1.typ
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | |||||||
|  | #import "/src/utils.typ": * | ||||||
|  |  | ||||||
|  | #let offsets = ( | ||||||
|  |   (-1, 0), | ||||||
|  |   (0, -1), | ||||||
|  |   (1, 0), | ||||||
|  |   (0, 1) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | #let in-grid(w, h, x, y) = { | ||||||
|  |   return 0 <= x and x < w and 0 <= y and y < h | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #let count-paths(grid, w, h, ox, oy) = { | ||||||
|  |   let tails = () | ||||||
|  |   let in-grid = in-grid.with(w, h) | ||||||
|  |  | ||||||
|  |   let to-visit = ((ox, oy),) | ||||||
|  |   while to-visit.len() != 0 { | ||||||
|  |     let (x, y) = to-visit.remove(0) | ||||||
|  |     let v = grid.at(y).at(x) | ||||||
|  |     for (dx, dy) in offsets { | ||||||
|  |       let (x2, y2) = (x + dx, y + dy) | ||||||
|  |       if not in-grid(x2, y2) { | ||||||
|  |         continue | ||||||
|  |       } | ||||||
|  |       let v2 = grid.at(y2).at(x2) | ||||||
|  |       if v2 == v + 1 { | ||||||
|  |         let pos2 = (x2, y2) | ||||||
|  |         if v2 == 9 { | ||||||
|  |           if pos2 not in tails { | ||||||
|  |             tails.push(pos2) | ||||||
|  |           } | ||||||
|  |         } else { | ||||||
|  |           to-visit.push(pos2) | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return tails.len() | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #let solve(input) = { | ||||||
|  |   let grid = input.split("\n").map(l => l.clusters().map(int)) | ||||||
|  |  | ||||||
|  |   let w = grid.first().len() | ||||||
|  |   let h = grid.len() | ||||||
|  |  | ||||||
|  |   let count-paths = count-paths.with(grid, w, h) | ||||||
|  |  | ||||||
|  |   let total = 0 | ||||||
|  |   for y in range(h) { | ||||||
|  |     for x in range(w) { | ||||||
|  |       if grid.at(y).at(x) == 0 { | ||||||
|  |         total += count-paths(x, y) | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return total | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #show-puzzle( | ||||||
|  |   10, 1, | ||||||
|  |   solve, | ||||||
|  |   example: 36 | ||||||
|  | ) | ||||||
							
								
								
									
										74
									
								
								src/day10/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								src/day10/puzzle2.typ
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,74 @@ | |||||||
|  | #import "/src/utils.typ": * | ||||||
|  | #let offsets = ( | ||||||
|  |   (-1, 0), | ||||||
|  |   (0, -1), | ||||||
|  |   (1, 0), | ||||||
|  |   (0, 1) | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | #let in-grid(w, h, x, y) = { | ||||||
|  |   return 0 <= x and x < w and 0 <= y and y < h | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #let count-paths(grid, w, h, ox, oy) = { | ||||||
|  |   let in-grid = in-grid.with(w, h) | ||||||
|  |   let rating = 0 | ||||||
|  |  | ||||||
|  |   let to-visit = ((ox, oy),) | ||||||
|  |   while to-visit.len() != 0 { | ||||||
|  |     let (x, y) = to-visit.remove(0) | ||||||
|  |     let v = grid.at(y).at(x) | ||||||
|  |     let branches = 0 | ||||||
|  |     for (dx, dy) in offsets { | ||||||
|  |       let (x2, y2) = (x + dx, y + dy) | ||||||
|  |       if not in-grid(x2, y2) { | ||||||
|  |         continue | ||||||
|  |       } | ||||||
|  |       let v2 = grid.at(y2).at(x2) | ||||||
|  |       if v2 == v + 1 { | ||||||
|  |         let pos2 = (x2, y2) | ||||||
|  |         branches += 1 | ||||||
|  |         if v2 != 9 { | ||||||
|  |           to-visit.push(pos2) | ||||||
|  |         } | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     rating += if v == 0 { | ||||||
|  |       branches | ||||||
|  |     } else { | ||||||
|  |       branches - 1  // If no branch -> -1 | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return rating | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #let solve(input) = { | ||||||
|  |   let grid = input.split("\n").map(l => l.clusters().map(int)) | ||||||
|  |  | ||||||
|  |   let w = grid.first().len() | ||||||
|  |   let h = grid.len() | ||||||
|  |  | ||||||
|  |   let count-paths = count-paths.with(grid, w, h) | ||||||
|  |  | ||||||
|  |   let ratings = () | ||||||
|  |   let total = 0 | ||||||
|  |   for y in range(h) { | ||||||
|  |     for x in range(w) { | ||||||
|  |       if grid.at(y).at(x) == 0 { | ||||||
|  |         let rating = count-paths(x, y) | ||||||
|  |         total += rating | ||||||
|  |         ratings.push(rating) | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |   } | ||||||
|  |   let a = ratings | ||||||
|  |  | ||||||
|  |   return total | ||||||
|  | } | ||||||
|  |  | ||||||
|  | #show-puzzle( | ||||||
|  |   10, 2, | ||||||
|  |   solve, | ||||||
|  |   example: 81 | ||||||
|  | ) | ||||||
							
								
								
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								src/main.pdf
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							| @@ -189,5 +189,10 @@ | |||||||
|  |  | ||||||
| #let template(body) = { | #let template(body) = { | ||||||
|   set text(font: "Source Sans 3") |   set text(font: "Source Sans 3") | ||||||
|  |   set page( | ||||||
|  |     footer: context { | ||||||
|  |       align(center, counter(page).display("1 / 1", both: true)) | ||||||
|  |     } | ||||||
|  |   ) | ||||||
|   body |   body | ||||||
| } | } | ||||||
		Reference in New Issue
	
	Block a user