aboutsummaryrefslogtreecommitdiffstats
path: root/day3
diff options
context:
space:
mode:
Diffstat (limited to 'day3')
-rw-r--r--day3/index.ts30
1 files 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<Coords>(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<Observation>(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]);