Skip to content

Instantly share code, notes, and snippets.

@abionics
Created April 29, 2025 17:54
Show Gist options
  • Save abionics/a6832efe0fb532a403d80853fdb87e85 to your computer and use it in GitHub Desktop.
Save abionics/a6832efe0fb532a403d80853fdb87e85 to your computer and use it in GitHub Desktop.
// https://codeforces.com/contest/2105/problem/A
// 124.35 points
message Vote {
value: Bool;
}
struct ProposalState {
yesCount: Int as uint32;
noCount: Int as uint32;
}
struct Init {
proposalId: Int as uint32;
votingEndingAt: Int as uint32;
}
asm fun magic() {
<b
<b 0xad4de08e 32 u, <b b> ref, <b
IFJMP:<{
<b
ZERO // [y]
ZERO // [y] [n]
c4 PUSH // [y] [n] [c4]
CTOS // [y] [n] [c4]
DUP // [y] [n] [c4] [c4]
SBITS // [y] [n] [c4] [c4] [bits]
65 EQINT // [y] [n] [c4] [c4] [inited]
IFRET // [y] [n] [c4]
32 PUSHINT // [y] [n] [c4] [32]
SDSKIPFIRST // [y] [n] [c4]
101 PLDU // [y] [n] [votes]
AGAIN:<{
// [y] [n] [votes]
s0 s0 PUSH2 // [y] [n] [votes] [votes] [votes]
1 GTINT // [y] [n] [votes] [votes] [votes>1]
IFNOTRETALT // [y] [n] [votes] [votes]
1 MODPOW2# // [y] [n] [votes] [v]
// not optimal, but it is a getter
IF:<{
// [y] [n] [votes]
s2 XCHG0 // [votes] [n] [y]
INC // [votes] [n] [y]
s2 XCHG0 // [y] [n] [votes]
}>ELSE<{
// [y] [n] [votes]
s1 XCHG0 // [y] [votes] [n]
INC // [y] [votes] [n]
s1 XCHG0 // [y] [n] [votes]
}>
1 RSHIFT# // [y] [n] [votes]
}>
b> ref,
}>
32 LDU // ...2 [msg] [mid] [body]
1 PLDU // ...2 [msg] [mid] [v]
c4 PUSH // ...2 [msg] [mid] [v] [c4]
CTOS // ...2 [msg] [mid] [v] [c4]
DUP // ...2 [msg] [mid] [v] [c4] [c4]
SBITS // ...2 [msg] [mid] [v] [c4] [c4] [bits]
65 NEQINT // ...2 [msg] [mid] [v] [c4] [c4] [inited]
IFNOT:<{
// ...2 [msg] [mid] [v] [c4]
65 PLDU // ...2 [msg] [mid] [v] [endTime] // load all 33 + 32
32 MODPOW2# // ...2 [msg] [mid] [v] [endTime] // leave last 32 bits
ONE // ...2 [msg] [mid] [v] [endTime] [votes]
ZERO // ...2 [msg] [mid] [v] [endTime] [votes] [prev]
}>ELSE<{
// ...2 [msg] [mid] [v] [c4]
32 LDU // ...2 [msg] [mid] [v] [endTime] [c4]
101 LDU // ...2 [msg] [mid] [v] [endTime] [votes] [c4]
256 PLDU // ...2 [msg] [mid] [v] [endTime] [votes] [prev]
s1 PUSH // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [votes]
100 PUSHPOW2 // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [votes] [2^100]
GREATER // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [votes>2^100]
2 THROWIF // ...2 [msg] [mid] [v] [endTime] [votes] [prev]
}>
s5 s2 PUSH2 // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [msg] [endTime]
NOW // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [msg] [endTime] [now]
LESS // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [msg] [now<endTime]
2 THROWIF // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [msg]
CTOS // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [msg]
15 PUSHINT // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [msg] [15] // info(4) std(3) wc(8)
SDSKIPFIRST // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [msg]
256 PLDU // ...2 [msg] [mid] [v] [endTime] [votes] [prev] [sender]
TUCK // ...2 [msg] [mid] [v] [endTime] [votes] [sender] [prev] [sender]
EQUAL // ...2 [msg] [mid] [v] [endTime] [votes] [sender] [sender==prev]
2 THROWIF // ...2 [msg] [mid] [v] [endTime] [votes] [sender]
// cashback to voter
<b 0xec3c86d42 40 u, <b b> ref, <b
1618320452599917072350981470401719315449936156661169887237 192 u,
1151069869968201124468582971260209755865574645523101515776 192 u,
b> ref, b> PUSHREF
c5 POP
// save // ...2 [msg] [mid] [v] [endTime] [votes] [sender]
s2 XCHG0 // ...2 [msg] [mid] [v] [sender] [votes] [endTime]
NEWC // ...2 [msg] [mid] [v] [sender] [votes] [endTime] [c4]
32 STU // ...2 [msg] [mid] [v] [sender] [votes] [c4.endTime]
s3 s1 PUXC // ...2 [msg] [mid] [v] [sender] [c4.endTime] [v] [votes]
1 LSHIFT# // ...2 [msg] [mid] [v] [sender] [c4.endTime] [v] [votes.0]
ADD // ...2 [msg] [mid] [v] [sender] [c4.endTime] [votes.v]
SWAP // ...2 [msg] [mid] [v] [sender] [votes.v] [c4.endTime]
101 STU // ...2 [msg] [mid] [v] [sender] [c4.endTime.(votes.v)]
256 STU // ...2 [msg] [mid] [v] [c4.endTime.(votes.v).sender]
ENDC // ...2 [msg] [mid] [v] [c4]
c4 POP // ...2 [msg] [mid] [v]
b> ref, b> PUSHREF
c5 POP
b>
dup hashB B>X $>B "hex" B>file
boc>B B>base64 $>B "boc" B>file
bye
}
contract Proposal {
init(data: Init) {}
receive() {}
receive(msg: Vote) {
magic();
}
get fun proposalState(): ProposalState {
return ProposalState{yesCount: 0, noCount: 0};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment