Skip to content

Commit d602552

Browse files
committed
Added day 1 part 1.
1 parent 879e6dc commit d602552

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

2016/day01/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# [Day 1: No Time for a Taxicab](http://adventofcode.com/2016/day/1)
2+
3+
Santa's sleigh uses a very high-precision clock to guide its movements, and the clock's oscillator is regulated by stars. Unfortunately, the stars have been stolen... by the Easter Bunny. To save Christmas, Santa needs you to retrieve all *fifty stars* by December 25th.
4+
5+
Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants *one star*. Good luck!
6+
7+
You're airdropped near _Easter Bunny Headquarters_ in a city somewhere. "Near", unfortunately, is as close as you can get - the instructions on the Easter Bunny Recruiting Document the Elves intercepted start here, and nobody had time to work them out further.
8+
9+
The Document indicates that you should start at the given coordinates (where you just landed) and face North. Then, follow the provided sequence: either turn left (L) or right (R) 90 degrees, then walk forward the given number of blocks, ending at a new intersection.
10+
11+
There's no time to follow such ridiculous instructions on foot, though, so you take a moment and work out the destination. Given that you can only walk on the [street grid of the city](https://en.wikipedia.org/wiki/Taxicab_geometry), how far is the shortest path to the destination?
12+
13+
For example:
14+
15+
* Following R2, L3 leaves you 2 blocks East and 3 blocks North, or 5 blocks away.
16+
* R2, R2, R2 leaves you 2 blocks due South of your starting position, which is 2 blocks away.
17+
* R5, L5, R5, R3 leaves you 12 blocks away.
18+
19+
_How many blocks away_ is Easter Bunny HQ?

2016/day01/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
R3, L2, L2, R4, L1, R2, R3, R4, L2, R4, L2, L5, L1, R5, R2, R2, L1, R4, R1, L5, L3, R4, R3, R1, L1, L5, L4, L2, R5, L3, L4, R3, R1, L3, R1, L3, R3, L4, R2, R5, L190, R2, L3, R47, R4, L3, R78, L1, R3, R190, R4, L3, R4, R2, R5, R3, R4, R3, L1, L4, R3, L4, R1, L4, L5, R3, L3, L4, R1, R2, L4, L3, R3, R3, L2, L5, R1, L4, L1, R5, L5, R1, R5, L4, R2, L2, R1, L5, L4, R4, R4, R3, R2, R3, L1, R4, R5, L2, L5, L4, L1, R4, L4, R4, L4, R1, R5, L1, R1, L5, R5, R1, R1, L3, L1, R4, L1, L4, L4, L3, R1, R4, R1, R1, R2, L5, L2, R4, L1, R3, L5, L2, R5, L4, R5, L5, R3, R4, L3, L3, L2, R2, L5, L5, R3, R4, R3, R4, R3, R1

2016/day01/part1.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'pry'
2+
input = File.read("input.txt")
3+
4+
lr_blocks = 0
5+
ns_blocks = 0
6+
orientation = "N"
7+
8+
input.split(',').each do |c|
9+
d = c.strip.split(/(?<=[a-zA-Z])/)
10+
if orientation == "N"
11+
if d[0] == "R"
12+
orientation = "E"
13+
lr_blocks = lr_blocks + d[1].to_i
14+
elsif d[0] == "L"
15+
orientation = "W"
16+
lr_blocks = lr_blocks - d[1].to_i
17+
end
18+
elsif orientation == "E"
19+
if d[0] == "R"
20+
orientation = "S"
21+
ns_blocks = ns_blocks - d[1].to_i
22+
elsif d[0] == "L"
23+
ns_blocks = ns_blocks + d[1].to_i
24+
orientation = "N"
25+
end
26+
elsif orientation == "S"
27+
if d[0] == "R"
28+
lr_blocks = lr_blocks - d[1].to_i
29+
orientation = "W"
30+
elsif d[0] == "L"
31+
orientation = "E"
32+
lr_blocks = lr_blocks + d[1].to_i
33+
end
34+
else
35+
if d[0] == "R"
36+
orientation = "N"
37+
ns_blocks = ns_blocks + d[1].to_i
38+
elsif d[0] == "L"
39+
ns_blocks = ns_blocks - d[1].to_i
40+
orientation = "S"
41+
end
42+
end
43+
end
44+
total_blocks = lr_blocks + ns_blocks
45+
puts "The Easter Bunny HQ is #{total_blocks} blocks away.\n"

0 commit comments

Comments
 (0)