-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.wsf
63 lines (56 loc) · 1.32 KB
/
11.wsf
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
# Project Euler
# Problem 11: Largest product in a grid
# https://projecteuler.net/problem=11
0 ^ readi retrieve
^ call read_square_grid
^ ^ * swap
1
.loop_horiz:
^ ^2 + swap # i += n
^1 1 call line_prods # i i+n 1
^ ^3 j< .loop_horiz # i < n*n
drop
0
.loop_vert:
1+ # i++
^ ^3 ^3 call line_prods # i n*n n
^ ^2 j< .loop_vert # i < n
drop
.loop_diag:
end
# read_square_grid reads a square grid of two-digit numbers into the
# heap, starting at address 1.
# (n -- )
read_square_grid:
^ *
1 # addr
.read_square_grid_loop:
2dup j< .read_square_grid_ret
^
^ ^ readc retrieve '0' - 10*
^1 ^ readc retrieve '0' - +
^1 readc # discard ' ' or '\n'
store
1+
jmp .read_square_grid_loop
.read_square_grid_ret:
2drop ret
# (start end stride -- )
line_prods:
^2 retrieve
^3 ^2 + retrieve * # half0 = grid[0] * grid[stride]
^3 ^2 2* + # i = stride * 2
.line_prods_loop:
swap
^1 retrieve
^2 ^4 + retrieve * # half1 = grid[i] * grid[i+stride]
swap ^1 * # prod = half0 * half1
0 retrieve ^1 j< .line_prods_greater_prod
drop
.line_prods_loop_next:
swap ^2 2* + # i += stride * 2
^ ^4 j< .line_prods_loop
5drop ret
.line_prods_greater_prod:
0 swap store
jmp .line_prods_loop_next