mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +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)
|
||||
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
|
||||
8 | Space Image Format | 3D Array manipulation
|
||||
|
||||
+7
-29
@@ -1,19 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
pixelString := readInputFile("./input.txt")
|
||||
// fmt.Println(pixelString)
|
||||
input := util.ReadFile("../input.txt")
|
||||
|
||||
charsSlice := strings.Split(pixelString, "")
|
||||
// fmt.Println(charsSlice)
|
||||
charsSlice := strings.Split(input, "")
|
||||
|
||||
layers := makeLayers(charsSlice)
|
||||
|
||||
@@ -25,24 +21,6 @@ func main() {
|
||||
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 {
|
||||
layers := make([][]string, 0)
|
||||
// layers are 25x6 = 150 characters
|
||||
@@ -50,7 +28,7 @@ func makeLayers(charsSlice []string) [][]string {
|
||||
for i := 0; i*150 < len(charsSlice); i++ {
|
||||
layers = append(layers, charsSlice[150*i:150*(i+1)])
|
||||
}
|
||||
// fmt.Println(layers)
|
||||
|
||||
return layers
|
||||
}
|
||||
|
||||
@@ -66,7 +44,6 @@ func getMinIndex(layers [][]string) int {
|
||||
// update min and minIndex if countZeroes is less than the min value
|
||||
if countZeroes < min {
|
||||
min, minIndex = countZeroes, index
|
||||
// fmt.Println(min, minIndex)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,9 +54,10 @@ func countOnesAndTwos(layer []string) (int, int) {
|
||||
ones, twos := 0, 0
|
||||
// count ones and twos of the layer with the least zeroes
|
||||
for _, pixel := range layer {
|
||||
if pixel == "1" {
|
||||
switch pixel {
|
||||
case "1":
|
||||
ones++
|
||||
} else if pixel == "2" {
|
||||
case "2":
|
||||
twos++
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
+9
-36
@@ -1,24 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
pixelString := readInputFile("./input.txt")
|
||||
// fmt.Println(pixelString)
|
||||
|
||||
// read the input file and put it into a slice for each character
|
||||
pixelString := util.ReadFile("../input.txt")
|
||||
charsSlice := strings.Split(pixelString, "")
|
||||
// fmt.Println(charsSlice)
|
||||
|
||||
// make the characers into a 2D slice divided into layers
|
||||
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)
|
||||
for i := 0; i < 150; i++ {
|
||||
final = append(final, "2")
|
||||
@@ -35,34 +32,10 @@ func main() {
|
||||
|
||||
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]
|
||||
|
||||
// return the final image, flattened
|
||||
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)
|
||||
// Print the six lines (25 characters at a time) individually so the word is legible
|
||||
for i := 0; i < 6; i++ {
|
||||
fmt.Println(finalString[i*25 : (i+1)*25])
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
pixelString = line
|
||||
}
|
||||
|
||||
return pixelString
|
||||
}
|
||||
|
||||
func makeLayers(charsSlice []string) [][]string {
|
||||
|
||||
Reference in New Issue
Block a user