Skip to content

Commit

Permalink
Day 11
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-spivakov committed Oct 19, 2024
1 parent d7c4663 commit 5dadb6a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A small practice project created to get familiar with Go syntax.
- [x] day 8
- [x] day 9
- [x] day 10
- [ ] day 11
- [x] day 11
- [ ] day 12
- [ ] day 13
- [ ] day 14
Expand Down
108 changes: 80 additions & 28 deletions day11/day11.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,69 @@ import (
)

func Solution1(filepath string) int {
matrix := parseInputMatrix(filepath)
printMatrix(matrix)
matrix := parseInputMatrix(filepath, true)
// printMatrix(matrix)
galaxies := findGalaxies(&matrix)
fmt.Printf("Galaxies %d: %v\n", len(galaxies), galaxies)
gPairs := makePairs(galaxies)
fmt.Printf("All pairs: %v\n", gPairs)

result := 0
for _, pair := range gPairs {
res := naiveDistCalc(pair[0], pair[1], pair[2], pair[3])
// fmt.Printf("ADDING {%d, %d}, {%d, %d} = %d\n", pair[0], pair[1], pair[2], pair[3], res)
result += res
}
return result
}

func Solution2(filepath string) int {
return 0
matrix := parseInputMatrix(filepath, false)
// printMatrix(matrix)
galaxies := findGalaxies(&matrix)
gPairs := makePairs(galaxies)

result := 0
dupVertical, dupHorizontal := findBlankLines(matrix)
for _, pair := range gPairs {
res := naiveDistCalc2(pair[0], pair[1], pair[2], pair[3], dupVertical, dupHorizontal)
result += res
}
return result
}

func naiveDistCalc(x1, y1, x2, y2 int) int {
func naiveDistCalc2(y1, x1, y2, x2 int, dupVertical, dupHorizontal []int) int {
dist := 0

for x1 != x2 {
diff := 1
if _, found := slices.BinarySearch(dupVertical, x1); found {
diff = 1000000
}

if x1 > x2 {
x1--
} else {
x1++
}
dist += diff
}

for y1 != y2 {
diff := 1
if _, found := slices.BinarySearch(dupHorizontal, y1); found {
diff = 1000000
}

if y1 > y2 {
y1--
} else {
y1++
}
dist += diff
}

return dist
}

func naiveDistCalc(y1, x1, y2, x2 int) int {
dist := 0

for x1 != x2 {
Expand Down Expand Up @@ -87,7 +129,7 @@ func findGalaxies(matrix *[][]string) [][2]int {
return gCoords
}

func parseInputMatrix(filepath string) [][]string {
func parseInputMatrix(filepath string, normalize bool) [][]string {
file, err := os.Open(filepath)
if err != nil {
log.Fatalf("Failed to open the input file with: %v\n", err.Error())
Expand All @@ -102,13 +144,36 @@ func parseInputMatrix(filepath string) [][]string {
log.Fatalf("Error during reading the input file: %v\n", err.Error())
}

return normalizeMatrix(matrix)
if normalize {
return normalizeMatrix(matrix)
} else {
return matrix
}
}

func normalizeMatrix(matrix [][]string) [][]string {
var normalized [][]string

var dVertical, dHorizonatl []int
dupVertical, dupHorizontal := findBlankLines(matrix)

for y := len(matrix) - 1; y >= 0; y-- {
newRow := make([]string, len(matrix[0]), len(matrix[0])+len(dupVertical))
copy(newRow, matrix[y])
for x := len(dupVertical) - 1; x >= 0; x-- {
newRow = slices.Insert(newRow, dupVertical[x], ".")
}

normalized = slices.Insert(normalized, 0, newRow)
if _, found := slices.BinarySearch(dupHorizontal, y); found {
normalized = slices.Insert(normalized, 0, newRow)
}
}

return normalized
}

func findBlankLines(matrix [][]string) ([]int, []int) {
var dupVertical, dupHorizontal []int
for x := 0; x < len(matrix[0]); x++ {
dup := true
for y := 0; y < len(matrix); y++ {
Expand All @@ -118,31 +183,18 @@ func normalizeMatrix(matrix [][]string) [][]string {
}

if dup {
dVertical = append(dVertical, x)
dupVertical = append(dupVertical, x)
}
}
for y, row := range matrix {
if !slices.Contains(row, "#") {
dHorizonatl = append(dHorizonatl, y)
}
}
slices.Sort(dVertical)
slices.Sort(dHorizonatl)

for y := len(matrix) - 1; y >= 0; y-- {
newRow := make([]string, len(matrix[0]), len(matrix[0])+len(dVertical))
copy(newRow, matrix[y])
for x := len(dVertical) - 1; x >= 0; x-- {
newRow = slices.Insert(newRow, dVertical[x], ".")
}

normalized = slices.Insert(normalized, 0, newRow)
if _, found := slices.BinarySearch(dHorizonatl, y); found {
normalized = slices.Insert(normalized, 0, newRow)
dupHorizontal = append(dupHorizontal, y)
}
}
slices.Sort(dupVertical)
slices.Sort(dupHorizontal)

return normalized
return dupVertical, dupHorizontal
}

func printMatrix(matrix [][]string) {
Expand Down
15 changes: 15 additions & 0 deletions day11/day11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ func TestSolution1(t *testing.T) {
}
}
}

func TestSolution2(t *testing.T) {
cases := map[string]int{
"test_input21": 4000000 + 2,
}

for inputFile, expectedResult := range cases {
result := Solution2(inputFile)
if result != expectedResult {
t.Fatalf("Solution2() = %d, expecting %d\n", result, expectedResult)
} else {
fmt.Printf("Solution2() = %d, OK\n", result)
}
}
}
3 changes: 3 additions & 0 deletions day11/test_input21
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#.......
........
....#...

0 comments on commit 5dadb6a

Please sign in to comment.