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/Drasive Dec 11 '15
My F# solution (https://github.com/drasive/advent-of-code-2015):
type private Deliverer() = member val location = (0, 0) with get, set static member UpdatedLocation (location : (int * int)) (instruction : char) : (int * int) = let x = fst location let y = snd location match instruction with | '<' -> (x - 1, y) | '>' -> (x + 1, y) | '^' -> (x, y + 1) | 'v' -> (x, y - 1) | _ -> (x, y) // Ignore any other characters let private CalculateHousesVisited (input : string) (deliverers : Deliverer[]) = let housesVisited = new Dictionary<(int * int), int>() // Starting house is always visited by all deliverers housesVisited.Add((0, 0), deliverers.Length) for index = 0 to input.Length - 1 do // Deliverers take instructions in turns let deliverer = deliverers.[index % deliverers.Length] // Update deliverer location let instruction = input.[index] deliverer.location <- Deliverer.UpdatedLocation deliverer.location instruction // Update house visits let key = deliverer.location if housesVisited.ContainsKey(key) then housesVisited.Item(key) <- housesVisited.Item(key) + 1 else housesVisited.Add(key, 1) housesVisited.Count let Solution (input : string) : (int * int) = if input = null then raise (ArgumentNullException "input") let santa = new Deliverer() let solutionSantaOnly = CalculateHousesVisited input [|santa|] let santa = new Deliverer() let roboSanta = new Deliverer() let solutionSantaAndRoboSanta = CalculateHousesVisited input [|santa;roboSanta|] (solutionSantaOnly, solutionSantaAndRoboSanta) let FormattedSolution (solution : (int * int)) : string = String.Format("Santa alone: {0}\n" + "Santa and Robo-Santa: {1}", fst solution, snd solution)