Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'22 Q13, fixing solution #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

gregszumel
Copy link

Really useful repo! Got stuck on AoC '22 #13, and the solution from this repo was not accepted (6,104). Maybe I set up the repo incorrectly, but I documented what I see the problem is and my solution to it below.

The problem: isInOrder(left, right) returns ties incorrectly as inOrder, while it should just continue to evaluate unless it's the end of the input. For example, take the two example inputs:

#1
[[1,2,3,4], 5]
[[1,2,3,4], 3]

#2
[[1,2,3,4], 2]
[[1,2,3,4], 3]

#1 is NOT in order, #2 is in order. However, isInOrder returns that BOTH are in order. isInOrder evaluates the first first element of both lists, sees they are lists. Then it recurses on the list, checking each element. After checking all elements and finding all ties, it returns true (line 138) and pops the recursion. Then line 128/135 return as true, while they should really continue checking the rest of the list since the recursed list was tied.

My Solution: add a tied-indicator to isInOrder.

I modify isInOrder to return two booleans, one to indicate if they are in order, the other to indicate if there is a tie. The recursive calls will now only return if an explicitly correct/incorrect ordering was found. If a tie was found, isInOrder continues to the next element. isInOrder only returns a tied indicator if it iterates through the whole list without finding an out of order pair and if the list lengths are equal. This returns the correct solution (5,675).

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