-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25_1.ps1
92 lines (54 loc) · 1.93 KB
/
day25_1.ps1
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Day 25: Full of Hot Air
# Ref: Negative bases. balanced ternary. balanced quinary.
# Two steps to convert from decimal to balanced quinary
# 1) convert from decimal to quinary
# 2) convert from quinary to balanced quinary
# -2,-1,0,1,2
##
# Part 1
##
#$data = get-content test.txt
$data = get-content input.txt
$SNAFU_to_base10list = new-object "system.collections.arraylist"
# Convert the SNAFU numbers to base 10
foreach ($num in $data) {
$digits = $num.tochararray()
[array]::Reverse($digits)
$value = 0
for ($x=0 ; $x -lt $digits.Count; $x++) {
switch ($digits[$x]) {
'=' { [int]$digit = -2 ; break}
'-' { [int]$digit = -1 ;break }
default {[int]$digit = [convert]::ToInt32($digits[$x],10) }
}
$value = $value + ([math]::Pow(5,$x) * $digit)
#"digit is : {0}. value is : {1}" -f $digit,([math]::Pow(5,$x) * $digit)
}
"Base10 of {0} is: {1}" -f $num, $value
[void]$SNAFU_to_base10list.Add($value)
}
[int64]$q = ($SNAFU_to_base10list | Measure-Object -sum).Sum
"SNAFU sum: {0}" -f $q
#Convert the sum from Base 10 back to SNAFU
#############
#$q=1747
# 1=-0-2
$final = new-object "System.Collections.Stack"
for ($pos=0; $pos -lt 25 -and $q -gt 0; $pos++) {
$remainder = $null
$newq = [math]::divrem($q,5,[ref]$remainder)
#"{0} / 5 = {1} mod {2}" -f $q, $newq, $remainder
switch ($remainder) {
# remainders 4,5 are values -1,-2. Need to carry the one to the left.
4 {[string]$digit = "-"; $newq+=1;break}
3 {[string]$digit = "="; $newq+=1;break}
2 {[string]$digit = "2";break}
1 {[string]$digit = "1";break}
0 {[string]$digit = "0";break}
}
#"{0}" -f $digit
$stack.Push($digit)
$q = $newq
}
"Base 10 to SNAFU: {0}" -f ($stack -join '')
$stack.Clear()