advent: add 2020 day1 part1 solution (partial)
This commit is contained in:
parent
4d51b47046
commit
566a90782d
35
scu-dsp/advent/2020/day1/generate_input.py
Normal file
35
scu-dsp/advent/2020/day1/generate_input.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
def has_2020_2(l):
|
||||||
|
length = len(l)
|
||||||
|
found = set()
|
||||||
|
for i in range(length):
|
||||||
|
for j in range(length):
|
||||||
|
if i == j:
|
||||||
|
continue
|
||||||
|
if l[i] + l[j] == 2020:
|
||||||
|
found.add(frozenset((i, j)))
|
||||||
|
return len(found) == 1
|
||||||
|
|
||||||
|
def has_2020_3(l):
|
||||||
|
length = len(l)
|
||||||
|
found = set()
|
||||||
|
for i in range(length):
|
||||||
|
for j in range(length):
|
||||||
|
for k in range(length):
|
||||||
|
if i == j or k == i:
|
||||||
|
continue
|
||||||
|
if l[i] + l[j] + l[k] == 2020:
|
||||||
|
found.add(frozenset((i, j)))
|
||||||
|
return len(found) == 1
|
||||||
|
|
||||||
|
def gen_random():
|
||||||
|
return [random.randint(100, 2500) for _ in range(64)]
|
||||||
|
|
||||||
|
def find_input():
|
||||||
|
while True:
|
||||||
|
l = gen_random()
|
||||||
|
if has_2020_2(l) and has_2020_3(l):
|
||||||
|
return l
|
||||||
|
|
||||||
|
print("\n".join(map(str, find_input())))
|
64
scu-dsp/advent/2020/day1/input.txt
Normal file
64
scu-dsp/advent/2020/day1/input.txt
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
2425
|
||||||
|
1176
|
||||||
|
1549
|
||||||
|
1178
|
||||||
|
1758
|
||||||
|
2293
|
||||||
|
880
|
||||||
|
1602
|
||||||
|
871
|
||||||
|
1423
|
||||||
|
347
|
||||||
|
2353
|
||||||
|
1460
|
||||||
|
2019
|
||||||
|
1767
|
||||||
|
2213
|
||||||
|
1988
|
||||||
|
193
|
||||||
|
2434
|
||||||
|
1862
|
||||||
|
2176
|
||||||
|
760
|
||||||
|
1227
|
||||||
|
1005
|
||||||
|
1143
|
||||||
|
2199
|
||||||
|
2268
|
||||||
|
927
|
||||||
|
942
|
||||||
|
240
|
||||||
|
211
|
||||||
|
1996
|
||||||
|
1479
|
||||||
|
1068
|
||||||
|
1392
|
||||||
|
1827
|
||||||
|
2285
|
||||||
|
1380
|
||||||
|
1669
|
||||||
|
1282
|
||||||
|
2182
|
||||||
|
2424
|
||||||
|
1082
|
||||||
|
1826
|
||||||
|
344
|
||||||
|
1584
|
||||||
|
1741
|
||||||
|
1614
|
||||||
|
517
|
||||||
|
907
|
||||||
|
1091
|
||||||
|
1755
|
||||||
|
1921
|
||||||
|
176
|
||||||
|
1668
|
||||||
|
2112
|
||||||
|
1485
|
||||||
|
944
|
||||||
|
1771
|
||||||
|
1105
|
||||||
|
409
|
||||||
|
1975
|
||||||
|
2333
|
||||||
|
2434
|
70
scu-dsp/advent/2020/day1/solution.asm
Normal file
70
scu-dsp/advent/2020/day1/solution.asm
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
;; input data: m0
|
||||||
|
;; input data: m1
|
||||||
|
;; input data: m2
|
||||||
|
|
||||||
|
;; variables m3:
|
||||||
|
loop_0_ix = 0 ; decrementing counter
|
||||||
|
loop_1_start = 1
|
||||||
|
|
||||||
|
;start = 0
|
||||||
|
start = 0 ; constant
|
||||||
|
|
||||||
|
;; initialization
|
||||||
|
;; [X ] [Y ] [D1 ]
|
||||||
|
mov (start + 0),ct0
|
||||||
|
|
||||||
|
;; initialize multiplier output to 2020
|
||||||
|
mov 63,ct3
|
||||||
|
mvi 2020,mc3 ; m3[63] used as temporary
|
||||||
|
mov 63,ct3
|
||||||
|
mov m3,y mov 1,rx
|
||||||
|
;; initialize variables
|
||||||
|
mov loop_0_ix,ct3
|
||||||
|
mov (64 - start),mc3 ; loop_0_ix+
|
||||||
|
mov (start + 1),mc3 ; loop_1_start+
|
||||||
|
|
||||||
|
;; outer loop init
|
||||||
|
mov loop_inner,top
|
||||||
|
mov (64 - start),lop ; (LOP: 64)
|
||||||
|
mov 1,pl
|
||||||
|
mov loop_1_start,ct3
|
||||||
|
solve_loop_0_top:
|
||||||
|
;; [X ] [Y ] [D1 ]
|
||||||
|
;; calculate inner loop starting index
|
||||||
|
;; P: 1 ; A: 64
|
||||||
|
mov m3,a mov m3,ct1 ; loop_1_start
|
||||||
|
;; pre-decrement lop; effectively a jump to loop_inner
|
||||||
|
btm ; (LOP: 63)
|
||||||
|
add mov all,mc3 ; loop_1_start+ (delay slot)
|
||||||
|
|
||||||
|
loop_inner:
|
||||||
|
mov m0,p mov m1,a
|
||||||
|
add mov mul,p mov alu,a
|
||||||
|
sub
|
||||||
|
jmp z,found
|
||||||
|
nop
|
||||||
|
|
||||||
|
btm
|
||||||
|
;; increment ct1 (A discarded)
|
||||||
|
mov mc1,a ; (delay slot)
|
||||||
|
|
||||||
|
;; increment ct0 (A discarded)
|
||||||
|
mov mc0,a
|
||||||
|
|
||||||
|
loop_exit_test:
|
||||||
|
;; [X ] [Y ] [D1 ]
|
||||||
|
mov loop_0_ix,ct3
|
||||||
|
mov m3,a mov 1,pl ; m3: loop_0_ix
|
||||||
|
sub mov alu,a mov all,mc3 ; m3: loop_0_ix+
|
||||||
|
jmp nz,solve_loop_0_top
|
||||||
|
mov all,lop ; (delay slot)
|
||||||
|
|
||||||
|
not_found:
|
||||||
|
jmp end
|
||||||
|
clr a ; (delay slot)
|
||||||
|
found:
|
||||||
|
;; [X ] [Y ] [D1 ]
|
||||||
|
mov m1,x mov m0,y clr a
|
||||||
|
mov mul,p
|
||||||
|
add mov alu,a ; (delay slot)
|
||||||
|
end:
|
20
scu-dsp/base10_to_bin.py
Normal file
20
scu-dsp/base10_to_bin.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import struct
|
||||||
|
import sys
|
||||||
|
|
||||||
|
input_file = sys.argv[1]
|
||||||
|
output_file = sys.argv[2]
|
||||||
|
|
||||||
|
with open(input_file, 'r') as f:
|
||||||
|
buf = f.read()
|
||||||
|
|
||||||
|
def parse_input(buf):
|
||||||
|
lines = buf.strip().split('\n')
|
||||||
|
for line in lines:
|
||||||
|
if not line.strip():
|
||||||
|
yield 0
|
||||||
|
else:
|
||||||
|
yield int(line)
|
||||||
|
|
||||||
|
with open(output_file, 'wb') as f:
|
||||||
|
for num in parse_input(buf):
|
||||||
|
f.write(struct.pack(">i", num))
|
Loading…
x
Reference in New Issue
Block a user