Created
December 1, 2022 06:17
-
-
Save Skyb0rg007/732c069dd1c2dd8aa3eb4a5d004e4516 to your computer and use it in GitHub Desktop.
Advent of Code 2022
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
CREATE top3 3 CELLS ALLOT top3 3 CELLS 0 FILL | |
0 VALUE file | |
12 CONSTANT max-line | |
CREATE line-buffer max-line ALLOT | |
: init-file ( -- ) S" day1.txt" R/O OPEN-FILE THROW TO file ; | |
: free-file ( -- ) file CLOSE-FILE THROW 0 TO file ; | |
: step ( -- u f ) line-buffer max-line 2 - file READ-LINE THROW ; | |
: update-leaderboard ( u -- ) | |
DUP top3 @ > IF | |
top3 top3 CELL+ 2 CELLS MOVE | |
top3 ! | |
ELSE | |
DUP top3 CELL+ @ > IF | |
top3 CELL+ @ top3 2 CELLS + ! | |
top3 CELL+ ! | |
ELSE | |
DUP top3 2 CELLS + @ > IF | |
top3 2 CELLS + ! | |
ELSE | |
DROP | |
THEN | |
THEN | |
THEN ; | |
: process ( -- ) | |
0 BEGIN | |
step | |
WHILE | |
?DUP IF | |
line-buffer SWAP 0. 2SWAP >NUMBER 2DROP D>S + | |
ELSE | |
update-leaderboard 0 | |
THEN | |
REPEAT 2DROP ; | |
init-file | |
process | |
." Max: " top3 @ . CR | |
." Sum: " top3 @ top3 CELL+ @ + top3 2 CELLS + @ + . CR | |
free-file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment