Day10
This commit is contained in:
parent
a2670664e3
commit
e43af3b8bc
|
@ -0,0 +1,56 @@
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,144 @@
|
||||||
|
noop
|
||||||
|
addx 33
|
||||||
|
addx -30
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 3
|
||||||
|
addx 3
|
||||||
|
addx -4
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx -38
|
||||||
|
noop
|
||||||
|
addx 16
|
||||||
|
addx -13
|
||||||
|
addx 2
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx -2
|
||||||
|
addx -10
|
||||||
|
addx 17
|
||||||
|
addx -5
|
||||||
|
addx 10
|
||||||
|
noop
|
||||||
|
addx -15
|
||||||
|
addx 16
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx -38
|
||||||
|
addx 7
|
||||||
|
addx -6
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx -20
|
||||||
|
addx 21
|
||||||
|
addx 3
|
||||||
|
addx 1
|
||||||
|
addx -35
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx 31
|
||||||
|
noop
|
||||||
|
addx -26
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -2
|
||||||
|
addx 25
|
||||||
|
addx -18
|
||||||
|
addx -13
|
||||||
|
addx 14
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 6
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx -38
|
||||||
|
addx 24
|
||||||
|
addx -17
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -2
|
||||||
|
addx 31
|
||||||
|
addx -24
|
||||||
|
addx 7
|
||||||
|
addx -10
|
||||||
|
addx 6
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx -2
|
||||||
|
addx -26
|
||||||
|
addx 31
|
||||||
|
addx 5
|
||||||
|
addx -40
|
||||||
|
addx 5
|
||||||
|
addx 33
|
||||||
|
addx -31
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
addx 1
|
||||||
|
addx 4
|
||||||
|
addx 20
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -14
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
|
@ -0,0 +1,146 @@
|
||||||
|
addx 15
|
||||||
|
addx -11
|
||||||
|
addx 6
|
||||||
|
addx -3
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -8
|
||||||
|
addx 13
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -35
|
||||||
|
addx 1
|
||||||
|
addx 24
|
||||||
|
addx -19
|
||||||
|
addx 1
|
||||||
|
addx 16
|
||||||
|
addx -11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 21
|
||||||
|
addx -15
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -3
|
||||||
|
addx 9
|
||||||
|
addx 1
|
||||||
|
addx -3
|
||||||
|
addx 8
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -36
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 6
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -13
|
||||||
|
addx 13
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx -33
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 8
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx -9
|
||||||
|
addx 1
|
||||||
|
addx 1
|
||||||
|
addx -3
|
||||||
|
addx 11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -13
|
||||||
|
addx -19
|
||||||
|
addx 1
|
||||||
|
addx 3
|
||||||
|
addx 26
|
||||||
|
addx -30
|
||||||
|
addx 12
|
||||||
|
addx -1
|
||||||
|
addx 3
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -9
|
||||||
|
addx 18
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 9
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 2
|
||||||
|
addx -37
|
||||||
|
addx 1
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 15
|
||||||
|
addx -21
|
||||||
|
addx 22
|
||||||
|
addx -6
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -10
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 20
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
addx 2
|
||||||
|
addx -6
|
||||||
|
addx -11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
|
@ -0,0 +1,3 @@
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -5
|
|
@ -114,7 +114,7 @@ func (simulation *Simulation) print(size int) {
|
||||||
tempArr += "#"
|
tempArr += "#"
|
||||||
} else if simulation.headPos == pos {
|
} else if simulation.headPos == pos {
|
||||||
tempArr += "H"
|
tempArr += "H"
|
||||||
} else if helper.Contains(simulation.tailPos, pos) {
|
} else if helper.Contains2Int(simulation.tailPos, pos) {
|
||||||
tempArr += "T"
|
tempArr += "T"
|
||||||
} else {
|
} else {
|
||||||
tempArr += "."
|
tempArr += "."
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package helper
|
package helper
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"constraints"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -78,7 +79,16 @@ func Sum[T int | int64](slice []T) (s T) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func Contains[T [2]int](elems []T, v T) bool {
|
func Contains2Int[T [2]int](elems []T, v T) bool {
|
||||||
|
for _, s := range elems {
|
||||||
|
if v == s {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func Contains[T constraints.Ordered](elems []T, v T) bool {
|
||||||
for _, s := range elems {
|
for _, s := range elems {
|
||||||
if v == s {
|
if v == s {
|
||||||
return true
|
return true
|
||||||
|
|
Loading…
Reference in New Issue
Block a user