-
Notifications
You must be signed in to change notification settings - Fork 0
/
charges.html
220 lines (186 loc) · 6.1 KB
/
charges.html
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<html><head>
<script>
var array = [[0n, 0n, 0n, 0n],[0n, 0n, 0n, 0n],[0n, 0n, 0n, 0n],[0n, 0n, 0n, 0n]]
var names = ["Daisy","George","James","Leo"]
var charges = []
var warned = false
function topounds(n_) {
n = BigInt(n_)
pennies = n%100n
if (pennies<10n) return "£" + (n/100n) + ".0" + pennies
else return "£" + (n/100n) + "." + pennies
}
function toint(js) { // array of indexed names to bigint
let out = 0n
for (i=0; i<js.length; i++) {
out += 2n**BigInt(js[i])
}
return out
}
function fromint(n) { // returns array of indexed names
let l = names.length
let out = []
for (i=0; i<l; i++) {
if (2n**BigInt(i) & n) out.push(i.toString())
}
return out
}
function ltostring(js) { // list of names to string
if (js.length == 0) return ""
else if (js.length == 1) return names[js[0]]
else if (js.length == 2) return names[js[0]] + " and " + names[js[1]]
else return names[js[0]] + ", " + ltostring(js.slice(1))
}
function printout() { // prints who owes who
var out = ""
for (i=0; i<names.length; i++) {
for (j=0; j<i; j++) {
if (array[i][j]>0n) out += names[j] + " owes " + names[i] + " " + topounds(array[i][j]) + "</br>"
if (array[i][j]<0n) out += names[i] + " owes " + names[j] + " " + topounds(-array[i][j]) + "</br>"
}
}
return out
}
function charge(i, js, amount, uncharge=false) {
if (js.length==0) {
window.alert("error: cannot divide by 0 cunts")
return
}
let n = BigInt(amount)/BigInt(js.length)
if (BigInt(amount)<=0 && !uncharge) {
window.alert("error: non-positive charged will fuck this machine up, if you fucked up a previous expense delete it and redo it")
return
}
if (n<=0 && !uncharge && !warned) {
window.alert("warning: as number of beneficiaries exceeds number of pennies, no adjustment to dues can be made for this transaction. this action is allowed for logging purposes only and to maintain bijectivity of keys. this wanrning shall not be displayed again")
warned = true
}
document.getElementById("genkey").innerHTML = ""
for (j of js) { // don't have to cast because lol
if (i==j) continue
else if (i>j) array[i][j] += n
else array[j][i] -= n
}
document.getElementById("data").innerHTML = printout()
if (!uncharge) {
charges.push([amount, i, toint(js)])
document.getElementById("logs").innerHTML = "<div id='log" + (charges.length-1) + "'>" + names[i] + " spent " + topounds(amount) + " benefiting " + ltostring(js) + ". <a href='javascript:uncharge(" + (charges.length-1) + ")'>Delete</a> </div>" + document.getElementById("logs").innerHTML
}
}
function uncharge(i) { // sets charge record to 0, will be ignored when generating export key
document.getElementById("log" + i).innerHTML = ""
charge(charges[i][1], fromint(charges[i][2]), -charges[i][0], true)
charges[i][0] = 0n
}
function sqrt(n, seed = 1n) {
if (n==0n) return 0n
var x = (seed + n/seed)/2n
if (x==seed || x==seed+1n) {
return seed
}
else return sqrt(n,x)
}
function pair(x,y) { // (x,y) => (x+y)(x+y+1)/2+y
let m = (x+y)
return (m+1n)*m/2n+y
}
function unpair(z) { // (x+y)(x+y+1)/2n+y => (x,y)
let m = (sqrt(8n*z+1n)-1n)/2n
let t = (m*m+m)/2n
let y = z-t
let x = m-y
return [x,y]
}
function chargenum(amount, i, js) { //(amount-1)*15*4+(beneficiaries-1)*4+charger
var out = amount-1n
out = out*15n+js-1n
out = out*4n+i
return out
}
function unchargenum(n_) {
var n = n_
var i = n%4n
n /= 4n
var js = n%15n+1n
n /= 15n
var amount = n+1n
return [amount, i, js]
}
function pairn(xs) { // pair(xs.length-1,xs)+1
if (xs.length==0n) return 0n
else return shitpair(BigInt(xs.length)-1n, pairs(xs))+1n
}
function pairs(xs) {
if (xs.length==1) return xs[0]
else return pair(pairs(xs.slice(0,Math.floor(xs.length/2))),pairs(xs.slice(Math.floor(xs.length/2))))
}
function unpairn(xs) {
if (xs==0n) return []
else {
var [n, paired] = shitunpair(xs-1n)
return unpairs(paired, n+1n)
}
}
function unpairs(xs,n) {
if (n==1n) return [xs]
else {
var [x1, x2] = unpair(xs)
return unpairs(x1, n/2n).concat(unpairs(x2, n-n/2n))
}
}
function genkey() {
var outs = []
for (c of charges) {
if (c[0] == 0n) continue
outs.push(chargenum(c[0],c[1],c[2]))
}
document.getElementById("genkey").innerHTML = "Key: " + pairn(outs)
}
function loadkey(n) {
document.getElementById("data").innerHTML = ""
document.getElementById("logs").innerHTML = ""
array = [[0n, 0n, 0n, 0n],[0n, 0n, 0n, 0n],[0n, 0n, 0n, 0n],[0n, 0n, 0n, 0n]]
charges = []
var tc = unpairn(n)
for (c_ of tc) {
c = unchargenum(c_)
charge(c[1], fromint(c[2]), c[0])
}
}
function shitpair(n_,m) { // m ++ 0 ++ 1^n
var n = n_
var out = 2n*m
while (n>0n) {
out *= 2n
out += 1n
n -= 1n
}
return out
}
function shitunpair(z) {
var n = 0n
var m = z
while (m%2n==1n) {
n += 1n
m /= 2n
}
return [n,m/2n]
}
</script>
</head>
<body>
<input type="text" id="keyin"></input> <a href="javascript:loadkey(BigInt(document.getElementById('keyin').value))">Import key</a>.
I am <select id="namepayer"><option value="0">Daisy</option><option value="1">George</option><option value="2">James</option><option value="3">Leo</option><select> and I spent <input type="text" id="amount"></input>p, benefiting <input type="checkbox" id="0"></input><label for="0">Daisy</label><input type="checkbox" id="1"></input><label for="0">George</label><input type="checkbox" id="2"></input><label for="0">James</label><input type="checkbox" id="3"></input><label for="3">Leo</label>. <a href="javascript: var bens=[]; for (i=0; i<4; i++) if (document.getElementById(i).checked) bens.push(i); charge(BigInt(document.getElementById('namepayer').value), bens, BigInt(document.getElementById('amount').value))">Add expense</a>. <a href="javascript:genkey()">Export key</a> <div id="genkey"></div>
</br>
</br>
WARNING: this tool does NOT divide pence.
</br>
</br>
<div id="data"></div>
</br>
</br>
Transactions:
</br>
<div id="logs"></div>
</body>
</html>