diff --git a/day09/common/bridge.go b/day09/common/bridge.go index b861063..b2d5747 100644 --- a/day09/common/bridge.go +++ b/day09/common/bridge.go @@ -1,8 +1,8 @@ package common import ( - "bufio" - "strconv" + "bufio" + "strconv" ) diff --git a/day10/common/cpu.go b/day10/common/cpu.go new file mode 100644 index 0000000..1b01932 --- /dev/null +++ b/day10/common/cpu.go @@ -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 +} diff --git a/day10/ex1/main.go b/day10/ex1/main.go new file mode 100644 index 0000000..c31c896 --- /dev/null +++ b/day10/ex1/main.go @@ -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) +} diff --git a/day10/ex2/main.go b/day10/ex2/main.go new file mode 100644 index 0000000..4282fb2 --- /dev/null +++ b/day10/ex2/main.go @@ -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() + } +}