Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { parser } from "./sol1";
- const reports = parser("input.txt");
- const checkBadReport = (report: number[]) => {
- const freqMap: Record<string, number[]> = {}
- const getProps = (i: number, j: number) => {
- const equality = report[i] < report[j] ? "increasing": report[i] > report[j] ? "decreasing": "equal"
- const valid = Math.abs(report[i] - report[j]) >= 1 && Math.abs(report[i] - report[j]) <= 3
- return [valid, equality] as [boolean, string]
- }
- for (let i = 0; i < report.length - 1; i++) {
- const props = JSON.stringify(getProps(i, i + 1))
- if (!(props in freqMap)) freqMap[props] = []
- freqMap[props].push(i)
- }
- if (Object.keys(freqMap).length < 2) return JSON.parse(Object.keys(freqMap)[0])[0]
- if (Object.keys(freqMap).length > 2) return false
- for (let props in freqMap) {
- if (freqMap[props].length === 1) {
- const otherProp = Object.keys(freqMap).find(k => k !== props)!
- const i = freqMap[props][0]
- const condition = (i: number) => (i <= 0 || i >= report.length - 1 || JSON.stringify(getProps(i - 1, i + 1)) === otherProp)
- if (condition(i) || condition(i + 1)) {
- return JSON.parse(otherProp)[0]
- }
- }
- }
- return false
- }
- const ans = reports.reduce<number>((ans, report) => {
- return checkBadReport(report) ? ans += 1 : ans
- }, 0);
- console.log(ans);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement