Last active
December 1, 2020 22:47
-
-
Save neuro-sys/81628d981b229ae576976ae88538ff0b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\ Advent of Code 2020 day 1.0 | |
include reader.fs | |
: 2020? + 2020 = ; \ true if sum is equal to 2020 | |
: find-pair-j ( uaddr1 uaddr2 -- uaddr3 uaddr4 t ) | |
begin | |
dup list->end? invert | |
while | |
over list->data \ uaddr1 uaddr2 u1 | |
over list->data \ uaddr1 uaddr2 u1 u2 | |
2020? if true exit then | |
list->next | |
repeat | |
drop | |
false | |
; | |
: find-pair ( uaddr -- u1 u2 ) | |
begin | |
dup list->end? invert | |
while | |
dup find-pair-j if true exit then | |
list->next | |
repeat | |
drop | |
false | |
; | |
: answer ( uaddr -- ) | |
find-pair | |
if | |
list->data swap list->data * . | |
else | |
." No result" | |
then | |
; | |
variable head | |
variable tail | |
s" input.txt" load-list | |
tail ! | |
head ! | |
tail @ answer | |
bye | |
\ This version exits from nested loop by a control in every level |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\ linked list | |
\ | |
\ link data | |
\ +-------+-------+ | |
\ 100 | 102 | 42 | 0 42 list-append 100 | |
\ +-------+-------+ | |
\ 102 | 104 | 666 | 666 list-append 102 | |
\ +-------+-------+ | |
\ 104 | 0 | 1337 | <- head 1337 list-append 104 | |
\ +-------+-------+ | |
\ link node offsets and size | |
0 | |
dup constant link 1 cells + | |
dup constant data 1 cells + | |
constant node | |
: list->next link + @ ; | |
: list->data data + @ ; | |
: list->end? link + 0= ; | |
\ allocate new node and set data to u | |
: list-new ( u -- addr ) | |
node allocate throw \ u addr ; allocate | |
dup node erase \ u addr ; erase | |
2dup data + ! \ u addr ; set data | |
nip \ addr | |
; | |
\ allocate new node with value u and append to head | |
\ leave new head at stack | |
: list-append ( addr1 u -- addr2 ) | |
dup list-new \ addr1 u addr2 ; allocate new node with value 0 | |
rot \ u addr2 addr1 | |
dup 0<> if | |
2dup ( link + ) ! \ u addr2 addr1 ; addr1->link = addr2 | |
then | |
drop \ u addr2 | |
dup rot \ addr2 addr2 u | |
swap \ addr2 u addr2 | |
data + ! \ addr2 ; addr2->data = 0 | |
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include list.fs | |
256 constant fdbufn | |
0 value fd1 \ File descriptor | |
variable fdbuf fdbufn allot \ Line buffer for text file | |
\ Loads file into a linked list | |
: load-list ( caddr u -- head tail ) | |
r/o open-file throw to fd1 | |
0 | |
begin | |
fdbuf fdbufn fd1 read-line throw | |
while | |
fdbuf swap \ addr1 caddr u | |
evaluate \ addr1 u | |
over 0= if | |
list-append | |
dup >r \ save tail in r-stack | |
else | |
list-append | |
then | |
repeat | |
drop \ drop 0 from read-line | |
r> \ head tail | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment