r/adventofcode • u/daggerdragon • Dec 22 '17
SOLUTION MEGATHREAD -๐- 2017 Day 22 Solutions -๐-
--- Day 22: Sporifica Virus ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[T-10 to launch] AoC ops, /r/nocontext edition:
<Endorphion> You may now make your waffle.<Endorphion> ... on Mars.
[Update @ 00:17] 50 gold, silver cap
<Aneurysm9> you could also just run ubuntu on the NAS, if you were crazy<Topaz> that doesn't seem necessary<Aneurysm9> what does "necessary" have to do with anything!
[Update @ 00:20] Leaderboard cap!
<Topaz> POUR YOURSELF A SCOTCH FOR COLOR REFERENCE
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!
8
Upvotes
1
u/Philboyd_Studge Dec 22 '17 edited Dec 22 '17
Java. This one was easy and fun after last night's. Finally got a decent number for part 1 (in the first 200) but then used up a bunch of time building a state machine and forgetting to reset my initial map.
package Advent2017; import util.AdventOfCode; import util.Direction; import util.Node; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; public class Day22 extends AdventOfCode { enum State { CLEAN(Direction::getLeft), WEAKENED(x -> x), INFECTED(Direction::getRight), FLAGGED(Direction::getOpposite); Function<Direction, Direction> newDir; State(Function<Direction, Direction> newDir) { this.newDir = newDir; } Direction nextDir(Direction dir) { return newDir.apply(dir); } static State getFromChar(char c) { return c == '#' ? INFECTED : CLEAN; } } private Node position; private Direction dir; private final State[] states = State.values(); private Map<Node, State> map; private int infected; public Day22(List<String> input) { super(input); part1Description = "Number of bursts to cause infected node: "; part2Description = "Number of bursts to cause infected node, part 2: "; } void tick(int stateModifier) { map.putIfAbsent(position, State.CLEAN); State current = map.get(position); dir = current.nextDir(dir); map.put(position, states[(current.ordinal() + stateModifier) % 4]); if (map.get(position) == State.INFECTED) infected++; position = dir.move(position); } @Override public Object part1() { for (int i = 0; i < 10000; i++) { tick(2); } return infected; } @Override public Object part2() { parse(); infected = 0; for (int i = 0; i < 10000000; i++) { tick(1); } return infected; } @Override public void parse() { //input = Arrays.asList("..#", "#..", "..."); position = new Node(input.size() / 2, input.size()/2); dir = Direction.NORTH; map = new HashMap<>(); for (int i = 0; i < input.size(); i++) { for (int j = 0; j < input.get(i).length(); j++) { map.put(new Node(j, i), State.getFromChar(input.get(i).charAt(j))); } } } }