Day 10
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"strconv"
|
||||
"bufio"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
||||
|
||||
95
day10/common/cpu.go
Normal file
95
day10/common/cpu.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
||||
type CPU struct {
|
||||
registers map[string]int
|
||||
}
|
||||
|
||||
func NewCPU() *CPU {
|
||||
return &CPU{map[string]int{"X": 1}}
|
||||
}
|
||||
|
||||
func (cpu CPU) GetRegisterValue(r string) int {
|
||||
return cpu.registers[r]
|
||||
}
|
||||
|
||||
|
||||
type Instruction interface {
|
||||
ExecuteCycle(*CPU) bool
|
||||
}
|
||||
|
||||
|
||||
type NoOp struct {
|
||||
currentCycle int
|
||||
}
|
||||
|
||||
func NewNoOp() *NoOp {
|
||||
return &NoOp{}
|
||||
}
|
||||
|
||||
func (instruction *NoOp) ExecuteCycle(cpu *CPU) bool {
|
||||
instruction.currentCycle++
|
||||
return instruction.currentCycle == 1
|
||||
}
|
||||
|
||||
|
||||
type AddX struct {
|
||||
parameter int
|
||||
currentCycle int
|
||||
}
|
||||
|
||||
func NewAddX(parameter int) *AddX {
|
||||
return &AddX{parameter: parameter, currentCycle: 0}
|
||||
}
|
||||
|
||||
func (instruction *AddX) ExecuteCycle(cpu *CPU) bool {
|
||||
instruction.currentCycle++
|
||||
if instruction.currentCycle == 2 {
|
||||
cpu.registers["X"] += instruction.parameter
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type Program struct {
|
||||
instructions []Instruction
|
||||
currentInstruction int
|
||||
}
|
||||
|
||||
func (program *Program) ExecuteCycles(cpu *CPU, nCycles int) bool {
|
||||
for i := 0; i < nCycles && program.currentInstruction < len(program.instructions); i++ {
|
||||
done := program.instructions[program.currentInstruction].ExecuteCycle(cpu)
|
||||
if done {
|
||||
program.currentInstruction++
|
||||
}
|
||||
}
|
||||
return program.currentInstruction == len(program.instructions)
|
||||
}
|
||||
|
||||
|
||||
func Parse(scanner bufio.Scanner) Program {
|
||||
program := Program{}
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
|
||||
var instruction Instruction
|
||||
switch line[0:4] {
|
||||
case "noop":
|
||||
instruction = NewNoOp()
|
||||
case "addx":
|
||||
value, _ := strconv.Atoi(line[5:])
|
||||
instruction = NewAddX(value)
|
||||
}
|
||||
program.instructions = append(program.instructions, instruction)
|
||||
}
|
||||
|
||||
return program
|
||||
}
|
||||
23
day10/ex1/main.go
Normal file
23
day10/ex1/main.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"aoc2022/day10/common"
|
||||
)
|
||||
|
||||
func main() {
|
||||
program := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||
cpu := common.NewCPU()
|
||||
strength := 0
|
||||
|
||||
program.ExecuteCycles(cpu, 19)
|
||||
strength += cpu.GetRegisterValue("X") * 20
|
||||
for i := 1; i <= 5; i ++ {
|
||||
program.ExecuteCycles(cpu, 40)
|
||||
strength += cpu.GetRegisterValue("X") * (20 + i*40)
|
||||
}
|
||||
|
||||
fmt.Println(strength)
|
||||
}
|
||||
34
day10/ex2/main.go
Normal file
34
day10/ex2/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"aoc2022/day10/common"
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
program := common.Parse(*bufio.NewScanner(os.Stdin))
|
||||
cpu := common.NewCPU()
|
||||
screen := [240]rune{}
|
||||
i := 0
|
||||
done := false
|
||||
|
||||
for !done {
|
||||
xValue := cpu.GetRegisterValue("X")
|
||||
if xValue >= (i % 40) - 1 && xValue <= (i % 40) + 1 {
|
||||
screen[i] = '#'
|
||||
} else {
|
||||
screen[i] = '.'
|
||||
}
|
||||
done = program.ExecuteCycles(cpu, 1)
|
||||
i = (i + 1) % 240
|
||||
}
|
||||
|
||||
for i := 0; i < 6; i++ {
|
||||
for j := 0; j < 40; j++ {
|
||||
fmt.Print(string(screen[i*40+j]))
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user