From 77b6c259c6304d9da92eb5c8ff9fe337d8cbb54a Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Fri, 9 Dec 2022 12:28:05 -0500 Subject: [PATCH] part 1 --- 2022/09/part1.js | 70 ++++++++++++++++++++++++++++++++++++++++++++++++ 2022/09/part2.js | 4 +++ 2022/09/sample | 8 ++++++ 2022/09/test.js | 40 +++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 2022/09/part1.js create mode 100644 2022/09/part2.js create mode 100644 2022/09/sample create mode 100644 2022/09/test.js diff --git a/2022/09/part1.js b/2022/09/part1.js new file mode 100644 index 0000000..3027d39 --- /dev/null +++ b/2022/09/part1.js @@ -0,0 +1,70 @@ +import * as R from "remeda"; +import Victor from "@a-robu/victor"; + +import { readFile } from "../05/part1.js"; +import { passthru } from '../08/part1.js' + +const directions = { + R: new Victor(1,0), + L: new Victor(-1,0), + U: new Victor(0,1), + D: new Victor(0,-1), +} + +const parseLine = R.createPipe( + line => line.split(" "), + ([direction,times]) => Array(parseInt(times)).fill(directions[direction]) +); + + +const readInput = (...args) => R.pipe( + readFile(...args), + text => text.split("\n"), + R.compact, + R.map( parseLine ), + R.flatten, +) + +export const puzzleInput = readInput(import.meta.url, "input"); +export const sample = readInput(import.meta.url, "sample"); + +const touching = (v1,v2) => [v1.distanceX(v2), v1.distanceY(v2)].every( + x => x*x <= 1 +); + +export function moveOnce( head, tail, d ) { + head.add(d); + + if( touching(head,tail) ) return; + + const delta = head.clone().subtract(tail).normalize(); + + if( delta.x * delta.y ) { + delta.x = delta.x > 0 ? 1 : -1; + delta.y = delta.y > 0 ? 1 : -1; + } + + tail.add(delta); +} + +const moveAround = (directives) => { + const head = new Victor(0,0); + const tail = new Victor(0,0); + + const visited = [ tail.toString() ] + + for ( const d of directives ) { + moveOnce(head,tail,d); + + visited.push( tail.toString() ); + } + + return visited; +}; + +export default R.createPipe( + moveAround, + passthru( x => console.log(x) ), + R.uniq, + R.countBy( R.identity ) +); diff --git a/2022/09/part2.js b/2022/09/part2.js new file mode 100644 index 0000000..10fc179 --- /dev/null +++ b/2022/09/part2.js @@ -0,0 +1,4 @@ +import * as R from "remeda"; + + +export default () => {}; diff --git a/2022/09/sample b/2022/09/sample new file mode 100644 index 0000000..9874df2 --- /dev/null +++ b/2022/09/sample @@ -0,0 +1,8 @@ +R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2 diff --git a/2022/09/test.js b/2022/09/test.js new file mode 100644 index 0000000..4a1df81 --- /dev/null +++ b/2022/09/test.js @@ -0,0 +1,40 @@ +import { test, expect, describe } from "vitest"; +import Victor from "@a-robu/victor"; + +const V = (...args) => new Victor(...args); + +import { expectSolution } from "../01/main.js"; +import part1, { moveOnce, sample, puzzleInput } from "./part1.js"; +import part2 from "./part2.js"; + +describe("part 1", () => { + test( "moveOnce", () => { + let head = V(1,0); + let tail = V(0,0); + moveOnce(head,tail,V(1,0)); + + expect(tail).toMatchObject({x:1,y:0}); + + moveOnce(head,tail,V(0,1)); + + expect(tail).toMatchObject({x:1,y:0}); + + moveOnce(head,tail,V(0,1)); + + expect(tail).toMatchObject({x:2,y:1}); + + } ); + + test( "sample", () => { + expect(part1(sample)).toEqual(13); + } ) + test("solution", () => { + expectSolution(part1(puzzleInput)).toEqual(6030); + }); +}); + +describe("part 2", () => { + test.todo("solution", () => { + expectSolution(part2(puzzleInput)).toEqual("TODO"); + }); +});