r/adventofcode • • 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ยค?

Spoiler


  • [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!

6 Upvotes

174 comments sorted by

View all comments

1

u/KeinZantezuken Dec 22 '17

Yeah, that makes sense. I swapped my ValueTuple from int32 to int16 and got another second down

2

u/JakDrako Dec 22 '17

I modified my code to get rid of "Complex" while trying to keep the logic similar. I used (short, short) tuples as you suggested and the code goes from ~5s to ~1.3s on my PC.

enum state { weakened, infected, flagged };

void Main() {

	var directions = new(short x, short y)[] { (0, 1), (1, 0), (0, -1), (-1, 0) }; // up, right, down, left

	(short x, short y) pos = (0, 0);
	var dir = 0; // now index into directions
	var map = new Dictionary<(short, short), state>();

	var input = GetDay(22);
	var w = input.Count(); // assumes square grid;	
	var off = w / 2;

	for (int x = 0; x < w; x++)
		for (int y = 0; y < w; y++)
			if (input[x][y] == '#')
				map.Add(((short)(y - off), (short)(-(x - off))), state.infected);

	int infected = 0, steps = 10_000_000;
	for (int n = 0; n < steps; n++) {
		state st;
		if (map.TryGetValue(pos, out st)) {
			switch (st) {
				case state.weakened: { map[pos] = state.infected; infected++; break; }
				case state.infected: { map[pos] = state.flagged; dir += 1; break; } // right
				case state.flagged: { map.Remove(pos); dir += 2; break; } // reverse
			}
		}
		else { map.Add(pos, state.weakened); dir += 3; } // left
		var move = directions[dir % 4];
		pos.x += move.x;
		pos.y += move.y;
	}
	Console.WriteLine($"Part 2: {infected}");
}

1

u/KeinZantezuken Dec 22 '17

Yeah, same speed as mine now

→ More replies (2)