diff --git a/nicktelsan/README.md b/nicktelsan/README.md index 96895e5..8efba06 100644 --- a/nicktelsan/README.md +++ b/nicktelsan/README.md @@ -12,6 +12,16 @@ Other dependencies may evolve as I complete other exercises, but I'm trying to d These are the commands required to run each day's test +Day 3: + +- Part 1: + `node day03/powerUsage.js` + `go run day03/powerUsage.go` + +- Part 2: + `node day03/lifeSupport.js` + `go run day03/lifeSupport.go` + Day 2: - Part 1: diff --git a/nicktelsan/day03/lifeSupport.go b/nicktelsan/day03/lifeSupport.go new file mode 100644 index 0000000..75ab216 --- /dev/null +++ b/nicktelsan/day03/lifeSupport.go @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "strconv" + "strings" +) + +func divideArray(array []string, position int) ([]string, []string) { + if len(array) == 1 { + return array, array + } + + zero := 0 + one := 0 + + leastCommonArray := make([]string, 0) + mostCommonArray := make([]string, 0) + + for i := 0; i < len(array); i++ { + if array[i][position] == '0' { + zero++ + } else { + one++ + } + } + + mostCommon := "" + + if zero > one { + mostCommon = "0" + } else { + mostCommon = "1" + } + + for i := 0; i < len(array); i++ { + if string(array[i][position]) == mostCommon { + mostCommonArray = append(mostCommonArray, array[i]) + } else { + leastCommonArray = append(leastCommonArray, array[i]) + } + } + + return mostCommonArray, leastCommonArray +} + +func main() { + content, err := ioutil.ReadFile("day03/input") + + if err != nil { + log.Fatal(err) + } + + inputArray := strings.Split(string(content), "\n") + bitLength := len(inputArray[0]) + + generator, scrubber := divideArray(inputArray, 0) + + for i := 1; i < bitLength; i++ { + generator, _ = divideArray(generator, i) + _, scrubber = divideArray(scrubber, i) + } + + decimalGenerator, _ := strconv.ParseInt(generator[0], 2, 64) + decimalScrubber, _ := strconv.ParseInt(scrubber[0], 2, 64) + lifeSupport := decimalGenerator * decimalScrubber + + fmt.Printf("O2 Generator Rating: %v\n", decimalGenerator) + fmt.Printf("CO2 Scrubber Rating: %v\n", decimalScrubber) + fmt.Printf("Life Support Rating: %v\n", lifeSupport) + +} diff --git a/nicktelsan/day03/lifeSupport.js b/nicktelsan/day03/lifeSupport.js new file mode 100644 index 0000000..8839d5c --- /dev/null +++ b/nicktelsan/day03/lifeSupport.js @@ -0,0 +1,57 @@ +import fs from 'fs' +import path from 'path' + +const __dirname = path.resolve() + +function divideArray(array, position) { + if (array.length === 1) { + return { mostCommonArray: array, leastCommonArray: array } + } + + let zero = 0 + let one = 0 + + const leastCommonArray = [] + const mostCommonArray = [] + + array.forEach(element => { + element[position] === '0' ? zero++ : one++ + }) + + let mostCommon = zero > one ? '0' : '1' + + array.forEach(element => { + if (element[position] === mostCommon) { + mostCommonArray.push(element) + } else { + leastCommonArray.push(element) + } + }) + + return { mostCommonArray, leastCommonArray } +} + +function main() { + const input = fs.readFileSync(path.join(__dirname, 'day03/input'), 'utf8') + const inputArray = input.split('\n') + const bitLength = inputArray[0].length + + let result = divideArray(inputArray, 0) + + let generator = result.mostCommonArray + let scrubber = result.leastCommonArray + + for (var i = 1; i < bitLength; i++) { + generator = divideArray(generator, i).mostCommonArray + scrubber = divideArray(scrubber, i).leastCommonArray + } + + generator = parseInt(generator[0], 2) + scrubber = parseInt(scrubber[0], 2) + + console.log(`O2 Generator Rating: ${generator}`) + console.log(`CO2 Scrubber Rating: ${scrubber}`) + console.log(`Life Support Rating: ${generator * scrubber}`) +} + +main() diff --git a/nicktelsan/day03/powerUsage.go b/nicktelsan/day03/powerUsage.go new file mode 100644 index 0000000..130ab43 --- /dev/null +++ b/nicktelsan/day03/powerUsage.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "strconv" + "strings" +) + +func mostCommonBit(array []string, position int) string { + zero := 0 + one := 0 + + for i := 0; i < len(array); i++ { + if array[i][position] == '0' { + zero++ + } else { + one++ + } + } + + if zero > one { + return "0" + } else { + return "1" + } +} + +func flipBits(number string) string { + newNumber := "" + + for i := 0; i < len(number); i++ { + if string(number[i]) == "0" { + newNumber += "1" + } else { + newNumber += "0" + } + } + return newNumber +} + +func main() { + content, err := ioutil.ReadFile("day03/input") + + if err != nil { + log.Fatal(err) + } + + inputArray := strings.Split(string(content), "\n") + bitLength := len(inputArray[0]) + + gamma := "" + + for i := 0; i < bitLength; i++ { + gamma += mostCommonBit(inputArray, i) + } + + epsilon := flipBits(gamma) + + decimalGamma, _ := strconv.ParseInt(gamma, 2, 64) + decimalEpsilon, _ := strconv.ParseInt(epsilon, 2, 64) + power := decimalGamma * decimalEpsilon + + fmt.Printf("gamma: %v\n", decimalGamma) + fmt.Printf("epsilon: %v\n", decimalEpsilon) + fmt.Printf("Power: %v\n", power) + +} diff --git a/nicktelsan/day03/powerUsage.js b/nicktelsan/day03/powerUsage.js new file mode 100644 index 0000000..f84f912 --- /dev/null +++ b/nicktelsan/day03/powerUsage.js @@ -0,0 +1,48 @@ +import fs from 'fs' +import path from 'path' + +const __dirname = path.resolve() + +function mostCommonBit(array, position) { + let zero = 0 + let one = 0 + + array.forEach(element => { + element[position] === '0' ? zero++ : one++ + }) + + return zero > one ? '0' : '1' +} + +function flipBits(number) { + let newNumber = '' + + for (var i = 0; i < number.length; i++) { + newNumber += number[i] === '0' ? '1' : '0' + } + return newNumber +} + +function main() { + const input = fs.readFileSync(path.join(__dirname, 'day03/input'), 'utf8') + const inputArray = input.split('\n') + const bitLength = inputArray[0].length + + let gamma = '' + + for (var i = 0; i < bitLength; i++) { + gamma += mostCommonBit(inputArray, i) + } + + let epsilon = flipBits(gamma) + + console.log(gamma) + gamma = parseInt(gamma, 2) + epsilon = parseInt(epsilon, 2) + + console.log(`gamma: ${gamma}`) + console.log(`epsilon: ${epsilon}`) + console.log(`Power: ${gamma * epsilon}`) +} + +main()