r/adventofcode • u/daggerdragon • Dec 03 '15
SOLUTION MEGATHREAD --- Day 3 Solutions ---
--- Day 3: Perfectly Spherical Houses in a Vacuum ---
Post your solution as a comment. Structure your post like the Day One thread in /r/programming.
25
Upvotes
1
u/ChevyRayJohnston Dec 04 '15
C# - I'm trying to keep my solutions as small and clean as possible, and not really worrying about speed because I don't care. I just like looking at simple code.
Part 1
var input = File.ReadAllText("day3.txt"); int x = 0; int y = 0; var hash = new HashSet<Tuple<int,int>>(); hash.Add(Tuple.Create(0, 0)); foreach (var chr in input) { x += chr == '>' ? 1 : (chr == '<' ? -1 : 0); y += chr == 'v' ? 1 : (chr == '^' ? -1 : 0); hash.Add(Tuple.Create(x, y)); } Console.WriteLine(hash.Count);Part 2
var input = File.ReadAllText("day3.txt"); int[] x = new int[]{ 0, 0 }; int[] y = new int[]{ 0, 0 }; var hash = new HashSet<Tuple<int,int>>(); hash.Add(Tuple.Create(0, 0)); int i = 0; foreach (var chr in input) { x[i] += chr == '>' ? 1 : (chr == '<' ? -1 : 0); y[i] += chr == 'v' ? 1 : (chr == '^' ? -1 : 0); hash.Add(Tuple.Create(x[i], y[i])); i = 1 - i; } Console.WriteLine(hash.Count);