@@ -0,0 +1,7 @@
|
|||||||
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="2026-02" type="BunRunConfiguration">
|
||||||
|
<option name="program" value="2026/02/run.ts" />
|
||||||
|
<option name="workingDirectory" value="$PROJECT_DIR$" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
|
describe('2026-02', () => {
|
||||||
|
it('first', () => {
|
||||||
|
expect(solveFirst(`obbvb`)).toBe(16);
|
||||||
|
expect(solveFirst(`roygbv`)).toBe(11);
|
||||||
|
expect(solveFirst(`vbgyor`)).toBe(30);
|
||||||
|
expect(solveFirst(`vvvvvv`)).toBe(35);
|
||||||
|
});
|
||||||
|
it('second', () => {
|
||||||
|
expect(solveSecond(`rybryb`)).toBe(32);
|
||||||
|
expect(solveSecond(`vrvbvg`)).toBe(33);
|
||||||
|
expect(solveSecond(`groovyyoyo`)).toBe(55);
|
||||||
|
expect(solveSecond(`roygbvroygbvroygbv`)).toBe(103);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { join } from 'path';
|
||||||
|
import { solveFirst, solveSecond } from './index.ts';
|
||||||
|
|
||||||
|
const input = await Bun.file(join(__dirname, 'input.txt')).text();
|
||||||
|
|
||||||
|
const firstAnswer = solveFirst(input);
|
||||||
|
|
||||||
|
console.log(firstAnswer);
|
||||||
|
|
||||||
|
const secondAnswer = solveSecond(input);
|
||||||
|
|
||||||
|
console.log(secondAnswer);
|
||||||
Reference in New Issue
Block a user