Created
April 11, 2023 01:54
-
-
Save dkaraush/1ee39b8e1c185a95810446fe3ff84492 to your computer and use it in GitHub Desktop.
advent of code 2022 first task in brainfuck
This file contains 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
>>>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[ | |
-<<+>>]<,----------]<[[-<+>]>,----------[--------------------------------------< | |
[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<]<[<[->>+>+<<<]>>>[-<<<+>>>]<<[-> | |
>+>+<<<]>>>[-<<<+>>>][-]>[-]>[-]+>[-]<<<<[>+>+<<-]>[<+>-]<<[>>+<<-]+>>>[>-]>[<<< | |
<->>[-]>>->]<+<<[>-[>-]>[<<<<->>[-]+>>->]<+<<-][-]>[-]>[-]<<<[-][-]>[-]<<[>+>+<< | |
-]>[<+>-]+>[<<<<[-]>[-<+>]>>->[-]]<[-]<<[-][-]>[-]>,----------[----------------- | |
---------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<[[-<+>]>,-- | |
--------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>> | |
]<,----------]<]<][-]>[-]+>[-]+<[>[-<-<<[->+>+<<]>[-<+>]>>]++++++++++>[-]+>[-]>[ | |
-]>[-]<<<<<[->-[>+>>]>[[-<+>]+>+>>]<<<<<]>>-[-<<+>>]<[-]++++++++[-<++++++>]>>[-< | |
<+>>]<<]<[.[-]<]< |
This file contains 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
some drafts of making that. | |
>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]< | |
# readInt({A=0}, B=0, C=0)) = A { | |
>,---------- # B = read() - 10 | |
[ # while (B != 0) { | |
-------------------------------------- # B -= 38 | |
<[->>++++++++++<<]>[->+<]>[-<<+>>]< # A = A * 10 + B | |
,---------- # B = read() - 10 | |
]< # } | |
# } | |
>>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<[[-<+>]>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<]< | |
# readIntSum({A=0} B=0) = A | |
> # A = 0 | |
>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]< # B = readInt() | |
[ # while (B != 0) { | |
[-<+>] # A = A B | |
>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]< # B = readInt() | |
]< # } | |
> | |
# read and sum integers | |
>>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<[[-<+>]>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<]< | |
[ | |
< | |
# MAX FUNCTION: | |
# check x less y | |
[->>+>+<<<]>>>[-<<<+>>>]<<[->>+>+<<<]>>>[-<<<+>>>]<<>>[-]>[-]>[-]+>[-]<<<<[>+>+<<-]>[<+>-]<<[>>+<<-]+>>>[>-]>[<<<<->>[-]>>->]<+<<[>-[>-]>[<<<<->>[-]+>>->]<+<<-][-]>[-]>[-]<<<[-]< | |
# if x less y ? return y : x | |
>[-]>[-]<<[>+>+<<-]>[<+>-]+>[<<<<[-]>[-<+>]>>><->[-]]<[-]<<[-]< | |
>[-] | |
# read and sum integers | |
>[-]>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<[[-<+>]>,----------[--------------------------------------<[->>++++++++++<<]>[->+<]>[-<<+>>]<,----------]<]< | |
] | |
< | |
>[-]>[-]+>[-]+<[>[-<-<<[->+>+<<]>[-<+>]>>]++++++++++>[-]+>[-]>[-]>[-]<<<<<[->-[>+>>]>[[-<+>]+>+>>]<<<<<]>>-[-<<+>>]<[-]++++++++[-<++++++>]>>[-<<+>>]<<]<[.[-]<]< | |
{x} y t0 tx1 ty1 tx2 ty2 z S | |
# (x y) => (x y x y) => (x y x y z) | |
# z = x < y | |
[->>+>+<<<]>>>[-<<<+>>>]<<[->>+>+<<<]>>>[-<<<+>>>]<<>>[-]>[-]>[-]+>[-]<<<<[>+>+<<-]>[<+>-]<<[>>+<<-]+>>>[>-]>[<<<<->>[-]>>->]<+<<[>-[>-]>[<<<<->>[-]+>>->]<+<<-][-]>[-]>[-]<<<[-]< | |
# if z ? (y) : (x) | |
>[-]>[-]<<[>+>+<<-]>[<+>-]+>[<<<<[-]>[-<+>]>>><->[-]]<[-]<<[-]< | |
>>[-]+>[-]>[-]<<[- // at t0=0 | |
<< // at x | |
[ | |
>[ | |
-<->>+>+ // x--, y--, t0++, z++ | |
> // goto t1, break | |
] | |
// y if y == 0, otherwise at t1 | |
] | |
] | |
[>+>+<<-]>[<+>-]+>[ | |
<->[-]]<[ | |
-] | |
if({A} temp0=0 temp1=0) | |
[>+>+<<-]>[<+>-]+>[ | |
# CODE IF TRUE (AT temp1) | |
<->[-]]<[ | |
# CODE IF FALSE (AT temp0) | |
-] | |
more({x} y=? z=? temp0=0 temp1=0) = z | |
>>[-]>[-]>[-]<<<<[>>>+<<[->>[-]>+<<<]>>[-<+>]>[-<<<+>>>]<<<-<-]> | |
# max(a, b) = (a + b + |a - b|) / 2 | |
# max({A} B} = A | |
sub({x} y) = a | |
>>[-]<[<->>+<-]>[<+>-]<<[-]>>[-<<+>>]<< | |
divide2({x}) = x | |
[-->+<]>[-<+>] | |
# max(A, B) = A | |
# | |
[->[-] | |
do while | |
+[- | |
B = readIntSum() | |
if (B != 0) { | |
} | |
max() | |
] |
This file contains 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
const fs = require('fs') | |
const Brainfuck = require('brainfuck-node'); | |
const brainfuck = new Brainfuck({ maxSteps: -1, base: Infinity }); | |
const start = Date.now() | |
console.log( | |
brainfuck.execute( | |
fs.readFileSync('./code.bf').toString(), | |
fs.readFileSync('./input.txt').toString() | |
) | |
) | |
console.log("Calculated in " + (Date.now() - start) + "ms"); // 282284ms = 4.7 minutes 🤯 | |
// оно не работает, мне пришлось делать форк |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment