Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { parseInputFile } from "./sol1";
- const input = parseInputFile("input.txt");
- const ruleMap: Record<number, number[]> = {}
- input.tuples.forEach(tuple => {
- if (!ruleMap[tuple[0]]) ruleMap[tuple[0]] = []
- ruleMap[tuple[0]].push(tuple[1])
- })
- const checkSubCondition = (array: number[], el: number) => array.slice(el + 1).every(n => array[el] in ruleMap && ruleMap[array[el]].includes(n))
- const checkCondition = (array: number[]) =>
- array.every((_, i) => i === array.length - 1
- ? true
- : checkSubCondition(array, i))
- const ans = input.arrays.reduce((acc, array) => {
- if (!checkCondition(array)) {
- for (let i = 0; i < array.length; i++) {
- if (checkSubCondition(array, i)) continue
- const temp = [...array]
- const removed = temp.splice(i, 1)
- for (let j = 0; j < temp.length; j++) {
- array = [...temp]
- array.splice(j + 1, 0, removed[0])
- if (checkCondition(array)) {
- console.log(array, acc)
- return acc += array[Math.floor(array.length / 2)];
- }
- }
- }
- }
- return acc
- }, 0)
- console.log(ans)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement