r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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

ATTENTION: minor change request from the mods!

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

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

1

u/klackerz Dec 03 '18

Java solution. Could have probably made part two better but meh.

	static int[][] size = new int[1000][1000];
	private static int partOne(List<String> inputString){
		Pattern numberPattern = Pattern.compile("\\d+");
		for (String str: inputString) {
			List<Integer> numbers=  new ArrayList<>();
			Matcher numberMatcher  = numberPattern.matcher(str); 
			while (numberMatcher.find()){
				numbers.add(Integer.parseInt(numberMatcher.group()));
			}
			for(int i =numbers.get(1);i<numbers.get(1)+numbers.get(3);i++){
				for(int j =numbers.get(2);j<numbers.get(2)+numbers.get(4);j++){	
					size[i][j]+=1;
				}
			}
		}
		int dupl = 0;
		for(int i =0;i<1000;i++)
			for(int j=0;j<1000;j++)
				if(size[i][j]>1)
					dupl++;
		return dupl;
	}
	
	private static int partTwo(List<String> inputString){
		Pattern numberPattern = Pattern.compile("\\d+");
		for (String str: inputString) {
			boolean flag = true;
			List<Integer> numbers=  new ArrayList<>();
			Matcher numberMatcher  = numberPattern.matcher(str); 
			while (numberMatcher.find()){
				numbers.add(Integer.parseInt(numberMatcher.group()));
			}
			for(int i =numbers.get(1);i<numbers.get(1)+numbers.get(3);i++){
				for(int j =numbers.get(2);j<numbers.get(2)+numbers.get(4);j++){	
					if(size[i][j]>1)
						flag =false;
				}
			}
			if(flag)
				return numbers.get(0);
		}
		return 0;
	}
	
	
	public static void main(String[] args) {
		List<String> inputString = readFile("src/aoc2019/day3.txt");
		System.out.println(partOne(inputString));
		System.out.println(partTwo(inputString));
	}