So I managed to get part 1 of the day, but it took 2 seconds to run on the real input, which is a bad sign.

I can’t see any kind of optimisation that means I can skip checks and know how many combinations are in those skipped checks (aside from 0.) I can bail out of branches of combinations if the info so far won’t fit, but that still leads me to visiting every valid combination which in one of the examples is 500k. (And probably way more in the input, since if I can’t complete the example near instantly the input is not happening.)

Right now I take the string, then replace the first instance of a ? with the two possible options. Check it matches the check digits so far then use recursion on those two strings.

I can try to optimise the matching, but I don’t think that solves the real problem of visiting every combination.

I don’t think (or hope) it’s just bad code but this is my code so far (python.)

edit:

spoiler

a cache was the solution!

You are viewing a single thread.
View all comments
2 points

I don’t think there are many significant optimizations with regards to reducing the search tree. It took me long enough to get behind it, but the “solution” (not saying there aren’t other ways) to part 2 is to not calculate anything more than once. Instead put partial solutions in a dict indexed by the current state and use that cached value if you need it again.

It seems like you are actually constructing all rows with replaced ?. This won’t be viable for part 2, your memory usage will explode. I have a recursive function that calls itself twice whenever a ? is encountered, once assuming it’s a ., and once a #.

permalink
report
reply
1 point

Memory is fine but I think I get what you mean. In the example:

????.###.????.### 1,3,1,3

I’m checking the second unknowns combinations for each of the first, but if my state was say

data: '...#.##.????.###'
position -----^
check_list: [1,3]

And I get 4 combinations from recursion then, I know that is the same number of combination for any of the first unknowns.

So I can then cache ".????.###",[1,3] -> 4.

permalink
report
parent
reply

Advent Of Code

!advent_of_code@programming.dev

Create post

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

  • Follow the programming.dev instance rules
  • Keep all content related to advent of code in some way
  • If what youre posting relates to a day, put in brackets the year and then day number in front of the post title (e.g. [2023 Day 10])
  • When an event is running, keep solutions in the solution megathread to avoid the community getting spammed with posts

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

Community stats

  • 2

    Monthly active users

  • 76

    Posts

  • 779

    Comments