aoc-2023/day_19/range.ts

39 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-12-09 22:41:02 +09:00
import { Condition } from "./parser.ts";
// inclusive range
export type MaterialRange = {
x: [number, number],
m: [number, number],
a: [number, number],
s: [number, number],
}
export function partition(cond: Condition, mRange: MaterialRange):
null | MaterialRange | [MaterialRange, MaterialRange] {
const [min, max] = mRange[cond.quality];
if (cond.cond === "<") {
if (min >= cond.value) return null;
if (max < cond.value) return mRange;
return [
{ // false condition
...mRange,
[cond.quality]: [cond.value, max],
},
{ // true condition
...mRange,
[cond.quality]: [min, cond.value - 1],
},
];
}
else { // if >
if (max <= cond.value) return null;
if (min > cond.value) return mRange;
return [{ // false condition
...mRange,
[cond.quality]: [min, cond.value],
}, { // true condition
...mRange,
[cond.quality]: [cond.value + 1, max],
}];
}
}