You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
funcCubeConundrum() int {
// Open the file named "input.txt". If an error occurs, print the error and return 0.file, err:=os.Open("input.txt")
iferr!=nil {
fmt.Println("Error opening file:", err)
return0
}
// Close the file when the function exits.deferfile.Close()
// Create a scanner to read the file line by line.scanner:=bufio.NewScanner(file)
// Initialize variables to keep track of the total and bufferCount.total:=0varbufferCountint// Loop through each line in the file.fori:=0; scanner.Scan(); i++ {
// Get the current line.lines:=scanner.Text()
// Loop through each character in the line.forj:=0; j<len(lines); j++ {
// If the character is a digit, update bufferCount accordingly.iflines[j] >='0'&&lines[j] <='9' {
bufferCount*=10bufferCount+=int(lines[j] -'0')
continue
}
// If the character is a space, skip to the next character.iflines[j] ==' ' {
continue
}
// Check the color of the current character and compare bufferCount.switchlines[j] {
case'r':
ifbufferCount>12 {
// If bufferCount exceeds the limit for red, exit the loop.goto exit
}
case'g':
ifbufferCount>13 {
// If bufferCount exceeds the limit for green, exit the loop.goto exit
}
case'b':
ifbufferCount>14 {
// If bufferCount exceeds the limit for blue, exit the loop.goto exit
}
}
// Reset bufferCount for the next color.bufferCount=0
}
// If the loop completes without exceeding any limits, update the total.total+=i+1
exit:
}
// Print the total and return it.fmt.Println(total)
returntotal
}
Part -2
// max returns the maximum value between two integers.funcmax(a, bint) int {
ifa>b {
returna
}
returnb
}
// CubeConundrumPower calculates the power based on color counts and buffer values.funcCubeConundrumPower() int {
// Open the file named "input.txt". If an error occurs, print the error and return 0.file, err:=os.Open("input.txt")
iferr!=nil {
fmt.Println("Error opening file:", err)
return0
}
// Close the file when the function exits.deferfile.Close()
// Create a scanner to read the file line by line.scanner:=bufio.NewScanner(file)
// Initialize variables to keep track of the power and bufferCount.power:=0varbufferCountint// Loop through each line in the file.forscanner.Scan() {
// Initialize color counts for red, green, and blue.varredCount, greenCount, blueCountint=0, 0, 0// Get the current line.lines:=scanner.Text()
// Loop through each character in the line.forj:=0; j<len(lines); j++ {
// If the character is a digit, update bufferCount accordingly.iflines[j] >='0'&&lines[j] <='9' {
bufferCount*=10bufferCount+=int(lines[j] -'0')
continue
}
// If the character is a space, skip to the next character.iflines[j] ==' ' {
continue
}
// Check the color of the current character and update the corresponding color count.switchlines[j] {
case'r':
redCount=max(redCount, bufferCount)
case'g':
greenCount=max(greenCount, bufferCount)
case'b':
blueCount=max(blueCount, bufferCount)
}
// Reset bufferCount for the next color.bufferCount=0
}
// Update the power based on the product of color counts.power+=redCount*greenCount*blueCount
}
// Print the power and return it.fmt.Println(power)
returnpower
}
Code TypeScript
Part -1
import*asfsfrom'fs';functionmax(a: number,b: number): number{returna>b ? a : b;}functioncubeConundrum(): number{try{// Read the contents of the file named "input.txt".constfileContent: string=fs.readFileSync('input.txt','utf-8');lettotal: number=0;letbufferCount: number=0;// Split the content into lines.constlines: string[]=fileContent.split('\n');for(leti=0;i<lines.length;i++){constline: string=lines[i];for(letj=0;j<line.length;j++){// If the character is a digit, update bufferCount accordingly.if(line[j]>='0'&&line[j]<='9'){bufferCount*=10;bufferCount+=parseInt(line[j],10);continue;}// If the character is a space, skip to the next character.if(line[j]===' '){continue;}// Check the color of the current character and compare bufferCount.switch(line[j]){case'r':
if(bufferCount>12){// If bufferCount exceeds the limit for red, exit the loop.bufferCount=0;break;}break;case'g':
if(bufferCount>13){// If bufferCount exceeds the limit for green, exit the loop.bufferCount=0;break;}break;case'b':
if(bufferCount>14){// If bufferCount exceeds the limit for blue, exit the loop.bufferCount=0;break;}break;}// Reset bufferCount for the next character.bufferCount=0;}// If the loop completes without exceeding any limits, update the total.total+=i+1;}console.log(total);returntotal;}catch(error){console.log('Error opening file:',error);return0;}}// Call the function to execute the TypeScript code.cubeConundrum();
Part -2
import*asfsfrom'fs';// Function to find the maximum of two numbersfunctionmax(a: number,b: number): number{returna>b ? a : b;}// Function to calculate power based on color counts and buffer valuesfunctioncubeConundrumPower(): number{try{// Read the contents of the file named "input.txt".constfileContent: string=fs.readFileSync('input.txt','utf-8');letpower: number=0;letbufferCount: number=0;// Split the content into lines.constlines: string[]=fileContent.split('\n');for(leti=0;i<lines.length;i++){letredCount: number=0,greenCount: number=0,blueCount: number=0;constline: string=lines[i];for(letj=0;j<line.length;j++){// If the character is a digit, update bufferCount accordingly.if(line[j]>='0'&&line[j]<='9'){bufferCount*=10;bufferCount+=parseInt(line[j],10);continue;}// If the character is a space, skip to the next character.if(line[j]===' '){continue;}// Check the color of the current character and update the corresponding color count.switch(line[j]){case'r':
redCount=max(redCount,bufferCount);break;case'g':
greenCount=max(greenCount,bufferCount);break;case'b':
blueCount=max(blueCount,bufferCount);break;}// Reset bufferCount for the next character.bufferCount=0;}// Update the power based on the product of color counts.power+=redCount*greenCount*blueCount;}console.log(power);returnpower;}catch(error){console.log('Error opening file:',error);return0;}}// Call the function to execute the TypeScript code.cubeConundrumPower();
Code Dart
part -1
import'dart:io';
intcubeConundrum() {
try {
// Open the file named "input.txt".File file =File("test.txt");
// Check if the file exists.if (!file.existsSync()) {
print("Error: File 'test.txt' not found.");
return0;
}
List<String> lines = file.readAsLinesSync();
int total =0;
int bufferCount =0;
bool shouldExit =false;
for (int i =0; i < lines.length &&!shouldExit; i++) {
String line = lines[i];
for (int j =0; j < line.length; j++) {
// If the character is a digit, update bufferCount accordingly.if (line[j].codeUnitAt(0) >='0'.codeUnitAt(0) &&
line[j].codeUnitAt(0) <='9'.codeUnitAt(0)) {
bufferCount *=10;
bufferCount +=int.parse(line[j]);
continue;
}
// If the character is a space, skip to the next character.if (line[j] ==' ') {
continue;
}
// Check the color of the current character and compare bufferCount.switch (line[j]) {
case'r':if (bufferCount >12) {
shouldExit =true;
}
break;
case'g':if (bufferCount >13) {
shouldExit =true;
}
break;
case'b':if (bufferCount >14) {
shouldExit =true;
}
break;
}
// Reset bufferCount for the next color.
bufferCount =0;
}
// If the loop completes without exceeding any limits, update the total.if (!shouldExit) {
total += i +1;
}
}
print(total);
return total;
} catch (error) {
print('Error opening file: $error');
return0;
}
}
Part 2
import'dart:io';
intmax(int a, int b) {
return a > b ? a : b;
}
intcubeConundrumPower() {
try {
// Open the file named "input.txt".File file =File("text.txt");
List<String> lines = file.readAsLinesSync();
int power =0;
int bufferCount =0;
for (String line in lines) {
int redCount =0, greenCount =0, blueCount =0;
for (int j =0; j < line.length; j++) {
// If the character is a digit, update bufferCount accordingly.if (line.codeUnitAt(j) >='0'.codeUnitAt(0) &&
line.codeUnitAt(j) <='9'.codeUnitAt(0)) {
bufferCount *=10;
bufferCount +=int.parse(line[j]);
continue;
}
// If the character is a space, skip to the next character.if (line[j] ==' ') {
continue;
}
// Check the color of the current character and compare bufferCount.switch (line[j]) {
case'r':
redCount =max(redCount, bufferCount);
break;
case'g':
greenCount =max(greenCount, bufferCount);
break;
case'b':
blueCount =max(blueCount, bufferCount);
break;
}
// Reset bufferCount for the next color.
bufferCount =0;
}
// Update the power based on the product of color counts.
power += redCount * greenCount * blueCount;
}
print(power);
return power;
} catch (error) {
print('Error opening file: $error');
return0;
}
}