He is a barbarian, and thinks the customs of his tribe and island are the laws of nature.
Go get them!
shocktohp/Day1.goshocktohp/Day1.go
package main import ( "bufio" "fmt" "log" "os" "strconv" ) func main() { fmt.Println("Test"); values := readFileToIntArr("day1"); var p1 int = part1(values); var p2 int = part2(values); fmt.Printf("p1:%d\np2:%d\n", p1, p2); bigboy := readFileToIntArr("bigboy1"); p1 = part1(bigboy); p2 = part2(bigboy); fmt.Printf("big1:%d\nbig2:%d\n", p1, p2); } func readFileToIntArr (filename string) ([]int) { var values []int; var file, err = os.Open(fmt.Sprintf("./inputs/%s", filename)); if err != nil { log.Fatal(err); } defer file.Close(); var scanner = bufio.NewScanner(file); for scanner.Scan() { num, err := strconv.Atoi(scanner.Text()); if (err != nil) { log.Fatal(err); } values = append(values, num); } return values; } func part1(values []int) (int) { var count int = 0; for i, value := range values { if(i < len(values) - 1 && value < values[i+1]) { count++; } } return count; } func part2(values []int) (int) { var count int = 0; for i, _ := range values { if(i < len(values) - 3 && lookahead(values, i) < lookahead(values, i+1)) { count++; } } return count; } func lookahead(values []int, index int) (int) { var sum int = 0; sum = values[index] + values[index+1] + values[index+2]; return sum; }
shocktohp/Day2.goshocktohp/Day2.go
package main import ( "bufio" "fmt" "log" "os" "strconv" ) var d int = 0; var h int = 0; func main() { fmt.Println("Test"); values := readFileToStrArr("day2"); var p1 int = part1(values); var p2 int = part2(values); fmt.Printf("p1:%d\np2:%d\n", p1, p2); /\*bigboy := readFileToIntArr("bigboy1"); p1 = part1(bigboy); p2 = part2(bigboy); fmt.Printf("big1:%d\nbig2:%d\n", p1, p2);\*/ } func readFileToStrArr (filename string) ([]string) { var values []string; var file, err = os.Open(fmt.Sprintf("./inputs/%s", filename)); if err != nil { log.Fatal(err); } defer file.Close(); var scanner = bufio.NewScanner(file); for scanner.Scan() { values = append(values, scanner.Text()); } return values; } func readFileToIntArr (filename string) ([]int) { var values []int; var file, err = os.Open(fmt.Sprintf("./inputs/%s", filename)); if err != nil { log.Fatal(err); } defer file.Close(); var scanner = bufio.NewScanner(file); for scanner.Scan() { num, err := strconv.Atoi(scanner.Text()); if (err != nil) { log.Fatal(err); } values = append(values, num); } return values; } func part1(values []string) (int) { var d int = 0; var f int = 0; for _, val := range values { var com string = val[0:1]; var l string = val[len(val) - 1:]; fmt.Printf("val: %s\ncom: %s\nl: %s\n\n", val, com, l); c, _ := strconv.Atoi(l); switch { case com == "f": f = f + c case com == "d": d = d + c; case com == "u": d = d - c; } } return f\*d; } func part2(values []string) (int) { var d int = 0; var f int = 0; var a int = 0; for _, val := range values { var com string = val[0:1]; var l string = val[len(val) - 1:]; fmt.Printf("val: %s\ncom: %s\nl: %s\n\n", val, com, l); c, _ := strconv.Atoi(l); switch { case com == "f": f = f + c d = d + (a \* c); case com == "d": a = a + c; case com == "u": a = a - c; } } return f\*d; }