small update to day12 solution

This commit is contained in:
Alex Chao
2020-08-04 11:50:35 -04:00
parent 307f57d6f9
commit 9f29795353
4 changed files with 51 additions and 92 deletions
+1
View File
@@ -17,3 +17,4 @@ Day | Name | Type of Algo & Notes
9 | Sensor Boost | __MORE INTCODE. YAYY__ 🙃 <br> - A new parameter mode and opcode. <br> - __Really feeling the (tech) debt of some earlier design choices here, went back to refactor day07 before jumping into this one__, then it was a small bit of code for the relative param/opcode & resizing computer memory if necessary
10 | Monitoring Station | - This (part2) is my favorite algo... Yes I have a favorite algo <br> Fundamentally it's a geometry problem, angles and trig <br> - Part 1: Calculated via slopes <br> - Part 2: Using Arctangent to find angles an asteroid makes against a vertical line from the home base asteroid. Then those angles can be used to determine if Asteroids are covering each other, AND iterating through all of them can find the next angle Asteroid to vaporize
11 | Space Police | - More Intcode stuff... <br> - __2D Array/Slice manipulation__ and a bit of maths/graphing <br> - Implemented a __RotateGrid__ algo
12 | The N-Body Problem | I like to call this a _(harmonic) frequency_ algo. Finding the harmonic frequency of multiple bodies/items and then finding the Least Common Multiple of those frequencies will tell you when all the bodies have returned to their initial state. <br> - I've used this approach for a leetcode problem about prisoners in jail cells too
+28 -46
View File
@@ -1,27 +1,23 @@
package main
import (
"bufio"
"adventofcode/util"
"fmt"
"log"
"math"
"os"
"path/filepath"
"strconv"
"strings"
)
type moon struct {
// Moon stores coordinates and velocities of a moon
type Moon struct {
x, y, z, dx, dy, dz int
}
func main() {
stringSlice := readInputFile("../input.txt")
// fmt.Println(stringSlice)
input := util.ReadFile("../input.txt")
stringSlice := strings.Split(input, "\n")
// need to set x y z, dx, dy, dz of each of the starting moons
sliceMoons := makeMoonSlice(stringSlice)
// fmt.Println(sliceMoons)
// iterate through for each step
for i := 0; i < 1000; i++ {
@@ -60,52 +56,36 @@ func main() {
sliceMoons[i2].y += e.dy
sliceMoons[i2].z += e.dz
}
// fmt.Println(sliceMoons)
}
// get final kinetic energy?
// get final kinetic energy
fmt.Println(kinetic(sliceMoons))
}
func readInputFile(path string) []string {
// var pixelString string
resultSlice := make([]string, 0)
absPath, _ := filepath.Abs(path)
file, err := os.Open(absPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// pixelString = line
resultSlice = append(resultSlice, line)
}
// return pixelString
return resultSlice
}
func makeMoonSlice(stringSlice []string) []moon {
sliceMoons := make([]moon, 0)
func makeMoonSlice(stringSlice []string) []Moon {
sliceMoons := make([]Moon, 0)
for _, str := range stringSlice {
x := str[strings.Index(str, "x=")+2 : strings.Index(str, ",")]
// this is gross
y := str[strings.Index(str, "y=")+2 : strings.Index(str, "y=")+strings.Index(str[strings.Index(str, ",")+1:], ",")-1]
z := str[strings.Index(str, "z=")+2 : len(str)-1]
xStart := strings.Index(str, "x=") + 2
xEnd := strings.Index(str, ",")
yStart := xEnd + 4
zStart := strings.Index(str, "z=") + 2
yEnd := zStart - 4
zEnd := len(str) - 1
intx, _ := strconv.Atoi(x)
inty, _ := strconv.Atoi(y)
intz, _ := strconv.Atoi(z)
sliceMoons = append(sliceMoons, moon{intx, inty, intz, 0, 0, 0})
x := str[xStart:xEnd]
y := str[yStart:yEnd]
z := str[zStart:zEnd]
intX, _ := strconv.Atoi(x)
intY, _ := strconv.Atoi(y)
intZ, _ := strconv.Atoi(z)
sliceMoons = append(sliceMoons, Moon{intX, intY, intZ, 0, 0, 0})
}
return sliceMoons
}
func kinetic(moons []moon) (result int) {
// get total "kinetic energy" of a slice of Moons
func kinetic(moons []Moon) (result int) {
for _, e := range moons {
sumXYZ := abs(e.x) + abs(e.y) + abs(e.z)
velXYZ := abs(e.dx) + abs(e.dy) + abs(e.dz)
@@ -115,6 +95,8 @@ func kinetic(moons []moon) (result int) {
}
func abs(value int) int {
flt := float64(value)
return int(math.Abs(flt))
if value < 0 {
return value * -1
}
return value
}
+22 -42
View File
@@ -1,16 +1,14 @@
package main
import (
"bufio"
"adventofcode/util"
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
type moon struct {
// Moon stores coordinates and velocities of a moon
type Moon struct {
x, y, z, dx, dy, dz int
}
type oneDim struct {
@@ -18,9 +16,8 @@ type oneDim struct {
}
func main() {
// stringSlice := readInputFile("../test2772.txt")
stringSlice := readInputFile("../input.txt")
// fmt.Println(stringSlice)
input := util.ReadFile("../input.txt")
stringSlice := strings.Split(input, "\n")
// need to set x y z, dx, dy, dz of each of the starting moons
sliceMoons := makeMoonSlice(stringSlice)
@@ -51,42 +48,25 @@ func main() {
fmt.Println(lcm2([]int{xSteps, ySteps, zSteps}))
}
// helper function to put the input file into a slice of strings (each elements is a line of the txt file)
func readInputFile(path string) []string {
// var pixelString string
resultSlice := make([]string, 0)
absPath, _ := filepath.Abs(path)
file, err := os.Open(absPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// pixelString = line
resultSlice = append(resultSlice, line)
}
// return pixelString
return resultSlice
}
// helper function to take the slice of strings and return a slice of moon structs
func makeMoonSlice(stringSlice []string) []moon {
sliceMoons := make([]moon, 0)
// helper function to take the slice of strings and return a slice of Moon structs
func makeMoonSlice(stringSlice []string) []Moon {
sliceMoons := make([]Moon, 0)
for _, str := range stringSlice {
x := str[strings.Index(str, "x=")+2 : strings.Index(str, ",")]
// this is gross
y := str[strings.Index(str, "y=")+2 : strings.Index(str, "y=")+strings.Index(str[strings.Index(str, ",")+1:], ",")-1]
z := str[strings.Index(str, "z=")+2 : len(str)-1]
xStart := strings.Index(str, "x=") + 2
xEnd := strings.Index(str, ",")
yStart := xEnd + 4
zStart := strings.Index(str, "z=") + 2
yEnd := zStart - 4
zEnd := len(str) - 1
intx, _ := strconv.Atoi(x)
inty, _ := strconv.Atoi(y)
intz, _ := strconv.Atoi(z)
sliceMoons = append(sliceMoons, moon{intx, inty, intz, 0, 0, 0})
x := str[xStart:xEnd]
y := str[yStart:yEnd]
z := str[zStart:zEnd]
intX, _ := strconv.Atoi(x)
intY, _ := strconv.Atoi(y)
intZ, _ := strconv.Atoi(z)
sliceMoons = append(sliceMoons, Moon{intX, intY, intZ, 0, 0, 0})
}
return sliceMoons
}
-4
View File
@@ -1,4 +0,0 @@
<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>