From 983000000fdd92cee5029c2c2f94a607c9c31389 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Thu, 3 Dec 2020 10:29:14 +0200 Subject: Solve 3.2 --- day3/index.ts | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/day3/index.ts b/day3/index.ts index 6dd8168..3f40f60 100644 --- a/day3/index.ts +++ b/day3/index.ts @@ -1,9 +1,22 @@ import { Observable, of } from "rxjs"; -import { concatAll, count, filter, map, take, tap } from 'rxjs/operators'; +import { concatAll, count, filter, groupBy, map, mergeMap, reduce, take, tap } from 'rxjs/operators'; import { ExerciseModuleFunc } from "../types"; type Coords = [number, number]; +const SLOPES: Coords[] = [ + [1, 1], + [3, 1], + [5, 1], + [7, 1], + [1, 2] +]; + +interface Observation { + slope: Coords; + coords: Coords; +} + const day3: ExerciseModuleFunc = async (input: string) => { const lines = input.split("\n"); const my = lines.length; @@ -11,18 +24,23 @@ const day3: ExerciseModuleFunc = async (input: string) => { const isTree = (x: number, y: number): boolean => lines[y][x % mx] === "#"; - const indexObs = new Observable(sub => { - for (let y = 0, x = 0; y < my; y += 1, x += 3) { - if (isTree(x, y)) { - sub.next([x, y]); + const indexObs = new Observable(sub => { + for (const slope of SLOPES) { + for (let y = 0, x = 0; y < my; y += slope[1], x += slope[0]) { + if (isTree(x, y)) { + sub.next({ slope, coords: [x, y] }); + } } } + sub.complete(); }) const prom1 = indexObs.pipe( + groupBy(obs => obs.slope), + mergeMap((grp$) => grp$.pipe(count())), tap(console.log), - count() + reduce((acc, val) => acc * val) ).toPromise(); return Promise.all([prom1]); -- cgit v1.3