Skip to content

Commit

Permalink
Day 23
Browse files Browse the repository at this point in the history
  • Loading branch information
Magiczne committed Nov 11, 2024
1 parent 672e8d6 commit daa2159
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
84 changes: 84 additions & 0 deletions d23/cpu/cpu.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package d23

import (
"aoc2015/util"
)

type Instruction struct {
Name string
Operand1 string
Operand2 string
}

type Processor struct {
registers map[string]int
programCounter int
}

func NewProcessor() Processor {
return Processor{
registers: map[string]int{
"a": 0,
"b": 0,
},
programCounter: 0,
}
}

func (processor *Processor) A() int {
return processor.registers["a"]
}

func (processor *Processor) SetA(value int) {
processor.registers["a"] = value
}

func (processor *Processor) B() int {
return processor.registers["b"]
}

func (processor *Processor) Run(instructions []Instruction) {
maxProgramCounter := len(instructions)

for {
if processor.programCounter >= maxProgramCounter {
return
}

instruction := instructions[processor.programCounter]

switch instruction.Name {
case "hlf":
processor.registers[instruction.Operand1] /= 2
processor.programCounter++

case "tpl":
processor.registers[instruction.Operand1] *= 3
processor.programCounter++

case "inc":
processor.registers[instruction.Operand1] += 1
processor.programCounter++

case "jmp":
processor.programCounter += util.StringToInt(instruction.Operand1)

case "jie":
if processor.registers[instruction.Operand1]%2 == 0 {
processor.programCounter += util.StringToInt(instruction.Operand2)
} else {
processor.programCounter++
}

case "jio":
if processor.registers[instruction.Operand1] == 1 {
processor.programCounter += util.StringToInt(instruction.Operand2)
} else {
processor.programCounter++
}

default:
panic(instruction.Name)
}
}
}
46 changes: 46 additions & 0 deletions d23/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
jio a, +16
inc a
inc a
tpl a
tpl a
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
tpl a
tpl a
inc a
jmp +23
tpl a
inc a
inc a
tpl a
inc a
inc a
tpl a
tpl a
inc a
inc a
tpl a
inc a
tpl a
inc a
tpl a
inc a
inc a
tpl a
inc a
tpl a
tpl a
inc a
jio a, +8
inc b
jie a, +4
tpl a
inc a
jmp +2
hlf a
jmp -7
42 changes: 42 additions & 0 deletions d23/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
d23 "aoc2015/d23/cpu"
"aoc2015/util"
"regexp"
)

func part1(instructions []d23.Instruction) int {
cpu := d23.NewProcessor()
cpu.Run(instructions)

return cpu.B()
}

func part2(instructions []d23.Instruction) int {
cpu := d23.NewProcessor()
cpu.SetA(1)
cpu.Run(instructions)

return cpu.B()
}

func main() {
instructionRegex := regexp.MustCompile(`([a-z]{3}) ([a-z0-9-+]+)(, )?([0-9-+]+)?`)
reader := func(name string) []d23.Instruction {
lines := util.ReadLines(name)

return util.ArrayMap(lines, func(line string) d23.Instruction {
matches := instructionRegex.FindStringSubmatch(line)

return d23.Instruction{
Name: matches[1],
Operand1: matches[2],
Operand2: matches[4],
}
})
}

util.TestRuns("23", reader, part1, part2)
util.SolutionRuns("23", reader, part1, part2)
}
4 changes: 4 additions & 0 deletions d23/test-runs/0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
inc b
jio b, +2
tpl b
inc b

0 comments on commit daa2159

Please sign in to comment.