-
Notifications
You must be signed in to change notification settings - Fork 0
/
AoC Day 05.m
41 lines (31 loc) · 1.22 KB
/
AoC Day 05.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
(* ::Package:: *)
(* ::Text:: *)
(*Written December 5th, 2022.*)
(*Import*)
day = 5;
inputPath = FileNameJoin[{NotebookDirectory[], "Day" <> ToString[day] <> "Input.txt"}];
input=Import[inputPath,"List"];
toExpression[inputText_] :=
Map[
If[! IntegerQ[#] \[And]
StringMatchQ[#,
Alternatives["+", "-", ""] ~~ DigitCharacter ..],
ToExpression[#], #] &,
inputText,
{Depth[inputText] - 1, Depth[inputText]}];
(*Setup*)
cutoffLine = Position[input, _?(StringMatchQ[#, Alternatives[" ", DigitCharacter] ..] &), Heads -> False][[1, 1]];
stackCenters = StringPosition[input[[cutoffLine]], DigitCharacter][[;; , 1]];
instructions = toExpression[StringSplit[input[[cutoffLine + 2 ;;]]]];
stacks = Select[#, # != " " &] & /@
Transpose[Characters[#][[stackCenters]] & /@ input[[;; cutoffLine - 1]]];
stacks2 = stacks;
(*Parts 1 & 2*)
Do[
stacks1[[i[[6]]]] = Join[Reverse[stacks1[[i[[4]], ;; i[[2]]]]], stacks1[[i[[6]]]]];
stacks1[[i[[4]]]] = Drop[stacks1[[i[[4]]]], i[[2]]];
stacks2[[i[[6]]]] = Join[stacks2[[i[[4]], ;; i[[2]]]], stacks2[[i[[6]]]]];
stacks2[[i[[4]]]] = Drop[stacks2[[i[[4]]]], i[[2]]];
, {i, instructions}];
Print[StringJoin[stacks1[[;; , 1]]]]
Print[StringJoin[stacks2[[;;,1]]]]