package main import ( "bufio" "fmt" "os" "strconv" "time" ) func mustAtoi(line string) int { i, _ := strconv.Atoi(line) return i } func main() { start := time.Now() partOne() duration := time.Since(start) partTwo() duration2 := time.Since(start) fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration) } func makeScanner(test bool) *bufio.Scanner { var f *os.File if test { f, _ = os.Open("inputs/testinput") } else { f, _ = os.Open("inputs/input") } reader := bufio.NewReader(f) return bufio.NewScanner(reader) } type intCodeProgram struct { program []int } func parseIntCodeInput(line string) intCodeProgram { program := intCodeProgram{ program: make([]int, 0), } acc := []byte{} for _, v := range line { switch v { case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': acc = append(acc, byte(v)) case ',': program.program = append(program.program, mustAtoi(string(acc))) acc = []byte{} } } program.program = append(program.program, mustAtoi(string(acc))) return program } func runIntCodeProgram(p intCodeProgram) intCodeProgram { pos := 0 for { switch p.program[pos] { case 1: sum := p.program[p.program[pos+1]] + p.program[p.program[pos+2]] p.program[p.program[pos+3]] = sum pos += 4 case 2: mul := p.program[p.program[pos+1]] * p.program[p.program[pos+2]] p.program[p.program[pos+3]] = mul pos += 4 case 99: return p } } } func partOne() { scanner := makeScanner(false) scanner.Scan() prog := parseIntCodeInput(scanner.Text()) prog.program[1] = 12 prog.program[2] = 2 fmt.Println(runIntCodeProgram(prog).program[0]) } func partTwo() { scanner := makeScanner(false) scanner.Scan() prog := parseIntCodeInput(scanner.Text()) possibleInputs := len(prog.program) for i := 0; i < possibleInputs; i++ { for j := 0; j < possibleInputs; j++ { thisRun := intCodeProgram{ program: make([]int, len(prog.program)), } copy(thisRun.program, prog.program) thisRun.program[1] = i thisRun.program[2] = j if runIntCodeProgram(thisRun).program[0] == 19690720 { fmt.Println(100*i + j) } } } }