Advent of code 2023 Day 1

Advent of code is started

https://adventofcode.com/2023/day/1

1 Like

Private leaderboard code: 3233550-02bd41fd (So we can compete and learn together)

My day1 of Advent of code 2023. I done it on java. If anyone have to optimise this code, that are welcome to do so.

	public static void main(String[] args) throws IOException {
		// i gave test case through .txt file. Set the path on dir string
		String s = "",dir="/home/PC/Music/AdventOfCode/code1.txt";
		BufferedReader br = new BufferedReader(new FileReader(dir));
		int sum=0;
		
		while((s =br.readLine()) !=null) // read th file from the directory
		{
			int i=0;
			char first='\0',last='\0';
	
			while(i < s.length())					// traverse the string from 0 to n 
			{
				if(Character.isDigit(s.charAt(i)))  // the digit(0-1) comes 
				{	
					if(first == '\0')				// first time it store the value in first variable, it is one time use
						first=s.charAt(i);
					last=s.charAt(i);				// if the char is digit(0-9) it alwasys store on last
				}
				i++;
			}
	
			System.out.println(first+" "+last);
			sum+= Integer.parseInt(String.valueOf(first)+String.valueOf(last));
		}
	System.out.println(sum);
	br.close();
	}
1 Like

My day1 part2 of Advent of code 2023. I done it on java.

public static void main(String[] args) throws IOException {
	// i gave test case through .txt file. Set the path on dir string
	String s = "",dir="/home/PC/Music/AdventOfCode/code1.txt";
	BufferedReader br = new BufferedReader(new FileReader(dir));
	int sum=0;	
	while((s =br.readLine()) !=null) // read th file from the directory
	{
		int i=0;
		char first='\0',last='\0';
		while(i < s.length())					// traverse the string from 0 to n 
		{
			if(Character.isDigit(s.charAt(i)))  // the digit(0-1) comes 
			{	
				if(first == '\0')				// first time it store the value in first variable, it is one time use
					first=s.charAt(i);
				last=s.charAt(i);				// if the char is digit(0-9) it alwasys store on last
			}
			i++;
		}
		System.out.println(first+" "+last);
		sum+= Integer.parseInt(String.valueOf(first)+String.valueOf(last));
		}
System.out.println(sum);
br.close();
}
1 Like

Hi all,

I too started solving advent of code yesterday.
Joined the above provate leaderboard as well.
Let’s have fun together :stuck_out_tongue_winking_eye:

Let’s complete all 25 days challenges :wink:

I have also started as well :grinning: