Skip to content

Pre-compute initial explode actions - #105

Open
ebblake wants to merge 1 commit into
maneatingape:mainfrom
ebblake:2021-18.1
Open

Pre-compute initial explode actions#105
ebblake wants to merge 1 commit into
maneatingape:mainfrom
ebblake:2021-18.1

Conversation

@ebblake

@ebblake ebblake commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Description

During part two, we are computing a pairing between every two lines. Any line that had one or more depth 4 number pairs will cause the same set of explode actions to be taken for each of the other 99 lines it is paired with, before we get into the main effort of add() in processing split actions. Rather than repeat early work that will produce the same answer, we can instead pre-compute these initial explode calls during parse time.

On my input, adding a simple counter shows that this reduces the number of calls to explode() from 180k down to 131k. parse() and part1() runtimes are about the same, while part2() speeds up from 600us to 560us.

Type of change

  • Performance improvement
  • Bug fix
  • Other

Checklist

  • Pull request title and commit messages are clear and informative.
  • Documentation has been updated if necessary.
  • Code style matches the existing code. This one is somewhat subjective, but try to "fit in" by
    using the same naming conventions. Code should be portable, avoiding any
    architecture-specific intrinsics.
  • Tests pass cargo test
  • Code is formatted cargo fmt -- `find . -name "*.rs"`
  • Code is linted cargo clippy --all-targets --all-features

Formatting and linting also can be executed by running just
(if installed) on the command line at the project root.

During part two, we are computing a pairing between every two
lines. Any line that had one or more depth 4 number pairs will cause
the same set of explode actions to be taken for each of the other 99
lines it is paired with, before we get into the main effort of add()
in processing split actions.  Rather than repeat early work that will
produce the same answer, we can instead pre-compute these initial
explode calls during parse time.

On my input, adding a simple counter shows that this reduces the
number of calls to explode() from 180k down to 131k. parse() and
part1() runtimes are about the same, while part2() speeds up from
600us to 560us.
@ebblake

ebblake commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

@ebblake

ebblake commented Jul 31, 2026

Copy link
Copy Markdown
Contributor Author

It would also be an interesting experiment (one I still need to run) to see if there is any reliable relation between magnitude(reduce([left,0])) and the eventual magnitude(reduce([left,right])) winner. For example, if the winning magnitude always depends on one of the top-ten magnitude values out of the 100 inputs being on the left, we could avoid a full 100*99 add() calls in part 2 and instead focus on the ones most likely to produce a large value. But it is complicated by the fact that split() means the mapping is not straightforward: [[0,[0,[0,0]]],[[[9,0],0],0]] has magnitude 486, but if any explode resulted in the intermediate [[0,[0,[0,0]]],[[[10,0],0],0]] that reduces to [[0,[0,[0,5]]],[[[0,5],0],0]] with magnitude 300.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant