Advertisement
_joel1024

Untitled

Dec 5th, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { parseInputFile } from "./sol1";
  2.  
  3. const input = parseInputFile("input.txt");
  4.  
  5. const ruleMap: Record<number, number[]> = {}
  6.  
  7. input.tuples.forEach(tuple => {
  8.     if (!ruleMap[tuple[0]]) ruleMap[tuple[0]] = []
  9.  
  10.     ruleMap[tuple[0]].push(tuple[1])
  11. })
  12.  
  13. const checkSubCondition = (array: number[], el: number) => array.slice(el + 1).every(n => array[el] in ruleMap && ruleMap[array[el]].includes(n))
  14. const checkCondition = (array: number[]) =>
  15.     array.every((_, i) => i === array.length - 1
  16.         ? true
  17.         : checkSubCondition(array, i))
  18.  
  19. const ans = input.arrays.reduce((acc, array) => {
  20.     if (!checkCondition(array)) {
  21.         for (let i = 0; i < array.length; i++) {
  22.             if (checkSubCondition(array, i)) continue
  23.             const temp = [...array]
  24.             const removed = temp.splice(i, 1)
  25.             for (let j = 0; j < temp.length; j++) {
  26.                 array = [...temp]
  27.                 array.splice(j + 1, 0, removed[0])
  28.                 if (checkCondition(array)) {
  29.                     console.log(array, acc)
  30.                     return acc += array[Math.floor(array.length / 2)];
  31.                 }
  32.             }
  33.         }
  34.     }
  35.  
  36.     return acc
  37. }, 0)
  38.  
  39. console.log(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement