diff --git a/day3/day-three.go b/day3/day-three.go index 8c4c141..7dd7138 100644 --- a/day3/day-three.go +++ b/day3/day-three.go @@ -24,14 +24,16 @@ func Run() int { fmt.Println(num) } - partNumbersSum := 0 - for _, num := range numbers { - if len(num.Specials) > 0 { - partNumbersSum += num.Num - } - } + // partNumbersSum := 0 + // for _, num := range numbers { + // if len(num.Specials) > 0 { + // partNumbersSum += num.Num + // } + // } - return partNumbersSum + gearRatiosSum := partTwo(numbers) + + return gearRatiosSum } type Matrix [][]rune @@ -135,3 +137,27 @@ func isValidCoord(x, y int, m *Matrix) bool { } return true } + +// part 2 - find all gears '*' that connect exactly two numbers +// multiply the numbers and calculate total sum +// i guess i could do full scan of my numbers? +// every time i encounter '*' in special symbols, could put that into map, hopefully? +// with value for the number + +func partTwo(numbers []MatrixNumber) int { + res := 0 + specialsMap := make(map[SpecialSymbol][]MatrixNumber) + for _, num := range numbers { + for special, _ := range num.Specials { + specialsMap[special] = append(specialsMap[special], num) + } + } + + for special, nums := range specialsMap { + if special.Symb == '*' && len(nums) == 2 { + res += nums[0].Num * nums[1].Num + } + } + + return res +}