From 597062851700d3973aa4eca67559c5c755eb97d8 Mon Sep 17 00:00:00 2001 From: s00ner Date: Tue, 12 Jul 2022 14:52:10 -0400 Subject: [PATCH] Solved Day 3 --- day03/input | 1 + day03/input.testing | 1 + day03/solution.rb | 52 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 day03/input create mode 100644 day03/input.testing create mode 100644 day03/solution.rb diff --git a/day03/input b/day03/input new file mode 100644 index 0000000..03504fc --- /dev/null +++ b/day03/input @@ -0,0 +1 @@ +^><^>>>^<^v<<^<><^<^v>^vv<>v>>^^^v<<<^>^<^v<^>^v><<<^>><>v>v^<<^>^<<^^^>v>>v^^>^>vv>>^>>v^>^v>^<^^v>^>^^v^^>v^^v><^><^<<>v^<^<^vv^>>>v^v^>^<>^v<^^vv^>^<>^^<vv<>^>v<^>^v>v^>^v<>^><>>v^v<><>v^v>>>>v^^>^><^^<^>^v^v<>v<<<^<v^^^<^^^>v<^v>>>>>v<^^^^>v<^<>>>>>>v^vvvv^^^v<^v^>v><^>^v<<>>vv^>v>v^^>vv^<^vvv<>><>><><^^^^<^^^<^v>>v>^v^^^>v^^^v>>^v^^<^>>^>^<>>^^<>>^vv>v^<^>>>><><^^v<><<<<^^<>>^<>vv^<^>v^^><^v>^^>v<>^v^<>^vv^>vvv>v>^>^>>^>><>>>^^^^v^<>v^^>^<>v<^^v><^v><>^^^^^v^v>>^^v><<><^^^^><^>v>><<<^v>v^^>^v^<^^v>v<^<<>>^v<<>v<^v^>v^>^v<<v>v>>v>v^^v>^v^>>>><>^>v>^v^>>>>v^<<<>vvv>><^^>^<><^^<^v^v^<^^>^<^v^<<<^^v^v>>><>^^>vv<<^v^<<<<^^>>>v>v<<<>^^>>>v>^>v>vv<<>^<^>^^<^>^v^<>v^><^^^>vv>><^v<^<<<><<^^<><>v>^>^<>>^^v>vv^<^^v>><^vv^<<<>vv^v<^<>v^^>><>^<^v<<<^<<^>>>^^<^^v>v^vvvv>v<>><^>^<<<v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<<^>^>><<^^^vvv<<^vv<^^>v^^vv^<^^<<^^>>^^<v^>>v^^>v<^>^>vv>><v><^<^vv>^^v>^>v<<^vv><^^^^^^><<^>>>^v<>^^v<<<>><<<>^<<v^>^^^<^>^^^v<<>v^><<^^<<^v<<>^v>>vv>><<^v^^>v^v>^^v<><^^^<^^>v>^<>vvv^v^^^>v^^v<^>^^>>^v<><^><^<<^vv^<><<>v^vv^<<<^^>v<<>>>v<>v<><<^v>^^v>^^>v>^>^>v<>><>^>^>^vvvv<^^^^^v>v>><>^>^><>^^>v^^<<><^><<<<>v>^^>^v<<<>vvv>>v<^v>>v>v^<<<>^>^>^<>v<^^vv><^v<<^vv<^<<^^vv^^>vv<^>v>^^<^>v<<^^<^>^^^v^^>>v^vv^<^v>^<>^<^>>^<^v<>v><^^<><>^>v<^<^vv>><^v>^<>^^>^<><<>^<>><<>vvv^<<^^>>v<^>>vv>^v^^^v<>v<>><>^vv^>vv^ \ No newline at end of file diff --git a/day03/input.testing b/day03/input.testing new file mode 100644 index 0000000..8f30005 --- /dev/null +++ b/day03/input.testing @@ -0,0 +1 @@ +^v^v^v^v^v \ No newline at end of file diff --git a/day03/solution.rb b/day03/solution.rb new file mode 100644 index 0000000..b69deec --- /dev/null +++ b/day03/solution.rb @@ -0,0 +1,52 @@ +#!/bin/env ruby + +directions = File.read('./input') + +# Part 1, How many houses receive at least one present? +locations = [] +locations[0] = [0, 0] +directions.each_char do |direction| + locations.push(locations[-1].dup) + case direction + when '<' + locations[-1][0] -= 1 + when '>' + locations[-1][0] += 1 + when '^' + locations[-1][1] += 1 + when 'v' + locations[-1][1] -= 1 + end +end + +# I initially misread the prompt and made it too complicated +# location_count = Hash.new(0) +# locations.each { |location| location_count.store(location.to_s.to_sym, location_count[location.to_s.to_sym] + 1)} +# puts "Part 1 Solution: " + location_count.count.to_s +# puts "Part 1 Solution: " + location_count.select { |location, visits| visits > 1 }.count.to_s +puts 'Part 1 Solution: ' + locations.uniq.count.to_s + +# Part 2, The next year, to speed up the process, Santa creates a robot version of himself, Robo-Santa, to deliver presents with him. +# Santa and Robo-Santa start at the same location (delivering two presents to the same starting house), then take turns moving based on instructions from the elf, who is eggnoggedly reading from the same script as the previous year. +# This year, how many houses receive at least one present? + +santa_locations = [] +robo_locations = [] +santa_locations[0] = [0, 0] +robo_locations[0] = [0, 0] +locations = [santa_locations, robo_locations].cycle +directions.each_char do |direction| + locations.peek.push(locations.peek[-1].dup) + case direction + when '<' + locations.next[-1][0] -= 1 + when '>' + locations.next[-1][0] += 1 + when '^' + locations.next[-1][1] += 1 + when 'v' + locations.next[-1][1] -= 1 + end +end + +puts 'Part 2 Solution: ' + (robo_locations + santa_locations).uniq.count.to_s