Created
September 17, 2023 08:50
-
-
Save ilsubyeega/10a8f1a407f3d801f7993cce433b9ed9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function range(start, end) { | |
| return Array.from({ length: end - start + 1 }, (_, i) => start + i); | |
| } | |
| function checkOverlap(square1, square2) { | |
| const [x1, y1, r1] = square1; | |
| const [x2, y2, r2] = square2; | |
| const size1 = r1 * 2 + 1; | |
| const size2 = r2 * 2 + 1; | |
| return Math.abs(x1 - x2) < (size1 + size2) / 2 && Math.abs(y1 - y2) < (size1 + size2) / 2; | |
| } | |
| function findOverlappingSquares(squares) { | |
| const groups = []; | |
| const remainingSquares = [...squares]; | |
| while (remainingSquares.length > 0) { | |
| const currentSquare = remainingSquares.shift(); | |
| const group = [currentSquare]; | |
| for (let i = 0; i < remainingSquares.length;) { | |
| if (checkOverlap(currentSquare, remainingSquares[i])) { | |
| group.push(remainingSquares.splice(i, 1)[0]); | |
| } else { | |
| i++; | |
| } | |
| } | |
| groups.push(group); | |
| } | |
| return groups; | |
| } | |
| function solution(n, m, tests, actual) { | |
| n += 1; | |
| m += 1; | |
| let done_tests = tests.filter(a => a[3] === 1).map(a => [a[0], a[1], a[2]]); | |
| if (done_tests.length === 0) { | |
| done_tests.push([0, 0, 999999999]) // Making sure if the done_test does not exist, create a full area. | |
| } | |
| let fail_tests = tests.filter(a => a[3] === 0).map(a => [a[0], a[1], a[2]]); | |
| console.log("Splitted test"); | |
| let overlapped = findOverlappingSquares(done_tests); | |
| console.log("Found overlapping") | |
| let items = []; | |
| overlapped.forEach(group => { | |
| let fails = []; | |
| group.forEach(rect => { | |
| const res = fail_tests.filter(fail => checkOverlap(rect, fail)); | |
| if (res) fails.push(...res); | |
| }) | |
| items.push({ | |
| group: group, | |
| fails: fails | |
| }) | |
| }); | |
| console.log("Found overlapping with fail") | |
| let area = 0; | |
| items.forEach(item => { | |
| console.log("loop item:", item) | |
| let group = item.group; | |
| let fails = item.fails; | |
| const or_filterable = group.map(rect => { | |
| let [x, y, r] = rect; | |
| return ((x1, y1) => { | |
| if (x1 == x && y1 == y) return false; | |
| if (x1 >= n || y1 >= m) return false; | |
| return Math.abs(x1 - x) + Math.abs(y1 - y) <= r; | |
| }) | |
| }) | |
| const and_filterable = fails.map(rect => { | |
| let [x, y, r] = rect; | |
| return ((x1, y1) => { | |
| if (x1 == x && y1 == y) return false; | |
| if (x1 >= n || y1 >= m) return false; | |
| return Math.abs(x1 - x) + Math.abs(y1 - y) <= r; | |
| }) | |
| }) | |
| let left_x = Math.min(Math.max(Math.min(...group.map(rect => rect[0] - rect[2])), 0), n); | |
| let right_x = Math.max(Math.min(Math.max(...group.map(rect => rect[0] + rect[2])), n), 0); | |
| let top_y = Math.max(Math.min(Math.max(...group.map(rect => rect[1] - rect[2])), m), 0); | |
| let bottom_y = Math.min(Math.max(Math.min(...group.map(rect => rect[1] + rect[2])), 0), m); | |
| let or = 0; | |
| let and = 0; | |
| range(left_x, right_x).forEach(x => { | |
| range(top_y, bottom_y).forEach(y => { | |
| const or_filter = or_filterable.some(f => f(x, y)); | |
| const and_filter = and_filterable.some(f => f(x, y)); | |
| if (or_filter) or++; | |
| if (and_filter) and++; | |
| if (or_filter && !and_filter) { | |
| area++; | |
| } | |
| }) | |
| }) | |
| }) | |
| console.log("RESULT", area, actual) | |
| } | |
| solution(3, 5, [[2, 3, 2, 1], [1, 0, 4, 0], [0, 4, 1, 0]], 4); | |
| solution(99999, 99999, [[0, 0, 199997, 1]], 9999999999); | |
| solution(99999, 99999, [[50000, 50000, 3, 0]], 9999999975); | |
| solution(300, 100, [[123, 28, 124, 1], [183, 22, 34, 0], [188, 81, 116, 1], [167, 53, 33, 0], [125, 55, 20, 0]], 6535); | |
| solution(99999, 99999, [[0, 0, 199997, 1]], 9999999999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment