Refactor to use generic Set type and update stdlib imports#2
Open
Refactor to use generic Set type and update stdlib imports#2
Conversation
- download-puzzles: fetches all AoC puzzle descriptions (2015-2024) and converts to markdown - analyze-puzzles: scans puzzles and identifies common algorithmic patterns https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
Tests: - binary_test.go: hex/binary conversion tests - functional_test.go: Map, Filter, FilterMap tests - grid_test.go: Grid creation, access, iteration tests - grid2_test.go: Grid2 sparse grid tests - ints_test.go: Ints type method tests - math_test.go: math utilities tests - range_test.go: Range and Rangeset tests - search_test.go: BFS/DFS tests - set_test.go: Set, StringSet, IntSet tests - stack_test.go: Stack and Queue tests - tree_test.go: Node tree traversal tests - utils_test.go: AtoI, BtoI, ReplaceAll tests Bug fixes: - range.go: Fixed Range.Equal (was comparing Start to End instead of End to End) - grid2.go: Fixed SlopeIterate infinite loop (origin was never updated) - graphics.go: Fixed SaveGIF ignoring filename parameter https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
New utilities: - memo.go: Memoize, Memoize2, MemoizeRecursive for dynamic programming - parse.go: ParseInts, ParseInt64s, SplitInts, Words for input parsing - pqueue.go: Generic priority queue using container/heap - dijkstra.go: Dijkstra, DijkstraAll, DijkstraFunc for weighted shortest path - slices.go: Reverse, Chunk, Window, Pairs, Count, All, Any, Find, Unique, Flatten, Reduce - directions.go: Direction constants (Up/Down/Left/Right), DirFromChar, TurnLeft/Right, Neighbors4/8 Improvements: - grid.go: Fixed Width() panic on empty grid, added IsEmpty(), fixed typo - set.go: Added deprecation notices for StringSet/IntSet (use Set[T] instead) - math.go: Added deprecation notice for PermutationsString - ints.go: Added note about generic alternatives - search.go: Improved BFS/DFS documentation, removed old commented code - functional.go: Removed redundant commented Reduce (now in slices.go) https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
Changes: - slices.go: Use slices.Clone + slices.Reverse for Reverse() - ints.go: Use slices.Min, slices.Max, slices.Index from stdlib - math.go: Use slices.Min/Max internally, define local Integer/Signed constraints - set.go: Add MapKeys helper (maps.Keys not in stdlib until Go 1.23) - grid2.go: Use MapKeys instead of maps.Keys The aoc/ library no longer imports golang.org/x/exp. (Some solution files in 2023/ still use x/exp/maps) https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
Update all 2023 solution files to use stdlib maps package with slices.Collect() wrapper for maps.Keys/Values (Go 1.23+ returns iterators). Removes golang.org/x/exp dependency entirely from project. https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
Major changes: - Remove deprecated StringSet/IntSet types (use Set[string]/Set[int]) - Remove deprecated ReverseInPlace (use slices.Reverse) - Remove deprecated PermutationsString (use Permutations[string]) - Remove MapKeys helper (use slices.Collect(maps.Keys())) - Merge functional.go into slices.go (Map, Filter, FilterMap) - Fix Queue to use two-stack implementation for O(1) amortized ops - Fix Words() to filter empty strings - Add deprecation notices to Ints type and StringInts Updates to solution files: - 2020/5, 2020/6, 2020/7, 2020/16: Use Set[T] instead of StringSet/IntSet - 2021/4, 2021/8, 2021/12: Use Set[T] instead of StringSet/IntSet - wordle: Use Set[T] instead of StringSet https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
Also simplified the Series implementation and fixed usage in 2020/5 that depended on the method syntax. https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
Major cleanup: - Remove deprecated Ints type and all methods (Sum, Product, Min, Max, etc.) - Remove deprecated StringInts function (use Map(AtoI, ...) instead) - Remove Zip function (had weak typing with [][2]any return type) - Keep only Series function in ints.go Updated solution files: - 2020/5, 2020/9, 2020/10, 2020/15, 2020/16: Use []int instead of Ints - 2023/4, 2023/5, 2023/6, 2023/9, 2023/12: Use Map(AtoI, ...) instead of StringInts The aoc library now uses modern Go idioms: - []int with standalone Sum/Min/Max functions - Map(AtoI, ...) for string-to-int conversion - Set[T] generic type (no more StringSet/IntSet) - O(1) amortized Queue https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
- Remove Test64 (redundant with generic Test[T]) - Remove MustInt (alias for AtoI) - Remove Min/Max wrappers (use builtin min/max or slices.Min/Max) - Merge ints.go (Series) into slices.go - Merge functional_test.go into slices_test.go - Update all solution files to use stdlib slices.Min/Max or builtins https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
Previously returned [goal] even when goal was unreachable, which could cause incorrect results. Now matches Dijkstra behavior of returning nil. https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
The name SparseGrid better describes what it is: - Sparse storage (map, not full 2D array) - Supports negative coordinates - Good for large/infinite grids https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR modernizes the codebase by replacing concrete
StringSetandIntSettypes with a genericSet[T]implementation, updates deprecatedgolang.org/x/expimports to use Go 1.22+ stdlib packages, and adds new utility modules for common AoC patterns.Key Changes
Generic Set Refactoring
aoc.StringSetwithaoc.Set[string]aoc.IntSetwithaoc.Set[int]NewStringSet()→NewSet[string](),NewIntSet()→NewSet[int]()AddMany()→AddSlice()for consistencyStandard Library Import Updates
golang.org/x/exp/mapswith stdlibmapspackage (Go 1.22+)golang.org/x/exp/sliceswith stdlibslicespackagemaps.Keys()andmaps.Values()withslices.Collect()where needed for type compatibilityNew Utility Modules
aoc/directions.go: Cardinal and diagonal direction constants, direction parsing, and neighbor finding utilitiesaoc/dijkstra.go: Generic Dijkstra's algorithm implementation with variants for single goal, all distances, and predicate-based goalsaoc/binary.go: Hex-to-binary and binary-to-decimal conversion utilities (moved from functional.go)Test Coverage
aoc/binary_test.go: 161 lines covering hex/binary conversions and round-trip testsaoc/dijkstra_test.go: 143 lines covering pathfinding scenariosaoc/directions_test.go: 210 lines covering direction operations and neighbor findingaoc/functional_test.go: 287 lines covering Map/Filter/FilterMap operationsBug Fixes
SaveGIF()using hardcoded filename instead of parameterGrid.Width()to handle empty grids gracefullyGrid.IsEmpty()convenience methodGrid2.SlopeIterate()variable naming issueCode Quality
aoc/functional.go(functionality preserved in tests)https://claude.ai/code/session_0177AokNsFRhuZtpAAVw2tmn