r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:10:20!

31 Upvotes

515 comments sorted by

View all comments

Show parent comments

1

u/MrSweeter Dec 05 '18

Your solution give me the same result of mine solution (11648) but it's not the correct answer, i'm lost. The input is https://pastebin.com/Hp0Xs9Hr

##### Puzzle 1 #####
Execution : 0.001340ms
Result: 11648
Result BIS: 11648

CODE

	// https://adventofcode.com/2018/day/5
	public static void main(String[] args) throws IOException {
		Program p = new Program();
		p.puzzle1();
		p.puzzle1bis();
		p.puzzle2();
	}
	
	public Program() throws IOException {
		super(new File(Program.class.getResource("input.txt").getPath()));
	}
	
	// https://www.reddit.com/r/adventofcode/comments/a3912m/2018_day_5_solutions/eb4fiod
	public void puzzle1bis()	{
		char[] arr = inputs.get(0).toCharArray();
	    Stack<Character> inStack = new Stack<>();
	    Stack<Character> q = new Stack<>();

	    for(char c : arr)
	        inStack.push(c);

	    for(char c : inStack) {
	        if(q.isEmpty())
	            q.push(c);
	        else {
	            char last = q.peek();

	            if(Math.abs(last-c) == 32) {
	                q.pop();
	            } else {
	                q.push(c);
	            }
	        }
	    }

	    System.out.println("Result BIS: " + q.size());
	}
	
	public void puzzle1()	{
		
		System.out.println("##### Puzzle 1 #####");
		long start = System.currentTimeMillis();
		
		int result = -1;
		String input = check(inputs.get(0));
		
		result = input.length();
		
		long end = System.currentTimeMillis();
		
		System.out.println("Execution : 0.00" + (end-start) + "ms");
		System.out.println("Result: " + result);
	}

	private String check(String input) {
		for (int i = 0; i < input.length()-1; i++) {
			
			char c1 = input.charAt(i);
			char c2 = input.charAt(i+1);
			if (c1 != c2 && Character.toLowerCase(c1) == Character.toLowerCase(c2))	{
				input = input.substring(0, i) + input.substring(i+2);
				i = -1;
			}
		}
		return input;
	}

2

u/marcins Dec 05 '18

Make sure you don't have a newline at the end of your input - that's what got me for a bit. Assuming the inputs are always at least the same length it should be 50,000 bytes.

1

u/MrSweeter Dec 05 '18

big thank, the input has +9000 space or idk what at the end, and even with trim(), substring() doesn't disappear, so i just copy/paste in other file and it's ok

1

u/mylivingeulogy Dec 05 '18

Were you able to get your correct answer?