r/adventofcode • • Dec 20 '16

SOLUTION MEGATHREAD --- 2016 Day 20 Solutions ---

--- Day 20: Firewall Rules ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/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".


ROLLING A NATURAL 20 IS MANDATORY [?]

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!

7 Upvotes

168 comments sorted by

View all comments

Show parent comment

4

u/Tandrial Dec 20 '16 edited Dec 20 '16
try {
  Scanner sc = new Scanner(new File(filename));
  while (sc.hasNextLine()) {
    lines.add(sc.nextLine());
  }
} catch (FileNotFoundException e) {
  System.out.println("Error: File not found: "+e.getMessage());
} // end try/catch block
String[] rawmsg = lines.toArray(new String[0]);

Can be replaced with

List<String> input = Files.readAllLines(Paths.get(filename));

also

first=msg.substring(0,msg.indexOf("-"));
last=msg.substring(msg.indexOf("-")+1, msg.length());
tm.put(Long.parseLong(first), Long.parseLong(last) );

can be replaced with

String[] in = msg.split("-");
tm.put(Long.parseLong(in[0]), Long.parseLong(in[1]));

and

Iterator i = set.iterator();
while(i.hasNext()) {
  Map.Entry me = (Map.Entry)i.next();
  start = ((long)me.getKey());    
   ...
  end = ((long)me.getValue());
   ...
  }// end iterator  

can be replaced with

for (Map.Entry<Long, Long> entry : tm.entrySet()) {
  start = entry.getKey();
  ...
  end = entry.getValue();
  ...
}

1

u/BadHorsemonkey Dec 20 '16

Hey, thanks!

I'm participating because I am both self-taught as a coder and out of practice and better ways to do what I'm doing is very helpful.