Day 1 part 2

main
Ashelyn Dawn 1 year ago
parent a46877582a
commit 6de0e6f11b

@ -5,11 +5,10 @@
(global $inputEnd (mut i32) (i32.const 0))
(global $outputEnd (mut i32) (i32.const 0))
(global $currentMaxCals (mut i32) (i32.const 0))
(func (export "part1") (result i32)
(local $currentMaxCals i32)
(local $currentElf i32)
;; Read nput length
;; Read input length
i32.const 0
i32.load
@ -23,16 +22,16 @@
global.set $readLoc
i32.const 0
global.set $currentMaxCals
local.set $currentMaxCals
loop $readLoop
global.get $currentMaxCals
local.get $currentMaxCals
call $readElf
local.tee $currentElf
i32.lt_u
if
local.get $currentElf
global.set $currentMaxCals
local.set $currentMaxCals
end
global.get $readLoc
@ -48,7 +47,118 @@
global.set $outputEnd
;; write the number
global.get $currentMaxCals
local.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 (export "part2") (result i32)
(local $currentElf i32)
(local $maxCals1 i32)
(local $maxCals2 i32)
(local $maxCals3 i32)
;; Read input 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
local.set $maxCals1
i32.const 0
local.set $maxCals2
i32.const 0
local.set $maxCals3
loop $readLoop
call $readElf
local.set $currentElf
block $comparisons
;; compare with stored value 1 (highest)
local.get $maxCals1
local.get $currentElf
i32.lt_u
if
;; pick up new max 3 (in order)
local.get $maxCals2
local.get $maxCals1
local.get $currentElf
local.set $maxCals1
local.set $maxCals2
local.set $maxCals3
br $comparisons
end
;; compare with stored value 2
local.get $maxCals2
local.get $currentElf
i32.lt_u
if
;; pick up new second and third max (in order)
local.get $maxCals2
local.get $currentElf
local.set $maxCals2
local.set $maxCals3
br $comparisons
end
;; compare with stored value 3
local.get $maxCals3
local.get $currentElf
i32.lt_u
if
local.get $currentElf
local.set $maxCals3
br $comparisons
end
end ;; $comparisons
global.get $readLoc
global.get $inputEnd
i32.lt_s
br_if $readLoop
end ;; $readLoop
;; add up top 3
local.get $maxCals1
local.get $maxCals2
local.get $maxCals3
i32.add
i32.add
local.set $maxCals1
;; set up output end pointer
global.get $inputEnd
i32.const 4
i32.add
global.set $outputEnd
;; write the number
local.get $maxCals1
call $writeNum
;; get the length to return

Loading…
Cancel
Save