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

bug: Fixing day 12 solution #30

Merged
merged 1 commit into from
Jan 5, 2025
Merged

bug: Fixing day 12 solution #30

merged 1 commit into from
Jan 5, 2025

Conversation

truggeri
Copy link
Owner

@truggeri truggeri commented Jan 5, 2025

Summary

My original solution from #13 did not solve Day 12 correctly. This change fixes that. Rather than maintaining lists of patches, we now have a single reference to the "garden" (data semantics of a reference) and iterate over it. The new approach is as follows,

  1. Parse the input characters into plots
  2. Update each plot to use the correct fences
  3. Assign region ids to each plot from top down and left to right
  4. Look for regions that can be merged from left to right
  • Always merge from higher id into lower id which removes the possibility of loops
  • Only merge into a patch once per iteration
  • Continue this iteration until no potential merges are found
  1. Iterate over the plots to collect the area and perimeter stats on each patch
  2. Calculate cost of each patch

The merge algorithm took the longest to get right. Having logic that detected when prior merges affected the current merge was tricky to get right, but simplifying the amount of changes per iteration and increasing the number of iterations fixed things.

@truggeri truggeri self-assigned this Jan 5, 2025
@truggeri truggeri merged commit b7c4835 into main Jan 5, 2025
1 check passed
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