57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"AOC2022/helper"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
type Chip struct {
|
||
|
activeValue int
|
||
|
currentcycle int
|
||
|
signalStrenght int
|
||
|
CRT [240]rune
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
args := os.Args[1:]
|
||
|
lines := helper.ReadTextFile(args[0])
|
||
|
chip := Chip{1, 1, 0, [240]rune{}}
|
||
|
for _, line := range lines {
|
||
|
command := strings.Split(line, " ")
|
||
|
switch command[0] {
|
||
|
case "addx":
|
||
|
chip.increaseCurrentCyle()
|
||
|
chip.increaseCurrentCyle()
|
||
|
chip.activeValue += helper.RemoveError(strconv.Atoi(command[1]))
|
||
|
case "noop":
|
||
|
chip.increaseCurrentCyle()
|
||
|
}
|
||
|
}
|
||
|
fmt.Println(chip.signalStrenght)
|
||
|
chip.pringCRT()
|
||
|
}
|
||
|
|
||
|
func (chip *Chip) increaseCurrentCyle() {
|
||
|
fmt.Printf("%v : %v \n", chip.currentcycle, chip.activeValue)
|
||
|
spritePos := chip.activeValue
|
||
|
if helper.Contains([]int{spritePos - 1, spritePos, spritePos + 1}, (chip.currentcycle-1)%40) {
|
||
|
chip.CRT[chip.currentcycle-1] = '#'
|
||
|
} else {
|
||
|
chip.CRT[chip.currentcycle-1] = '.'
|
||
|
}
|
||
|
if chip.currentcycle == 20 || (chip.currentcycle-20)%40 == 0 {
|
||
|
chip.signalStrenght += chip.activeValue * chip.currentcycle
|
||
|
}
|
||
|
chip.currentcycle++
|
||
|
}
|
||
|
func (chip *Chip) pringCRT() {
|
||
|
j := 0
|
||
|
for i := 0; i <= 240; i += 40 {
|
||
|
fmt.Println(string(chip.CRT[j:i]))
|
||
|
j = i
|
||
|
}
|
||
|
}
|