-
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
773a397
commit d7c4663
Showing
4 changed files
with
189 additions
and
0 deletions.
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,154 @@ | ||
package day11 | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"os" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
func Solution1(filepath string) int { | ||
matrix := parseInputMatrix(filepath) | ||
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 | ||
} | ||
|
||
func naiveDistCalc(x1, y1, x2, y2 int) int { | ||
dist := 0 | ||
|
||
for x1 != x2 { | ||
if x1 > x2 { | ||
x1-- | ||
} else { | ||
x1++ | ||
} | ||
dist++ | ||
} | ||
|
||
for y1 != y2 { | ||
if y1 > y2 { | ||
y1-- | ||
} else { | ||
y1++ | ||
} | ||
dist++ | ||
} | ||
|
||
return dist | ||
} | ||
|
||
func makePairs(galaxies [][2]int) [][4]int { | ||
var coordPairs [][4]int | ||
|
||
for i := 0; i < len(galaxies); i++ { | ||
for j := i + 1; j < len(galaxies); j++ { | ||
newPair := [4]int{ | ||
galaxies[i][0], | ||
galaxies[i][1], | ||
galaxies[j][0], | ||
galaxies[j][1], | ||
} | ||
coordPairs = append(coordPairs, newPair) | ||
} | ||
} | ||
|
||
return coordPairs | ||
} | ||
|
||
func findGalaxies(matrix *[][]string) [][2]int { | ||
m := *matrix | ||
var gCoords [][2]int | ||
|
||
for y, row := range m { | ||
for x, el := range row { | ||
if el == "#" { | ||
gCoords = append(gCoords, [2]int{y, x}) | ||
} | ||
} | ||
} | ||
|
||
return gCoords | ||
} | ||
|
||
func parseInputMatrix(filepath string) [][]string { | ||
file, err := os.Open(filepath) | ||
if err != nil { | ||
log.Fatalf("Failed to open the input file with: %v\n", err.Error()) | ||
} | ||
|
||
var matrix [][]string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
matrix = append(matrix, strings.Split(scanner.Text(), "")) | ||
} | ||
if err = scanner.Err(); err != nil { | ||
log.Fatalf("Error during reading the input file: %v\n", err.Error()) | ||
} | ||
|
||
return normalizeMatrix(matrix) | ||
} | ||
|
||
func normalizeMatrix(matrix [][]string) [][]string { | ||
var normalized [][]string | ||
|
||
var dVertical, dHorizonatl []int | ||
for x := 0; x < len(matrix[0]); x++ { | ||
dup := true | ||
for y := 0; y < len(matrix); y++ { | ||
if matrix[y][x] == "#" { | ||
dup = false | ||
} | ||
} | ||
|
||
if dup { | ||
dVertical = append(dVertical, 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) | ||
} | ||
} | ||
|
||
return normalized | ||
} | ||
|
||
func printMatrix(matrix [][]string) { | ||
fmt.Println("----------MATRIX START----------") | ||
for _, mRow := range matrix { | ||
fmt.Println(mRow) | ||
} | ||
fmt.Println("-----------MATRIX END----------") | ||
} |
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,21 @@ | ||
package day11 | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestSolution1(t *testing.T) { | ||
cases := map[string]int{ | ||
"test_input11": 374, | ||
} | ||
|
||
for inputFile, expectedResult := range cases { | ||
result := Solution1(inputFile) | ||
if result != expectedResult { | ||
t.Fatalf("Solution1() = %d, expecting %d\n", result, expectedResult) | ||
} else { | ||
fmt.Printf("Solution1() = %d, OK\n", result) | ||
} | ||
} | ||
} |
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 @@ | ||
...#...... | ||
.......#.. | ||
#......... | ||
.......... | ||
......#... | ||
.#........ | ||
.........# | ||
.......... | ||
.......#.. | ||
#...#..... |
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