mirror of
https://github.com/seigler/aoc2024
synced 2025-07-26 00:36:10 +00:00
cleaner, better
This commit is contained in:
parent
ee81d62ef8
commit
0aa40e557a
2 changed files with 14 additions and 25 deletions
|
@ -66,13 +66,13 @@
|
|||
"solved": true,
|
||||
"result": "4905",
|
||||
"attempts": [],
|
||||
"time": 29.389645
|
||||
"time": 28.306566
|
||||
},
|
||||
"part2": {
|
||||
"solved": true,
|
||||
"result": "6204",
|
||||
"result": "0",
|
||||
"attempts": [],
|
||||
"time": 54.772924
|
||||
"time": 35.287833
|
||||
}
|
||||
},
|
||||
{
|
||||
|
|
|
@ -11,33 +11,22 @@ const parseInput = (rawInput: string) => {
|
|||
const part1 = (rawInput: string) => {
|
||||
const {rules, updates} = parseInput(rawInput)
|
||||
return updates.reduce((total, update) => {
|
||||
let seen: number[] = []
|
||||
for (const page of update) {
|
||||
if (rules.some(rule => seen.includes(rule[1]) && rule[0] === page)) {
|
||||
return total
|
||||
}
|
||||
seen.push(page)
|
||||
}
|
||||
return total + seen[Math.floor(seen.length / 2)]
|
||||
const sortedUpdate = [...update].sort((p1,p2) => {
|
||||
return rules.some(rule => rule[0] === p1 && rule[1] === p2) ? -1 : 1
|
||||
})
|
||||
if (update.join() !== sortedUpdate.join()) return total
|
||||
return total + update[Math.floor(update.length / 2)]
|
||||
}, 0)
|
||||
}
|
||||
|
||||
const part2 = (rawInput: string) => {
|
||||
const {rules, updates} = parseInput(rawInput)
|
||||
return updates.reduce((total, update) => {
|
||||
let seen: number[] = []
|
||||
for (const page of update) {
|
||||
if (rules.some(rule => seen.includes(rule[1]) && rule[0] === page)) {
|
||||
// needs fixing
|
||||
const sortedUpdate = update.sort((p1,p2) => {
|
||||
return rules.some(rule => rule[0] === p1 && rule[1] === p2) ? -1 : 1
|
||||
})
|
||||
return total + update[Math.floor(update.length / 2)]
|
||||
}
|
||||
seen.push(page)
|
||||
}
|
||||
// it's fine
|
||||
return total
|
||||
const sortedUpdate = [...update].sort((p1,p2) => {
|
||||
return rules.some(rule => rule[0] === p1 && rule[1] === p2) ? -1 : 1
|
||||
})
|
||||
if (update.join() === sortedUpdate.join()) return total
|
||||
return total + sortedUpdate[Math.floor(update.length / 2)]
|
||||
}, 0)
|
||||
}
|
||||
|
||||
|
@ -115,5 +104,5 @@ run({
|
|||
solution: part2,
|
||||
},
|
||||
trimTestInputs: true,
|
||||
onlyTests: false,
|
||||
onlyTests: true,
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue