mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
day08, solution not modified much
This commit is contained in:
@@ -13,3 +13,4 @@ Day | Name | Type of Algo & Notes
|
|||||||
5 | Sunny with a Chance of Asteroids | - Yay more Intcode!........ <br> - This gave me fits... <br> - Good application for recursion (in my opinion)
|
5 | Sunny with a Chance of Asteroids | - Yay more Intcode!........ <br> - This gave me fits... <br> - Good application for recursion (in my opinion)
|
||||||
6 | Universal Orbit Map | - __Tree traversal__ and depth calculations. It's not quite a Graph, but it has a __directed graph__ algo feel too
|
6 | Universal Orbit Map | - __Tree traversal__ and depth calculations. It's not quite a Graph, but it has a __directed graph__ algo feel too
|
||||||
7 | Amplification Circuit | - More Intcode... Piping together multiple Intcode computers 😳😳😳 <br> - Refactored Intcode computer to an OOP approach so a single computer maintains its data <br> - Also requires making __permutations generator__ <br> - Some gymnastics to make this circular, but its easier with this OOP approach and the "objects"/instances of a struct maintaining their own data
|
7 | Amplification Circuit | - More Intcode... Piping together multiple Intcode computers 😳😳😳 <br> - Refactored Intcode computer to an OOP approach so a single computer maintains its data <br> - Also requires making __permutations generator__ <br> - Some gymnastics to make this circular, but its easier with this OOP approach and the "objects"/instances of a struct maintaining their own data
|
||||||
|
8 | Space Image Format | 3D Array manipulation
|
||||||
|
|||||||
+7
-29
@@ -1,19 +1,15 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"adventofcode/util"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pixelString := readInputFile("./input.txt")
|
input := util.ReadFile("../input.txt")
|
||||||
// fmt.Println(pixelString)
|
|
||||||
|
|
||||||
charsSlice := strings.Split(pixelString, "")
|
charsSlice := strings.Split(input, "")
|
||||||
// fmt.Println(charsSlice)
|
|
||||||
|
|
||||||
layers := makeLayers(charsSlice)
|
layers := makeLayers(charsSlice)
|
||||||
|
|
||||||
@@ -25,24 +21,6 @@ func main() {
|
|||||||
fmt.Println(ones * twos)
|
fmt.Println(ones * twos)
|
||||||
}
|
}
|
||||||
|
|
||||||
func readInputFile(path string) string {
|
|
||||||
var pixelString string
|
|
||||||
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
pixelString = line
|
|
||||||
}
|
|
||||||
|
|
||||||
return pixelString
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeLayers(charsSlice []string) [][]string {
|
func makeLayers(charsSlice []string) [][]string {
|
||||||
layers := make([][]string, 0)
|
layers := make([][]string, 0)
|
||||||
// layers are 25x6 = 150 characters
|
// layers are 25x6 = 150 characters
|
||||||
@@ -50,7 +28,7 @@ func makeLayers(charsSlice []string) [][]string {
|
|||||||
for i := 0; i*150 < len(charsSlice); i++ {
|
for i := 0; i*150 < len(charsSlice); i++ {
|
||||||
layers = append(layers, charsSlice[150*i:150*(i+1)])
|
layers = append(layers, charsSlice[150*i:150*(i+1)])
|
||||||
}
|
}
|
||||||
// fmt.Println(layers)
|
|
||||||
return layers
|
return layers
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +44,6 @@ func getMinIndex(layers [][]string) int {
|
|||||||
// update min and minIndex if countZeroes is less than the min value
|
// update min and minIndex if countZeroes is less than the min value
|
||||||
if countZeroes < min {
|
if countZeroes < min {
|
||||||
min, minIndex = countZeroes, index
|
min, minIndex = countZeroes, index
|
||||||
// fmt.Println(min, minIndex)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,9 +54,10 @@ func countOnesAndTwos(layer []string) (int, int) {
|
|||||||
ones, twos := 0, 0
|
ones, twos := 0, 0
|
||||||
// count ones and twos of the layer with the least zeroes
|
// count ones and twos of the layer with the least zeroes
|
||||||
for _, pixel := range layer {
|
for _, pixel := range layer {
|
||||||
if pixel == "1" {
|
switch pixel {
|
||||||
|
case "1":
|
||||||
ones++
|
ones++
|
||||||
} else if pixel == "2" {
|
case "2":
|
||||||
twos++
|
twos++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
+9
-36
@@ -1,24 +1,21 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"adventofcode/util"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pixelString := readInputFile("./input.txt")
|
// read the input file and put it into a slice for each character
|
||||||
// fmt.Println(pixelString)
|
pixelString := util.ReadFile("../input.txt")
|
||||||
|
|
||||||
charsSlice := strings.Split(pixelString, "")
|
charsSlice := strings.Split(pixelString, "")
|
||||||
// fmt.Println(charsSlice)
|
|
||||||
|
|
||||||
|
// make the characers into a 2D slice divided into layers
|
||||||
layers := makeLayers(charsSlice)
|
layers := makeLayers(charsSlice)
|
||||||
|
|
||||||
// fmt.Println(layers)
|
// for each pixel (all layers combined), iterate through all layers of the pixel
|
||||||
|
// iterate from the top layer until a 1 or 0 is found, set it to that value
|
||||||
final := make([]string, 0)
|
final := make([]string, 0)
|
||||||
for i := 0; i < 150; i++ {
|
for i := 0; i < 150; i++ {
|
||||||
final = append(final, "2")
|
final = append(final, "2")
|
||||||
@@ -35,34 +32,10 @@ func main() {
|
|||||||
|
|
||||||
finalString := strings.Join(final, "")
|
finalString := strings.Join(final, "")
|
||||||
|
|
||||||
line1, line2, line3, line4, line5, line6 := finalString[0:25], finalString[25:50], finalString[50:75], finalString[75:100], finalString[100:125], finalString[125:150]
|
// Print the six lines (25 characters at a time) individually so the word is legible
|
||||||
|
for i := 0; i < 6; i++ {
|
||||||
// return the final image, flattened
|
fmt.Println(finalString[i*25 : (i+1)*25])
|
||||||
fmt.Println(line1)
|
|
||||||
fmt.Println(line2)
|
|
||||||
fmt.Println(line3)
|
|
||||||
fmt.Println(line4)
|
|
||||||
fmt.Println(line5)
|
|
||||||
fmt.Println(line6)
|
|
||||||
}
|
|
||||||
|
|
||||||
// helper functions
|
|
||||||
func readInputFile(path string) string {
|
|
||||||
var pixelString string
|
|
||||||
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
for scanner.Scan() {
|
|
||||||
line := scanner.Text()
|
|
||||||
pixelString = line
|
|
||||||
}
|
|
||||||
|
|
||||||
return pixelString
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeLayers(charsSlice []string) [][]string {
|
func makeLayers(charsSlice []string) [][]string {
|
||||||
|
|||||||
Reference in New Issue
Block a user