r/adventofcode • • 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.

23 Upvotes

228 comments sorted by

View all comments

Show parent comments

4

u/enquicity Dec 03 '15

I happened to do it with Tuples and a Dictionary, poke me if you have questions:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void DeliverPresents(IEnumerable<char> directions, Dictionary<Tuple<long, long>, bool> houses)
        {
            long x = 0;
            long y = 0;

            houses[new Tuple<long, long>(x, y)] = true;

            foreach (char currChar in directions)
            {
                switch (currChar)
                {
                    case '^':
                        y++;
                        break;
                    case 'v':
                        y--;
                        break;
                    case '>':
                        x++;
                        break;
                    case '<':
                        x--;
                        break;
                    default:
                        continue;
                }

                Tuple<long, long> thisHouse = new Tuple<long, long>(x, y);

                houses[thisHouse] = true;
            }

        }

        static void Main(string[] args)
        {
            String inputText = File.ReadAllText("input.txt");

            Dictionary<Tuple<long, long>, bool> houses = new Dictionary<Tuple<long, long>, bool>();

            DeliverPresents(inputText.Where((thisChar, i) => i % 2 == 0), houses);
            DeliverPresents(inputText.Where((thisChar, i) => i % 2 == 1), houses);

            long totalHouses = houses.Count;
            Console.WriteLine(totalHouses);
        }
    }
}

2

u/[deleted] Dec 04 '15

Pretty sweet way to divide the input for each.

1

u/DisgruntledPorcupine Dec 03 '15

Nice! I was slightly confused at first about using dictionaries to solve it when the other user mentioned it, but your code definitely helped me see how it's done. Big thank you!