Skip to content

Commit

Permalink
day 2 part 1 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Dec 2, 2019
1 parent d3b5a38 commit 6bb61e6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions day2p1/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,13,1,19,1,19,9,23,1,5,23,27,1,27,9,31,1,6,31,35,2,35,9,39,1,39,6,43,2,9,43,47,1,47,6,51,2,51,9,55,1,5,55,59,2,59,6,63,1,9,63,67,1,67,10,71,1,71,13,75,2,13,75,79,1,6,79,83,2,9,83,87,1,87,6,91,2,10,91,95,2,13,95,99,1,9,99,103,1,5,103,107,2,9,107,111,1,111,5,115,1,115,5,119,1,10,119,123,1,13,123,127,1,2,127,131,1,131,13,0,99,2,14,0,0
47 changes: 47 additions & 0 deletions day2p1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"fmt"
"io"
"os"
"strconv"
"strings"
)

// nolint
var (
in io.Reader = os.Stdin
out io.Writer = os.Stdout
)

func main() {
prog := ""

fmt.Fscanf(in, "%s", &prog)

strs := strings.Split(prog, ",")
opcodes := make([]int, len(strs))

for i, s := range strs {
res, _ := strconv.Atoi(s)
opcodes[i] = res
}

opcodes[1] = 12
opcodes[2] = 2

for pc := 0; opcodes[pc] != 99; pc += 4 {
val1 := opcodes[opcodes[pc+1]]
val2 := opcodes[opcodes[pc+2]]
outPos := opcodes[pc+3]

switch code := opcodes[pc]; code {
case 1:
opcodes[outPos] = val1 + val2
case 2:
opcodes[outPos] = val1 * val2
}
}

fmt.Fprintln(out, opcodes[0])
}
36 changes: 36 additions & 0 deletions day2p1/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"bytes"
"os"
"strings"
"testing"
)

func TestMain(t *testing.T) {
file, _ := os.Open("input.txt")
in = file
buff := new(bytes.Buffer)
out = buff

main()

result := strings.TrimSpace(buff.String())
if expected := "3409710"; expected != result {
t.Errorf("Expected %v, got %v", expected, result)
}

file.Close()
}

func BenchmarkMain(b *testing.B) {
for i := 0; i < b.N; i++ {
file, _ := os.Open("input.txt")
in = file
buff := new(bytes.Buffer)
out = buff

main()
file.Close()
}
}

0 comments on commit 6bb61e6

Please sign in to comment.