Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nicktelan] Day 03 #24

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions nicktelsan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
74 changes: 74 additions & 0 deletions nicktelsan/day03/lifeSupport.go
Original file line number Diff line number Diff line change
@@ -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)

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm impressed at how compact this whole file is 👏

57 changes: 57 additions & 0 deletions nicktelsan/day03/lifeSupport.js
Original file line number Diff line number Diff line change
@@ -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()
69 changes: 69 additions & 0 deletions nicktelsan/day03/powerUsage.go
Original file line number Diff line number Diff line change
@@ -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)

}
48 changes: 48 additions & 0 deletions nicktelsan/day03/powerUsage.js
Original file line number Diff line number Diff line change
@@ -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()