|
1 | 1 | import { FileReader } from "../common";
|
2 | 2 |
|
| 3 | +class Point { |
| 4 | + x: number |
| 5 | + y: number |
| 6 | + constructor(x: number, y: number) { |
| 7 | + this.x = x |
| 8 | + this.y = y |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +class Beacon extends Point {} |
| 13 | + |
| 14 | +class Sensor extends Point {} |
| 15 | + |
| 16 | +const dist = (p1: Point, p2: Point): number => Math.abs(p1.x - p2.x) + Math.abs(p1.y - p2.y) |
| 17 | + |
3 | 18 | class Solve15 extends FileReader {
|
| 19 | + sensorBeacon: Map<Sensor, Beacon> = new Map() |
| 20 | + sensorDist: Map<Sensor, number> = new Map() |
| 21 | + minX: number = Number.MAX_SAFE_INTEGER |
| 22 | + maxX: number = Number.MIN_SAFE_INTEGER |
| 23 | + |
4 | 24 | constructor() {
|
5 | 25 | super();
|
6 | 26 | this.readData("src/15/test.data")
|
7 | 27 | .then((data) => {
|
8 |
| - this.process(data.split("\n")); |
| 28 | + this.process(data.split("\n"), 10); |
9 | 29 | })
|
10 | 30 | .catch((err) => console.log(err));
|
11 | 31 | }
|
12 | 32 |
|
13 |
| - process = (data: string[]) => { |
| 33 | + process = (data: string[], y: number) => { |
| 34 | + // Sensor at x=2, y=18: closest beacon is at x=-2, y=15 |
| 35 | + data.forEach(row => { |
| 36 | + const data = row.split(': closest beacon is at x=') |
| 37 | + const sensorData = data[0].slice(12).split(', y=') |
| 38 | + const beaconData = data[1].split(', y=') |
| 39 | + const sensor: Point = new Sensor(+sensorData[0], +sensorData[1]) |
| 40 | + const beacon: Point = new Beacon(+beaconData[0], +beaconData[1]) |
| 41 | + console.log(sensor, beacon) |
| 42 | + this.sensorBeacon.set(sensor, beacon) |
| 43 | + this.sensorDist.set(sensor, dist(sensor, beacon)) |
| 44 | + }) |
14 | 45 | };
|
15 | 46 | }
|
16 | 47 |
|
|
0 commit comments