mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
2015-day23: an actually easy assembly computer
This commit is contained in:
+50
-25
@@ -15,32 +15,57 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Println("Running part", part)
|
fmt.Println("Running part", part)
|
||||||
|
|
||||||
if part == 1 {
|
ans := simpleAssemblyComputer(util.ReadFile("./input.txt"), part)
|
||||||
ans := part1(util.ReadFile("./input.txt"))
|
fmt.Println("Output:", ans)
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
}
|
||||||
fmt.Println("Output:", ans)
|
|
||||||
} else {
|
func simpleAssemblyComputer(input string, part int) int {
|
||||||
ans := part2(util.ReadFile("./input.txt"))
|
instructions := strings.Split(input, "\n")
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
var index int
|
||||||
fmt.Println("Output:", ans)
|
|
||||||
|
registers := map[string]int{}
|
||||||
|
if part == 2 {
|
||||||
|
registers["a"] = 1
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func part1(input string) int {
|
for index < len(instructions) {
|
||||||
parsed := parseInput(input)
|
parts := strings.Split(instructions[index], " ")
|
||||||
_ = parsed
|
switch parts[0] {
|
||||||
|
case "hlf":
|
||||||
return 0
|
reg := parts[1]
|
||||||
}
|
registers[reg] /= 2
|
||||||
|
index++
|
||||||
func part2(input string) int {
|
case "tpl":
|
||||||
return 0
|
reg := parts[1]
|
||||||
}
|
registers[reg] *= 3
|
||||||
|
index++
|
||||||
func parseInput(input string) (ans []int) {
|
case "inc":
|
||||||
lines := strings.Split(input, "\n")
|
reg := parts[1]
|
||||||
for _, l := range lines {
|
registers[reg]++
|
||||||
ans = append(ans, cast.ToInt(l))
|
index++
|
||||||
|
case "jmp":
|
||||||
|
diff := cast.ToInt(parts[1])
|
||||||
|
index += diff
|
||||||
|
case "jie":
|
||||||
|
reg := strings.Trim(parts[1], ",")
|
||||||
|
diff := cast.ToInt(parts[2])
|
||||||
|
if registers[reg]%2 == 0 {
|
||||||
|
index += diff
|
||||||
|
} else {
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
case "jio":
|
||||||
|
reg := strings.Trim(parts[1], ",")
|
||||||
|
diff := cast.ToInt(parts[2])
|
||||||
|
if registers[reg] == 1 {
|
||||||
|
index += diff
|
||||||
|
} else {
|
||||||
|
index++
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("unhandled instruction type: " + parts[0])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ans
|
|
||||||
|
return registers["b"]
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-21
@@ -2,37 +2,24 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_part1(t *testing.T) {
|
func Test_simpleAssemblyComputer(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
|
part int
|
||||||
want int
|
want int
|
||||||
}{
|
}{
|
||||||
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
|
{"actual", util.ReadFile("input.txt"), 1, 307},
|
||||||
|
{"actual", util.ReadFile("input.txt"), 2, 160},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := part1(tt.input); got != tt.want {
|
if got := simpleAssemblyComputer(tt.input, tt.part); got != tt.want {
|
||||||
t.Errorf("part1() = %v, want %v", got, tt.want)
|
t.Errorf("simpleAssemblyComputer() = %v, want %v", got, tt.want)
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_part2(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
want int
|
|
||||||
}{
|
|
||||||
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if got := part2(tt.input); got != tt.want {
|
|
||||||
t.Errorf("part2() = %v, want %v", got, tt.want)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user