Add solution day 25
This commit is contained in:
		
							parent
							
								
									134bf147e9
								
							
						
					
					
						commit
						2acf1db252
					
				
					 3 changed files with 4104 additions and 0 deletions
				
			
		
							
								
								
									
										39
									
								
								day_25/example.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								day_25/example.txt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,39 @@
 | 
				
			||||||
 | 
					#####
 | 
				
			||||||
 | 
					.####
 | 
				
			||||||
 | 
					.####
 | 
				
			||||||
 | 
					.####
 | 
				
			||||||
 | 
					.#.#.
 | 
				
			||||||
 | 
					.#...
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#####
 | 
				
			||||||
 | 
					##.##
 | 
				
			||||||
 | 
					.#.##
 | 
				
			||||||
 | 
					...##
 | 
				
			||||||
 | 
					...#.
 | 
				
			||||||
 | 
					...#.
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					#....
 | 
				
			||||||
 | 
					#....
 | 
				
			||||||
 | 
					#...#
 | 
				
			||||||
 | 
					#.#.#
 | 
				
			||||||
 | 
					#.###
 | 
				
			||||||
 | 
					#####
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					#.#..
 | 
				
			||||||
 | 
					###..
 | 
				
			||||||
 | 
					###.#
 | 
				
			||||||
 | 
					###.#
 | 
				
			||||||
 | 
					#####
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					.....
 | 
				
			||||||
 | 
					#....
 | 
				
			||||||
 | 
					#.#..
 | 
				
			||||||
 | 
					#.#.#
 | 
				
			||||||
 | 
					#####
 | 
				
			||||||
							
								
								
									
										3999
									
								
								day_25/input.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										3999
									
								
								day_25/input.txt
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										66
									
								
								day_25/solve_1.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								day_25/solve_1.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,66 @@
 | 
				
			||||||
 | 
					// 
 | 
				
			||||||
 | 
					export type KeyImage = string[][];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export async function readKeyImageData(path: string): Promise<KeyImage[]> {
 | 
				
			||||||
 | 
					    const data = await Deno.readTextFile(path);
 | 
				
			||||||
 | 
					    const keyImage: KeyImage[] = data.replaceAll("\r","").split('\n\n').map((line) => line.split('\n').map((char) => char.split('')));
 | 
				
			||||||
 | 
					    return keyImage;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type KeyImageType = "key" | "lock";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function getKeyImageType(keyImage: KeyImage): KeyImageType {
 | 
				
			||||||
 | 
					    return keyImage[0][0] === "." ? "key" : "lock";
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export type LockShape = number[];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function getLockShape(keyImage: KeyImage): LockShape {
 | 
				
			||||||
 | 
					    const lockShape: LockShape = [0,0,0,0,0]
 | 
				
			||||||
 | 
					    let start, increment, end;
 | 
				
			||||||
 | 
					    if (getKeyImageType(keyImage) === "key") {
 | 
				
			||||||
 | 
					        start = keyImage.length - 1;
 | 
				
			||||||
 | 
					        increment = -1;
 | 
				
			||||||
 | 
					        end = -1;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else {
 | 
				
			||||||
 | 
					        start = 0;
 | 
				
			||||||
 | 
					        increment = 1;
 | 
				
			||||||
 | 
					        end = keyImage.length;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (let i = 0; i < keyImage[0].length; i++) {
 | 
				
			||||||
 | 
					        let height = 0;
 | 
				
			||||||
 | 
					        for (let j = start; j != end ; j += increment) {
 | 
				
			||||||
 | 
					            if (keyImage[j][i] === ".") {
 | 
				
			||||||
 | 
					                lockShape[i] = height - 1; 
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            height++;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    return lockShape;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if (import.meta.main) {
 | 
				
			||||||
 | 
					    const keyImage = await readKeyImageData(new URL('input.txt', import.meta.url).pathname.slice(1));
 | 
				
			||||||
 | 
					    const keys = keyImage.filter((key) => getKeyImageType(key) === "key").map(getLockShape);
 | 
				
			||||||
 | 
					    const locks = keyImage.filter((key) => getKeyImageType(key) === "lock").map(getLockShape);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let fitCount = 0;
 | 
				
			||||||
 | 
					    for (const lock of locks) {
 | 
				
			||||||
 | 
					        for (const key of keys) {
 | 
				
			||||||
 | 
					            let fit = true;
 | 
				
			||||||
 | 
					            for (let i = 0; i < 5; i++) {
 | 
				
			||||||
 | 
					                if (key[i] + lock[i] > 5) {
 | 
				
			||||||
 | 
					                    fit = false;
 | 
				
			||||||
 | 
					                    break;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            if (fit) {
 | 
				
			||||||
 | 
					                fitCount++;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    console.log(fitCount);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue