Last active
December 4, 2025 04:53
-
-
Save danielcristofani/78d2f83c0f18341ecf0b402d0660cfd7 to your computer and use it in GitHub Desktop.
brainfuck solution for day 3 of Advent of Code 2025
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
| [2025day3.b -- 2025 Advent of Code Day 3 | |
| (c) 2025 Daniel B. Cristofani | |
| http://brainfuck.org/ | |
| This program is licensed under a Creative Commons Attribution-ShareAlike 4.0 | |
| International License (http://creativecommons.org/licenses/by-sa/4.0/). | |
| This program solves Advent of Code 2025, Day 3. | |
| (https://adventofcode.com/2025/day/3) | |
| You can change battery count in parentheses at the start for part 1 or 2. | |
| This uses a moving window on the input text; I'm sure many people found the | |
| same solution. A chunk of the input text is paired with a matching array of | |
| "highest value found for this position"; each time we shift the input text, | |
| we check, from most to least significant digit, "is this better than what | |
| we already have"; if so, we replace that "best" value, AND all less | |
| significant digits' best values, with the matching input digits. | |
| When we find a linefeed we add the final "best" values into our running | |
| total and reset "best" and "input" to all 1s. They're kept nonzero to keep | |
| track of the length of our window. This means the program doesn't reject | |
| invalid banks with fewer batteries than the specified count, but I wasn't | |
| going to bother doing that anyway. | |
| Basic memory layout is: | |
| 0 0 0 0 b 0 i t b 0 i t b 0 i t ... b 0 i t 0 0 0 ... | |
| where b is "best found", i is "input", t is "total". t is stored such that | |
| a 0 digit has a cell value of 1 and a 9 digit has a cell value of 10, so we | |
| can track the length of it. b and i are stored as 0=0. | |
| This is O(12*input length), about 0.06 seconds on my laptop. It assumes | |
| EOF="no change" or EOF=0. Does not assume cells are bytes, I don't think. | |
| If you have questions or comments, feel free to email me, and/or consider | |
| reading my "get good at brainfuck" series (https://brainfuck.org/ggab.html) | |
| which I wrote to try to help people get a handle on the language.] | |
| >>>>(++++++++++++)[-[>>>>+<<<<-]+>>+>+>]<[<<<<]<,[ | |
| ----------[ | |
| -->++++++[<------>-]>[>>>>]<<[-]<<[<<[>>>>+<<<<-]<<]>>>>[>>]<<[ | |
| >>+<<[<<[-<<<+>>>>>-<]>]>>>[<<<+[>]]<-<<<<<<<[>>>+>>+<<<<<-] | |
| >>>>[->[<<[-]>>[<+>-]<[<+>>+<-]<<<]>>>]<<< | |
| ]< | |
| ]>>[ | |
| [[>>>+<<<-]+>>[-]>>]<[<<<<]>>>>[ | |
| <<++++++++++[>>[->>+<<<]<[>]<-] | |
| >>[>>[-]>>[-<<]<<[>>]>>++<<<<[>>+<<-]]>>[<<+>>-]>> | |
| ]>-[+<<-]+[>>+<<<<<<]>>> | |
| ]<, | |
| ]>>>>>[++>[-]++++++++>>>]<<<[+[<+++++>-]<.<<<] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment