-
Notifications
You must be signed in to change notification settings - Fork 1
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
nick-telsan
wants to merge
1
commit into
main
Choose a base branch
from
nt/day03
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 👏