-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC Day 15.m
49 lines (38 loc) · 1.19 KB
/
AoC Day 15.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(* ::Package:: *)
(* ::Text:: *)
(*Written December 15th, 2022.*)
(*Import*)
day = 15;
inputPath = FileNameJoin[{NotebookDirectory[], "Day" <> ToString[day] <> "Input.txt"}];
toExpression[inputText_] :=
Map[
If[! IntegerQ[#] \[And]
StringMatchQ[#,
Alternatives["+", "-", ""] ~~ DigitCharacter ..],
ToExpression[#], #] &,
inputText,
{Depth[inputText] - 1, Depth[inputText]}];
input = toExpression[
StringSplit[#, {"=", ":", ",", " "}] & /@
StringSplit[Import[inputPath], {"\n"}]];
(*Setup*)
sensors = input[[;; , {4, 7}]];
beacons = input[[;; , {14, 17}]];
(*Part 1*)
RegionMeasure@Region[IntervalUnion @@
Table[
beaconDist = ManhattanDistance[sensors[[i]], beacons[[i]]];
rowDist =
ManhattanDistance[{sensors[[i, 1]], 2000000}, sensors[[i]]];
diff = beaconDist - rowDist;
If[diff < 0, Nothing,
Interval[{sensors[[i, 1]] - diff, sensors[[i, 1]] + diff}]]
, {i, Length[input]}]]
(*Part 2*)
(x*4000000 + y) /.
FindInstance[Join[
And @@ Table[
ManhattanDistance[sensors[[i]], {x, y}] >
ManhattanDistance[sensors[[i]], beacons[[i]]]
, {i, Length[input]}] && 0 <= {x, y} <= 4000000],
{x, y}, Integers][[1]]