-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC Day 18.m
55 lines (43 loc) · 1.4 KB
/
AoC Day 18.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
50
51
52
53
54
55
(* ::Package:: *)
(* ::Text:: *)
(*Written December 18th, 2022.*)
(*Import*)
day = 18;
inputPath = FileNameJoin[{NotebookDirectory[], "Day" <> ToString[day] <> "Input.txt"}];
toExpression[inputText_] :=
Map[
If[! IntegerQ[#] &&
StringMatchQ[#,
Alternatives["+", "-", ""] ~~ DigitCharacter ..],
ToExpression[#], #] &,
inputText,
{Depth[inputText] - 1, Depth[inputText]}];
input = toExpression[
StringSplit[#, Alternatives[",", " ", ":"]] & /@
StringSplit[Import[inputPath], "\n"]];
(*Setup*)
dirs[cube_] := {cube + {1, 0, 0}, cube + {-1, 0, 0}, cube + {0, 1, 0},
cube + {0, -1, 0}, cube + {0, 0, 1}, cube + {0, 0, -1}};
surfaces = dirs /@ input;
ClearAll@cube; cube[pos_] := False;
(cube[#] = True) & /@ input;
(*Part 1*)
DeleteDuplicates[{#[[1]] - {1, 0, 0}, Count[#, _?(! cube[#] &)]} & /@
surfaces][[;; , -1]] // Total
(*Part 2*)
lims = {#[[1]] - {1, 1, 1}, #[[2]] + {1, 1, 1}} &@
Transpose[MinMax /@ Transpose[input]];
outside = {lims[[1]]};
touching = {};
ClearAll@seen; seen[cube_] := False;
While[
Length[outside] >= 1,
(seen[#] = True) & /@ outside;
touching =
Join[touching, Select[outside, MemberQ[cube /@ dirs[#], True] &]];
outside = DeleteDuplicates@Select[
Flatten[dirs /@ outside, 1],
!cube[#] && !seen[#] && !MemberQ[Thread[lims[[1]] <= # <= lims[[2]]], False] &
];
];
Length[Intersection[#, touching]] & /@ surfaces // Total