-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC Day 25.m
39 lines (31 loc) · 1.03 KB
/
AoC Day 25.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
(* ::Package:: *)
(* ::Text:: *)
(*Written December 25th, 2022.*)
(*Import*)
day = 25;
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 = StringSplit[Import[inputPath], "\n"];
(*Part 1*)
sum = Total[
Total[5^Range[Length[#] - 1, 0, -1]*#] & /@ (toExpression[
Characters /@ input] /. {"-" -> -1, "=" -> -2})];
balancedBase[n_, b_] :=
Module[{digits = Join[{0}, IntegerDigits[n, b]], pos},
While[
Max[digits] > Floor[b/2],
pos = FirstPosition[
digits, _?(# > Floor[b/2] &)][[1]];
digits =
Join[digits[[;; pos - 2]], {digits[[pos - 1]] +
1}, {digits[[pos]] - b}, digits[[pos + 1 ;;]]]
];
If[digits[[1]] == 0, digits = digits[[2 ;;]]];
StringJoin[ToString /@ digits /. {"-1" -> "-", "-2" -> "="}]
]
balancedBase[sum, 5]