aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorj-james2020-06-23 18:35:58 +0000
committerj-james2020-06-23 18:35:58 +0000
commitcaeff74a34725631f737a78cc0a6e06b03f457a4 (patch)
tree1fa72bd87ba4524c63748e8158b0613b78e9c0c3
parentfd70759166d6da2c65622ec7dff80390f6d17205 (diff)
Day Two
-rw-r--r--LICENSE22
-rw-r--r--two.go66
2 files changed, 88 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..43eabfb
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+MIT License
+
+Copyright (C) 2020 j-james <https://j-james.me>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/two.go b/two.go
new file mode 100644
index 0000000..8108c87
--- /dev/null
+++ b/two.go
@@ -0,0 +1,66 @@
+package main
+
+import (
+ "fmt"
+ "io/ioutil"
+ "os"
+ "strconv"
+)
+
+func main() {
+ var slice []int
+ start, size := 0, 0
+ if len(os.Args) < 2 {
+ panic("runtime error: missing operand")
+ }
+ file, err := ioutil.ReadFile(os.Args[1])
+ if err != nil {
+ panic(err)
+ }
+ for i := 0; i < len(file); i++ {
+ if file[i] == ',' || file[i] == '\n' {
+ arg, err := strconv.Atoi(string(file[start:i])) // i-1??
+ if err != nil {
+ panic(err)
+ }
+ slice = append(slice, arg)
+ start = i + 1
+ size++
+ }
+ }
+ noun, verb := edocpo(slice, 19690720)
+ slice[1] = 12
+ slice[2] = 2
+ fmt.Println(opcode(slice)[0])
+ fmt.Println(100*noun + verb)
+}
+
+func opcode(slice []int) []int {
+ for i := 0; i < len(slice); i += 4 {
+ if slice[i] == 1 {
+ slice[slice[i+3]] = slice[slice[i+1]] + slice[slice[i+2]]
+ } else if slice[i] == 2 {
+ slice[slice[i+3]] = slice[slice[i+1]] * slice[slice[i+2]]
+ } else if slice[i] == 99 {
+ return slice
+ } else {
+ fmt.Println("Unsupported code", slice[i], "at", i)
+ os.Exit(0)
+ }
+ }
+ return slice
+}
+
+func edocpo(slice []int, output int) (int, int) {
+ var ecils []int
+ for i := 0; i < len(slice); i++ {
+ for j := 0; j < len(slice); j++ {
+ ecils = append([]int(nil), slice...) // reset ecils to slice
+ ecils[1], ecils[2] = i, j
+ if opcode(ecils)[0] == output {
+ return i, j
+ }
+ }
+ }
+ return -1, -1
+}