day3, part2

This commit is contained in:
efim 2023-12-03 07:27:00 +00:00
parent b767ef256d
commit f2f250eabd
1 changed files with 33 additions and 7 deletions

View File

@ -24,14 +24,16 @@ func Run() int {
fmt.Println(num) fmt.Println(num)
} }
partNumbersSum := 0 // partNumbersSum := 0
for _, num := range numbers { // for _, num := range numbers {
if len(num.Specials) > 0 { // if len(num.Specials) > 0 {
partNumbersSum += num.Num // partNumbersSum += num.Num
} // }
} // }
return partNumbersSum gearRatiosSum := partTwo(numbers)
return gearRatiosSum
} }
type Matrix [][]rune type Matrix [][]rune
@ -135,3 +137,27 @@ func isValidCoord(x, y int, m *Matrix) bool {
} }
return true 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
}