✨ Finish Part 1 & Part 2 from Day 8
This commit is contained in:
parent
da97805203
commit
0fbbe517c6
1
day8/input.txt
Normal file
1
day8/input.txt
Normal file
File diff suppressed because one or more lines are too long
153
day8/main.go
Normal file
153
day8/main.go
Normal file
@ -0,0 +1,153 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"math"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Layer struct {
|
||||
Data [][]int
|
||||
}
|
||||
|
||||
type Image struct {
|
||||
layers []Layer
|
||||
}
|
||||
|
||||
func main() {
|
||||
file, err := os.Open("input.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
if !scanner.Scan() {
|
||||
log.Fatalln("Problem while read line.")
|
||||
return
|
||||
}
|
||||
|
||||
stringarr := strings.Split(scanner.Text(), "")
|
||||
t2 := make([]int, 0)
|
||||
|
||||
for _, i := range stringarr {
|
||||
j, err := strconv.Atoi(i)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t2 = append(t2, j)
|
||||
}
|
||||
|
||||
SolvePuzzle1(t2)
|
||||
SolvePuzzle2(t2)
|
||||
}
|
||||
|
||||
func SolvePuzzle1(pixels []int) {
|
||||
image := ParseImage(pixels, 25, 6)
|
||||
|
||||
fewestZeroCount := math.MaxInt32
|
||||
fewestZeroLayer := -1
|
||||
for index, layer := range image.layers {
|
||||
count := CountNumberInLayer(0, layer)
|
||||
|
||||
if count < fewestZeroCount {
|
||||
fewestZeroCount = count
|
||||
fewestZeroLayer = index
|
||||
}
|
||||
}
|
||||
|
||||
count1 := CountNumberInLayer(1, image.layers[fewestZeroLayer])
|
||||
count2 := CountNumberInLayer(2, image.layers[fewestZeroLayer])
|
||||
|
||||
log.Println("Part1: " + strconv.Itoa(count1*count2))
|
||||
}
|
||||
|
||||
func SolvePuzzle2(pixels []int) {
|
||||
image := ParseImage(pixels, 25, 6)
|
||||
layer := WrapImageLayers(image)
|
||||
log.Println("Part2: ")
|
||||
WriteLayerImageToConsole(layer)
|
||||
}
|
||||
|
||||
func WrapImageLayers(image Image) Layer {
|
||||
layer := Layer{}
|
||||
layer.Data = make([][]int, 0)
|
||||
for i, line := range image.layers[0].Data {
|
||||
layer.Data = append(layer.Data, make([]int, 0))
|
||||
for range line {
|
||||
layer.Data[i] = append(layer.Data[i], 2)
|
||||
}
|
||||
}
|
||||
|
||||
for _, inLayer := range image.layers {
|
||||
for i, line := range inLayer.Data {
|
||||
for j, _ := range line {
|
||||
num := inLayer.Data[i][j]
|
||||
|
||||
if layer.Data[i][j] == 2 {
|
||||
layer.Data[i][j] = num
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
return layer
|
||||
}
|
||||
|
||||
func CountNumberInLayer(number int, layer Layer) int {
|
||||
sum := 0
|
||||
for i := 0; i < len(layer.Data); i++ {
|
||||
for j := 0; j < len(layer.Data[i]); j++ {
|
||||
if layer.Data[i][j] == number {
|
||||
sum++
|
||||
}
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func WriteImageToConsole(image Image) {
|
||||
for index, layer := range image.layers {
|
||||
log.Printf("Layer %d:", index)
|
||||
|
||||
for _, line := range layer.Data {
|
||||
lineStr := strings.Join(strings.Fields(fmt.Sprint(line)), "")
|
||||
log.Println(lineStr)
|
||||
}
|
||||
log.Println("")
|
||||
}
|
||||
}
|
||||
|
||||
func WriteLayerImageToConsole(layer Layer) {
|
||||
for _, line := range layer.Data {
|
||||
lineStr := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(line)), " "), "[]")
|
||||
log.Println(lineStr)
|
||||
}
|
||||
}
|
||||
|
||||
func ParseImage(pixels []int, wide int, tall int) Image {
|
||||
image := Image{layers: make([]Layer, 0)}
|
||||
for i := 0; i < len(pixels); i += wide * tall {
|
||||
layer := ParseLayer(pixels[i:(i+wide*tall)], wide, tall)
|
||||
image.layers = append(image.layers, layer)
|
||||
}
|
||||
return image
|
||||
}
|
||||
|
||||
func ParseLayer(pixels []int, wide int, tall int) Layer {
|
||||
index := 0
|
||||
layer := Layer{Data: make([][]int, 0)}
|
||||
for i := 0; i < tall; i++ {
|
||||
layer.Data = append(layer.Data, make([]int, 0))
|
||||
for j := 0; j < wide; j++ {
|
||||
layer.Data[i] = append(layer.Data[i], pixels[index])
|
||||
|
||||
index++
|
||||
}
|
||||
}
|
||||
return layer
|
||||
}
|
Loading…
Reference in New Issue
Block a user