feat: add part one solution
This commit is contained in:
67
internal/2020/DayEight/code.go
Normal file
67
internal/2020/DayEight/code.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package dayeight
|
||||
|
||||
import (
|
||||
"advent-of-code/internal/registry"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type instruction struct {
|
||||
operation string
|
||||
argument int
|
||||
}
|
||||
|
||||
func init() {
|
||||
registry.Register("2020D8", ParseInput, PartOne, PartTwo)
|
||||
}
|
||||
|
||||
func ParseInput(filepath string) []string {
|
||||
content, _ := os.ReadFile(filepath)
|
||||
return strings.Split(string(content), "\n")
|
||||
}
|
||||
|
||||
func parseInstructions(data []string) []instruction {
|
||||
instructions := make([]instruction, 0, len(data))
|
||||
for _, line := range data {
|
||||
line = strings.TrimSpace(line)
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
arg, _ := strconv.Atoi(parts[1])
|
||||
instructions = append(instructions, instruction{operation: parts[0], argument: arg})
|
||||
}
|
||||
return instructions
|
||||
}
|
||||
|
||||
func PartOne(data []string) int {
|
||||
instructions := parseInstructions(data)
|
||||
visited := make(map[int]bool)
|
||||
accumulator := 0
|
||||
pc := 0
|
||||
|
||||
for pc < len(instructions) {
|
||||
if visited[pc] {
|
||||
return accumulator
|
||||
}
|
||||
visited[pc] = true
|
||||
|
||||
instruction := instructions[pc]
|
||||
switch instruction.operation {
|
||||
case "acc":
|
||||
accumulator += instruction.argument
|
||||
pc++
|
||||
case "jmp":
|
||||
pc += instruction.argument
|
||||
case "nop":
|
||||
pc++
|
||||
}
|
||||
}
|
||||
|
||||
return accumulator
|
||||
}
|
||||
|
||||
func PartTwo(data []string) int {
|
||||
return 0
|
||||
}
|
||||
Reference in New Issue
Block a user