aboutsummaryrefslogtreecommitdiffstats
path: root/day5/index.ts
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2020-12-05 13:28:36 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2020-12-05 13:28:36 +0200
commitea972bbc868c9fbfada2acb6b4e6cdaebcc6e24c (patch)
treec816b9c260990080d84093b242400c6375b97772 /day5/index.ts
parent2cbddad19a7207d905a30938d08d147ebb283921 (diff)
Solve 5.2
Diffstat (limited to 'day5/index.ts')
-rw-r--r--day5/index.ts44
1 files changed, 38 insertions, 6 deletions
diff --git a/day5/index.ts b/day5/index.ts
index 1cf4a3c..bb02d77 100644
--- a/day5/index.ts
+++ b/day5/index.ts
@@ -1,5 +1,5 @@
import { Observable, of } from "rxjs";
-import { concatAll, count, filter, groupBy, map, max, mergeMap, reduce, tap } from 'rxjs/operators';
+import { concatAll, count, filter, groupBy, map, max, mergeMap, reduce, scan, tap, toArray } from 'rxjs/operators';
import { ExerciseModuleFunc } from "../types";
type Pair = [number, number];
@@ -10,8 +10,6 @@ const getColumnAndRow = (line: string): Pair => {
lo = 0; hi = 127;
for (let i = 0; i < 7; i += 1) {
- console.log("i:",i,"lo:",lo,"hi:",hi);
-
const chr = chars[i];
const diff = hi - lo;
if (chr === "F") {
@@ -26,8 +24,6 @@ const getColumnAndRow = (line: string): Pair => {
lo = 0; hi = 7;
for (let i = 0; i < 3; i += 1) {
- console.log("i:",i,"lo:",lo,"hi:",hi);
-
const chr = chars[i + 7];
const diff = hi - lo;
if (chr === "L") {
@@ -42,6 +38,31 @@ const getColumnAndRow = (line: string): Pair => {
return [col, row];
}
+const findVacantSeats = (lst: Pair[]): Pair[] => {
+ const seatMap: boolean[][] = Array(128);
+ for (let i = 0; i < 127; i += 1) {
+ seatMap[i] = Array(8);
+ for (let j = 0; j < 7; j += 1) {
+ seatMap[i][j] = true;
+ }
+ }
+
+ for (let seat of lst) {
+ seatMap[seat[1]][seat[0]] = false;
+ }
+
+ const vacants: Pair[] = [];
+ for (let row = 0; row < 127; row += 1) {
+ for (let col = 0; col < 7; col += 1) {
+ if (seatMap[row][col] === true) {
+ vacants.push([col, row]);
+ }
+ }
+ }
+
+ return vacants;
+}
+
const calcID = (pair: Pair) => pair[1] * 8 + pair[0];
const day5: ExerciseModuleFunc = async (input: string) => {
@@ -53,7 +74,18 @@ const day5: ExerciseModuleFunc = async (input: string) => {
max()
).toPromise();
- return Promise.all([prom1]);
+ const prom2 = of(input).pipe(
+ map(l => l.split("\n")),
+ concatAll(),
+ map(getColumnAndRow),
+ toArray(),
+ map(findVacantSeats),
+ concatAll(),
+ map(calcID),
+ toArray()
+ ).toPromise();
+
+ return Promise.all([prom1, prom2]);
}
export default day5;