-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4195e3d
commit 4f1268b
Showing
7 changed files
with
609 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package gear_ratios | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
type Number struct { | ||
Value string | ||
Row int | ||
StartIndex int | ||
EndIndex int | ||
} | ||
|
||
type Symbol struct { | ||
Value string | ||
Row int | ||
Index int | ||
} | ||
|
||
func Run() { | ||
var ( | ||
lines []string | ||
numberList []Number | ||
symbolList []Symbol | ||
) | ||
|
||
file, err := os.Open("./03_gear_ratios/input.txt") | ||
|
||
if err != nil { | ||
fmt.Println("Error reading input file:", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
lines = append(lines, line) | ||
} | ||
numberList = buildNumberList(lines) | ||
symbolList = buildSymbolList(lines) | ||
|
||
fmt.Println("Part One - Sum of part numbers:", findPartNumbersSum(numberList, symbolList)) | ||
fmt.Println("Part Two - Sum of gear ratios:", findGearRatiosSum(numberList, symbolList)) | ||
} | ||
|
||
func buildNumberList(input []string) []Number { | ||
numberList := []Number{} | ||
digitPattern := `\d+` | ||
digitRegex := regexp.MustCompile(digitPattern) | ||
|
||
for idx, line := range input { | ||
matches := digitRegex.FindAllStringIndex(line, -1) | ||
|
||
for _, match := range matches { | ||
startIndex := match[0] | ||
endIndex := match[1] | ||
matchedNumber := line[startIndex:endIndex] | ||
numberList = append(numberList, Number{ | ||
Value: matchedNumber, | ||
StartIndex: startIndex, | ||
EndIndex: endIndex, | ||
Row: idx, | ||
}) | ||
} | ||
} | ||
|
||
return numberList | ||
} | ||
|
||
func buildSymbolList(input []string) []Symbol { | ||
symbolList := []Symbol{} | ||
symbolPattern := `[^\d|.]` | ||
symbolRegex := regexp.MustCompile(symbolPattern) | ||
|
||
for idx, line := range input { | ||
matches := symbolRegex.FindAllStringIndex(line, -1) | ||
|
||
for _, match := range matches { | ||
index := match[0] | ||
matchedSymbol := line[index] | ||
symbolList = append(symbolList, Symbol{ | ||
Value: string(matchedSymbol), | ||
Index: index, | ||
Row: idx, | ||
}) | ||
} | ||
} | ||
|
||
return symbolList | ||
} | ||
|
||
func findPartNumbersSum(numberList []Number, symbolList []Symbol) int { | ||
partNumbersSum := 0 | ||
|
||
for _, number := range numberList { | ||
for _, symbol := range symbolList { | ||
inAdjacentRow := symbol.Row >= number.Row-1 && symbol.Row <= number.Row+1 | ||
inAdjacentColumn := symbol.Index >= number.StartIndex-1 && symbol.Index <= number.EndIndex | ||
|
||
if inAdjacentRow && inAdjacentColumn { | ||
numberValue, _ := strconv.Atoi(number.Value) | ||
partNumbersSum += numberValue | ||
break | ||
} | ||
} | ||
} | ||
|
||
return partNumbersSum | ||
} | ||
|
||
func findGearRatiosSum(numberList []Number, symbolList []Symbol) int { | ||
gearRatiosSum := 0 | ||
|
||
for _, symbol := range symbolList { | ||
adjacentNumbers := []Number{} | ||
|
||
if symbol.Value != "*" { | ||
continue | ||
} | ||
|
||
for _, number := range numberList { | ||
numberLength := len(number.Value) | ||
inAdjacentRow := number.Row >= symbol.Row-1 && number.Row <= symbol.Row+1 | ||
inAdjacentColumn := number.StartIndex >= symbol.Index-numberLength && number.EndIndex <= symbol.Index+numberLength+1 | ||
|
||
if inAdjacentRow && inAdjacentColumn { | ||
adjacentNumbers = append(adjacentNumbers, number) | ||
} | ||
} | ||
|
||
if len(adjacentNumbers) == 2 { | ||
firstNumber, _ := strconv.Atoi(adjacentNumbers[0].Value) | ||
secondNumber, _ := strconv.Atoi(adjacentNumbers[1].Value) | ||
gearRatio := firstNumber * secondNumber | ||
|
||
gearRatiosSum += gearRatio | ||
} | ||
} | ||
|
||
return gearRatiosSum | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
467..114.. | ||
...*...... | ||
..35..633. | ||
......#... | ||
617*...... | ||
.....+.58. | ||
..592..... | ||
......755. | ||
...$.*.... | ||
.664.598.. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package scratchcards | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"math" | ||
"os" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func Run() { | ||
var ( | ||
lines []string | ||
) | ||
|
||
file, err := os.Open("./04_scratchcards/input.txt") | ||
if err != nil { | ||
fmt.Println("Error reading input:", err) | ||
return | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
lines = append(lines, line) | ||
} | ||
|
||
totalPointsWon, totalScratchcardsWon := findPointsWon(lines) | ||
|
||
fmt.Println("Part One - Total points won:", totalPointsWon) | ||
fmt.Println("Part Two - Total scratchcards won:", totalScratchcardsWon) | ||
} | ||
|
||
func findPointsWon(cards []string) (int, int) { | ||
totalPoints := 0 | ||
totalScratchcards := 0 | ||
digitPattern := `\d+` | ||
digitRegex := regexp.MustCompile(digitPattern) | ||
scratchcardsWonCache := map[int]int{} | ||
|
||
for idx, card := range cards { | ||
pointsWon := []string{} | ||
|
||
parts := strings.Split(card, ":") | ||
points := strings.Split(parts[1], "|") | ||
winningPoints := digitRegex.FindAllString(points[0], -1) | ||
pointsGotten := digitRegex.FindAllString(points[1], -1) | ||
|
||
for _, winningPoint := range winningPoints { | ||
for _, pointGotten := range pointsGotten { | ||
if winningPoint == pointGotten { | ||
pointsWon = append(pointsWon, winningPoint) | ||
break | ||
} | ||
} | ||
} | ||
|
||
cardPoints := len(pointsWon) | ||
scratchcardsWonCache[idx] = cardPoints | ||
|
||
if len(pointsWon) == 0 { | ||
continue | ||
} | ||
|
||
totalPoints += int(math.Pow(2, float64(len(pointsWon)-1))) | ||
} | ||
|
||
for i := len(cards) - 1; i >= 0; i-- { | ||
currCardPoints := scratchcardsWonCache[i] | ||
totalScratchcards++ | ||
currScratchcards := 1 | ||
|
||
for j := 1; j <= currCardPoints; j++ { | ||
totalScratchcards += scratchcardsWonCache[i+j] | ||
currScratchcards += scratchcardsWonCache[i+j] | ||
} | ||
scratchcardsWonCache[i] = currScratchcards | ||
} | ||
|
||
return totalPoints, totalScratchcards | ||
} |
Oops, something went wrong.