Non-working day 1 part 1 implementation

main
Ashelyn Dawn 1 year ago
parent 06c5da09a6
commit c202f8f185

@ -1,7 +1,198 @@
(module
(memory (export "memory") 10)
(func (export "run")
(global $readLoc (mut i32) (i32.const 0))
(global $inputEnd (mut i32) (i32.const 0))
(global $outputEnd (mut i32) (i32.const 0))
(global $currentMaxCals (mut i32) (i32.const 0))
(func (export "run") (result i32)
(local $currentElf i32)
;; Read nput length
i32.const 0
i32.load
;; Store input end byte
i32.const 4
i32.add
global.set $inputEnd
;; Set up start read location
i32.const 4 ;; first memory address after length u32
global.set $readLoc
i32.const 0
global.set $currentMaxCals
loop $readLoop
global.get $currentMaxCals
call $readElf
local.tee $currentElf
i32.lt_u
if
local.get $currentElf
global.set $currentMaxCals
end
global.get $readLoc
global.get $inputEnd
i32.lt_s
br_if $readLoop
end $readLoop
;; set up output end pointer
global.get $inputEnd
i32.const 4
i32.add
global.set $outputEnd
;; write the number
global.get $currentMaxCals
call $writeNum
;; get the length to return
global.get $inputEnd
global.get $outputEnd
global.get $inputEnd
i32.const 4
i32.add
i32.sub
i32.store
;; return location of length
global.get $inputEnd
)
(func $readElf (result i32)
(local $currentSum i32)
(local $mostRecent i32)
loop $readLoop
;; read next number in file
call $readNumber
local.tee $mostRecent
;; if the number was 0
i32.eqz
if
;; return sum
local.get $currentSum
return
end
;; else add number to sum and continue
local.get $currentSum
local.get $mostRecent
i32.add
local.set $currentSum
br $readLoop
end
i32.const -1
)
(func $readNumber (result i32)
(local $number i32)
(local $currentDigit i32)
i32.const 0
local.set $number
loop $readLoop
call $readDigit
local.tee $currentDigit
;; check if less than 0
i32.const 0
i32.lt_s
if
local.get $number
return
end
;; shift number left
local.get $number
i32.const 10
i32.mul
;; add digit
local.get $currentDigit
i32.add
local.set $number
br $readLoop
end
i32.const 0
)
(func $readDigit (result i32)
(local $charcode i32)
global.get $readLoc
i32.load8_u
local.tee $charcode
;; increment read location
global.get $readLoc
i32.const 1
i32.add
global.set $readLoc
;; check if out of range (lower)
i32.const 48
i32.lt_u
if
i32.const -1
return
end
;; check if out of range (upper)
local.get $charcode
i32.const 57
i32.gt_u
if
i32.const -1
return
end
local.get $charcode
i32.const 48
i32.sub
)
(func $writeNum (param $number i32)
loop $digitLoop
local.get $number
i32.const 10
i32.rem_u
call $writeDigit
local.get $number
i32.const 10
i32.div_u
local.tee $number
i32.eqz
if
return
end
br $digitLoop
end
)
(func $writeDigit (param $digit i32)
global.get $outputEnd
local.get $digit
i32.const 48
i32.add
i32.store
global.get $outputEnd
i32.const 1
i32.add
global.set $outputEnd
)
)

@ -19,6 +19,8 @@ async function getDayList() {
}
}
console.clear()
for (const day of days) {
const option = document.createElement('option')
option.value = day
@ -53,6 +55,7 @@ async function loadDay(dayNum) {
} catch (err) {
output.className = 'error'
output.innerText = 'Error: ' + err.message
console.error(err)
return
}
@ -90,7 +93,7 @@ function runLoaded() {
// Read the output
const outputBuffer = wasmBuffer.slice(outputLocation)
const outputLargeArray = new Uint32Array(outputBuffer)
const outputLargeArray = new Uint32Array(outputBuffer.slice(0, 4))
const outputSmallArray = new Uint8Array(outputBuffer)
const outputLength = outputLargeArray[0]

Loading…
Cancel
Save