Skip to content

Commit

Permalink
Day 11: part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-spivakov committed Oct 19, 2024
1 parent 773a397 commit d7c4663
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 0 deletions.
154 changes: 154 additions & 0 deletions day11/day11.go
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----------")
}
21 changes: 21 additions & 0 deletions day11/day11_test.go
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)
}
}
}
10 changes: 10 additions & 0 deletions day11/test_input11
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
...#......
.......#..
#.........
..........
......#...
.#........
.........#
..........
.......#..
#...#.....
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"advent-of-code/day08"
"advent-of-code/day09"
"advent-of-code/day10"
"advent-of-code/day11"
"fmt"
)

Expand Down Expand Up @@ -44,4 +45,7 @@ func main() {

fmt.Printf("Day 10, solution 1: %v\n", day10.Solution1("inputs/day10"))
fmt.Printf("Day 10, solution 2: %v\n", day10.Solution2("inputs/day10"))

fmt.Printf("Day 11, solution 1: %v\n", day11.Solution1("inputs/day11"))
fmt.Printf("Day 11, solution 2: %v\n", day11.Solution2("inputs/day11"))
}

0 comments on commit d7c4663

Please sign in to comment.