Skip to content

Commit

Permalink
[Go] refactor to use set
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilbpandey committed Dec 19, 2023
1 parent 9d0741c commit 85184b6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
11 changes: 6 additions & 5 deletions 2023/day04/day04.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,24 @@ import (

"github.com/sunilbpandey/advent-of-code/utils/go/intutils"
"github.com/sunilbpandey/advent-of-code/utils/go/strutils"
"github.com/sunilbpandey/go-toolkit/set"
)

//go:embed input.txt
var content string

func parseWinningNumbers(text string) map[string]bool {
winningNumbers := map[string]bool{}
func parseWinningNumbers(text string) set.Set[string] {
winningNumbers := set.Set[string]{}
for _, number := range regexp.MustCompile(`\s+`).Split(text, -1) {
winningNumbers[number] = true
winningNumbers.Add(number)
}
return winningNumbers
}

func countNumbersWon(winningNumbers map[string]bool, text string) int {
func countNumbersWon(winningNumbers set.Set[string], text string) int {
count := 0
for _, number := range regexp.MustCompile(`\s+`).Split(strings.TrimSpace(text), -1) {
if winningNumbers[number] {
if winningNumbers.Contains(number) {
count++
}
}
Expand Down
8 changes: 5 additions & 3 deletions 2023/day16/day16.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
_ "embed"
"strconv"
"strings"

"github.com/sunilbpandey/go-toolkit/set"
)

//go:embed input.txt
var content string

func countEnergizedTiles(grid []string, beams []Beam) int {
seen := make(map[string]bool)
seen := set.NewSet[string]()
energized := make(map[int]map[int]bool)
for len(beams) > 0 {
updated := []Beam{}
Expand All @@ -20,10 +22,10 @@ func countEnergizedTiles(grid []string, beams []Beam) int {
}

key := beam.String()
if _, exists := seen[key]; exists {
if seen.Contains(key) {
continue
}
seen[key] = true
seen.Add(key)

if _, exists := energized[beam.Row]; !exists {
energized[beam.Row] = make(map[int]bool)
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/sunilbpandey/advent-of-code

go 1.21.0
go 1.21.5

require github.com/sunilbpandey/go-toolkit v0.0.0-20231219023211-34a174173d8a
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/sunilbpandey/go-toolkit v0.0.0-20231219023211-34a174173d8a h1:Gf12D00C41/D6wxq1is77Hyz9rKqOb3St/BGz7SFMP4=
github.com/sunilbpandey/go-toolkit v0.0.0-20231219023211-34a174173d8a/go.mod h1:BbZExXJrrlfCb8QTtmx21LHpxbWcfUIe6AzVlpHR4Js=

0 comments on commit 85184b6

Please sign in to comment.