Big update: Adding all advent of codes to same repo
This commit is contained in:
64
AdventOfCode2015/day12/day12.go
Normal file
64
AdventOfCode2015/day12/day12.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("Day 12")
|
||||
start := time.Now()
|
||||
|
||||
dat, err := ioutil.ReadFile("day12.json")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
json.Unmarshal(dat, &result)
|
||||
fmt.Printf("Input: %v | %T \n", result, result)
|
||||
|
||||
var count float64 = solver(result)
|
||||
|
||||
elapsed := time.Since(start)
|
||||
fmt.Printf("Part 1: %v %v \n", count, elapsed)
|
||||
|
||||
}
|
||||
|
||||
func solver(json map[string]interface{}) float64 {
|
||||
var counter float64
|
||||
for _, value := range json {
|
||||
switch valueType := value.(type) {
|
||||
case bool:
|
||||
case float64:
|
||||
counter += valueType
|
||||
case string:
|
||||
case []interface{}:
|
||||
counter += interfaceArraySolver(valueType)
|
||||
case map[string]interface{}:
|
||||
counter += solver(valueType)
|
||||
}
|
||||
|
||||
}
|
||||
return counter
|
||||
}
|
||||
|
||||
func interfaceArraySolver(json []interface{}) float64 {
|
||||
var counter float64
|
||||
for _, value := range json {
|
||||
switch valueType := value.(type) {
|
||||
case bool:
|
||||
case float64:
|
||||
counter += valueType
|
||||
case string:
|
||||
case []interface{}:
|
||||
counter += interfaceArraySolver(valueType)
|
||||
case map[string]interface{}:
|
||||
counter += solver(valueType)
|
||||
}
|
||||
|
||||
}
|
||||
return counter
|
||||
}
|
||||
55
AdventOfCode2015/day12/day12.js
Normal file
55
AdventOfCode2015/day12/day12.js
Normal file
@@ -0,0 +1,55 @@
|
||||
const fs = require('fs');
|
||||
const obj = JSON.parse(fs.readFileSync('day12.json', 'utf8'));
|
||||
|
||||
console.log("Part 1: " + solver(obj));
|
||||
console.log("Part 2: ", + solverPart2(obj));
|
||||
|
||||
function solver(json) {
|
||||
|
||||
let counter = 0;
|
||||
|
||||
for (let value in json) {
|
||||
if (typeof json[value] == 'number') {
|
||||
counter += json[value]
|
||||
} else if (typeof json[value] == 'boolean') {
|
||||
|
||||
} else if (typeof json[value] == 'string') {
|
||||
|
||||
} else if (typeof json[value] == 'object') {
|
||||
counter += solver(json[value]);
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
|
||||
}
|
||||
|
||||
function solverPart2(json) {
|
||||
let counter = 0;
|
||||
let redFound = false;
|
||||
let toSolver = [];
|
||||
|
||||
for (let value in json) {
|
||||
if (typeof json[value] == 'number') {
|
||||
counter += json[value]
|
||||
} else if (typeof json[value] == 'boolean') {
|
||||
|
||||
} else if (typeof json[value] == 'string') {
|
||||
if (json[value] == 'red' && !Array.isArray(json)) {
|
||||
redFound = true;
|
||||
break;
|
||||
}
|
||||
} else if (typeof json[value] == 'object') {
|
||||
toSolver.push(json[value]);
|
||||
}
|
||||
}
|
||||
|
||||
if (redFound) {
|
||||
return 0;
|
||||
} else {
|
||||
for (let e of toSolver) {
|
||||
counter += solverPart2(e);
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user