2018 day 19 complete
This commit is contained in:
		@@ -12,19 +12,19 @@ import (
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	input := stdinToStringSlice()
 | 
			
		||||
	part1(input)
 | 
			
		||||
	//part1(input)
 | 
			
		||||
	part2(input)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func part1(input []string) {
 | 
			
		||||
	vpc := NewState()
 | 
			
		||||
	vpc.reg[0] = 1
 | 
			
		||||
	if strings.HasPrefix(input[0], "#ip") {
 | 
			
		||||
		// Binding the ip to a register...
 | 
			
		||||
		pts := strings.Split(input[0], " ")
 | 
			
		||||
		vpc.ipReg = Atoi(pts[1])
 | 
			
		||||
		input = input[1:]
 | 
			
		||||
	}
 | 
			
		||||
	for i := 0; i < 1000000; i++ {
 | 
			
		||||
	for i := 0; i < 200; i++ {
 | 
			
		||||
		if vpc.ip >= len(input) {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
@@ -33,7 +33,9 @@ func part1(input []string) {
 | 
			
		||||
			if vpc.ipReg > -1 {
 | 
			
		||||
				vpc.reg[vpc.ipReg] = vpc.ip
 | 
			
		||||
			}
 | 
			
		||||
			fmt.Print(vpc.string(), " -> ", op, " ", cmd, " -> ")
 | 
			
		||||
			vpc.run(op, cmd)
 | 
			
		||||
			fmt.Println(vpc.string())
 | 
			
		||||
			vpc.ip = vpc.reg[vpc.ipReg]
 | 
			
		||||
		}
 | 
			
		||||
		vpc.ip++
 | 
			
		||||
@@ -41,6 +43,44 @@ func part1(input []string) {
 | 
			
		||||
	fmt.Println(vpc.string())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func part2(input []string) {
 | 
			
		||||
	vpc := NewState()
 | 
			
		||||
	vpc.reg[0] = 1
 | 
			
		||||
	if strings.HasPrefix(input[0], "#ip") {
 | 
			
		||||
		// Binding the ip to a register...
 | 
			
		||||
		pts := strings.Split(input[0], " ")
 | 
			
		||||
		vpc.ipReg = Atoi(pts[1])
 | 
			
		||||
		input = input[1:]
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for {
 | 
			
		||||
		if vpc.ip >= len(input) {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		if vpc.ip == 3 {
 | 
			
		||||
			// When ip == 3, we kick off the loop.
 | 
			
		||||
			// Looks like it's just summing the divisors of the value in register 2
 | 
			
		||||
			divisors := getDivisors(vpc.reg[2])
 | 
			
		||||
			fmt.Println(divisors)
 | 
			
		||||
			fmt.Println(sum(divisors))
 | 
			
		||||
			break
 | 
			
		||||
		} else {
 | 
			
		||||
			// Normal Execution
 | 
			
		||||
			inp := input[vpc.ip]
 | 
			
		||||
			if op, cmd, err := vpc.parseCommand(inp); err == nil {
 | 
			
		||||
				if vpc.ipReg > -1 {
 | 
			
		||||
					vpc.reg[vpc.ipReg] = vpc.ip
 | 
			
		||||
				}
 | 
			
		||||
				fmt.Print(vpc.string(), " -> ", op, " ", cmd, " -> ")
 | 
			
		||||
				vpc.run(op, cmd)
 | 
			
		||||
				fmt.Println(vpc.string())
 | 
			
		||||
				vpc.ip = vpc.reg[vpc.ipReg]
 | 
			
		||||
			}
 | 
			
		||||
			vpc.ip++
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type State struct {
 | 
			
		||||
	ip, ipReg int
 | 
			
		||||
	reg       [6]int
 | 
			
		||||
@@ -178,6 +218,25 @@ func boolToInt(v bool) int {
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func getDivisors(n int) []int {
 | 
			
		||||
	ret := []int{n}
 | 
			
		||||
	for i := 1; i < (n / 2); i++ {
 | 
			
		||||
		if n%i == 0 {
 | 
			
		||||
			ret = append(ret, i)
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return ret
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func sum(vals []int) int {
 | 
			
		||||
	var ret int
 | 
			
		||||
	for _, v := range vals {
 | 
			
		||||
		ret += v
 | 
			
		||||
	}
 | 
			
		||||
	return ret
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func stdinToStringSlice() []string {
 | 
			
		||||
	var input []string
 | 
			
		||||
	scanner := bufio.NewScanner(os.Stdin)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user