52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { groupBy } from '../../utils/array.ts';
|
|
|
|
export function solveFirst(input: string): number {
|
|
const waypoints = 'roygbv';
|
|
const instr = input.split('');
|
|
|
|
let position = 0;
|
|
let distance = 0;
|
|
|
|
for (let i of instr) {
|
|
const offset = waypoints.indexOf(i);
|
|
if (offset > position) {
|
|
distance += offset - position;
|
|
} else {
|
|
distance += 6 - position + offset;
|
|
}
|
|
// console.log(distance);
|
|
position = offset;
|
|
}
|
|
|
|
// console.log(instr.map((i) => waypoints.indexOf(i)));
|
|
|
|
return distance;
|
|
}
|
|
|
|
export function solveSecond(input: string): number {
|
|
const waypoints = 'oygbvr';
|
|
const instr = input.split('');
|
|
const groups = groupBy(instr, (e) => e).toSorted(
|
|
(a, b) => waypoints.indexOf(b.key) - waypoints.indexOf(a.key)
|
|
);
|
|
|
|
let position = 0;
|
|
let distance = 0;
|
|
const waypoints2 = 'roygbv';
|
|
for (let g of groups) {
|
|
const offset = waypoints2.indexOf(g.key);
|
|
if (offset > position) {
|
|
distance += offset - position;
|
|
} else {
|
|
distance += 6 - position + offset;
|
|
}
|
|
distance += 6 * (g.elements.length - 1);
|
|
position = offset;
|
|
}
|
|
return distance;
|
|
|
|
// const newInstr = groups.map((g) => g.elements.join('')).join('');
|
|
//
|
|
// return solveFirst(newInstr);
|
|
}
|