Advertisement
Guest User

https://adventofcode.com/2024/day/5

a guest
Dec 5th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as fs from 'fs';
  2. import * as readline from 'readline';
  3. import { performance } from 'perf_hooks';
  4.  
  5.  
  6. function swap(array: number[], from: number, to: number): void {
  7.     [array[from], array[to]] = [array[to], array[from]];
  8. }
  9.  
  10. function rearrangeSequence(sortingPairs: number[][], sequence: number[], iterations: number = 0, maxIterations = 100): number {
  11.     if (iterations > maxIterations) {
  12.         console.error('Max iterations reached');
  13.         return 0;
  14.     }
  15.  
  16.     let swapped = false;
  17.     for (let i = 0; i < sortingPairs.length; i++) {
  18.         const [start, end] = sortingPairs[i];
  19.         const startPos = sequence.indexOf(start);
  20.         const endPos = sequence.indexOf(end);
  21.  
  22.         if (startPos > endPos) {
  23.             swap(sequence, startPos, endPos);
  24.             swapped = true;            
  25.         }
  26.     }
  27.  
  28.     if (swapped) {
  29.         iterations++;
  30.         return rearrangeSequence(sortingPairs, sequence, iterations, maxIterations);
  31.     }    
  32.  
  33.     return iterations;
  34. }
  35.  
  36. const rl = readline.createInterface({
  37.   input: fs.createReadStream(__dirname + '/day5a_input.txt')
  38. });
  39.  
  40. const sortingPairs: number[][] = [];
  41. const sequences: number[][] = [];
  42.  
  43. rl.on('line', (line) => {
  44.     if (line.indexOf('|') !== -1) {
  45.         sortingPairs.push(line.split('|').map((pair) => +pair.trim()));
  46.     } else if (line.trim() !== '') {
  47.         sequences.push(line.split(',').map((order) => +order.trim()));
  48.     }  
  49. });
  50. rl.on('close', () => {
  51.     let day1Answer = 0;
  52.     let day2Answer = 0;
  53.     let totalIterations = 0;
  54.     let maxIterations = 0;
  55.  
  56.     const startTime = performance.now()
  57.    
  58.     sequences.forEach((sequence) => {    
  59.         // Get sorting pairs where both numbers exist in the sequence
  60.         const matchingSortingPairs = sortingPairs.filter((pair) => sequence.indexOf(pair[0]) >= 0 && sequence.indexOf(pair[1]) >= 0);
  61.        
  62.         let iterations = rearrangeSequence(matchingSortingPairs, sequence, 0);
  63.         if (iterations > 0) {
  64.             day2Answer += sequence[Math.floor(sequence.length / 2)];            
  65.         } else {
  66.             day1Answer += sequence[Math.floor(sequence.length / 2)];
  67.         }
  68.         totalIterations += iterations;
  69.         maxIterations = Math.max(maxIterations, iterations);
  70.     });
  71.     console.log(day1Answer);    
  72.     console.log(day2Answer);    
  73.     console.log(maxIterations);    
  74.     const endTime = performance.now();    
  75.     console.log(`the solution took ${endTime - startTime} milliseconds`)
  76. });
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement