Skip to content

Commit fa8fd7b

Browse files
committed
Day 24 2016
1 parent 57f45c6 commit fa8fd7b

File tree

4 files changed

+216
-0
lines changed

4 files changed

+216
-0
lines changed

2016/day-24/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Day 24: Air Duct Spelunking
2+
3+
You've finally met your match; the doors that provide access to the roof are locked tight, and all of the controls and related electronics are inaccessible. You simply can't reach them.
4+
5+
The robot that cleans the air ducts, however, can.
6+
7+
It's not a very fast little robot, but you reconfigure it to be able to interface with some of the exposed wires that have been routed through the HVAC system. If you can direct it to each of those locations, you should be able to bypass the security controls.
8+
9+
You extract the duct layout for this area from some blueprints you acquired and create a map with the relevant locations marked (your puzzle input). 0 is your current location, from which the cleaning robot embarks; the other numbers are (in no particular order) the locations the robot needs to visit at least once each. Walls are marked as #, and open passages are marked as .. Numbers behave like open passages.
10+
11+
For example, suppose you have a map like the following:
12+
13+
```
14+
###########
15+
#0.1.....2#
16+
#.#######.#
17+
#4.......3#
18+
###########
19+
```
20+
21+
To reach all of the points of interest as quickly as possible, you would have the robot take the following path:
22+
23+
* 0 to 4 (2 steps)
24+
* 4 to 1 (4 steps; it can't move diagonally)
25+
* 1 to 2 (6 steps)
26+
* 2 to 3 (2 steps)
27+
28+
Since the robot isn't very fast, you need to find it the shortest route. This path is the fewest steps (in the above example, a total of 14) required to start at 0 and then visit every other location at least once.
29+
30+
Given your actual map, and starting from location 0, what is the fewest number of steps required to visit every non-0 number marked on the map at least once?
31+
32+
### Part Two
33+
34+
Of course, if you leave the cleaning robot somewhere weird, someone is bound to notice.
35+
36+
What is the fewest number of steps required to start at 0, visit every non-0 number marked on the map at least once, and then return to 0?

2016/day-24/day-24-input.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#################################################################################################################################################################################
2+
#...................#.#.#.........#.........#...............#.....#.....#.........#.............#.............#...#.......#.......#...#...#.......#...#...#.#...#.........#...#.#
3+
#.#.#.#.#.#.#.#.#.#.#.#.#.###.###.#.###.#.#.#.#.#.#.#.#.###.#.#.#.#.#.#.#.#.#######.#####.###.#.###.#.#.#.#.#.#.#.#.###.#.#.#########.#.###.###.#.#.#####.#.#.#####.###.#.#.#.#.#
4+
#...#.....#.#...#...#...#.#...#...#.....#...#.#.....#...#.......#.#.....#...........#.........#.#.....#.#...#.........#.#.....#.........#...#.....#.....#.....#...#.#.....#.....#
5+
#.#.#.###.#.#.#.#.#.#.#.#.#.#.#.###.#####.#.###.###.#.###.###.#.#.#.#####.#####.#.#.#.#.###.#.#.#.#.#.#.#######.#.#.#####.#.#####.###.###.#.#.###.#.#.#.###.###.#.#.#.###.#.#.#.#
6+
#.#.#...#...#...........#.......#...#.#.#.#.....#.#.......#...#...#.#.....#...#...#.#.#...........#.#...#...#...#.....#...........#...#0....#...#.#.#.....#.......#.....#...#...#
7+
#.#.#.#.#.#.#.###########.###.###.###.#.#.#.#.#.#.#########.#####.#.#.###.#.#.#.#.#####.#.#.#.###.#.#.###.###.###.#.#.#.###.#.#.#.#.#######.#.#.#.#.#.###.#######.#######.#####.#
8+
#.....#...#...#.#3........#.......#.#...#.#.#...#.......#.#.......#.#...#...#.#.....#...#.....#...#.#.....#.#.......#.......#...............#...#.......#.......#.#.............#
9+
#.#.#.###.###.#.###.#.###.#.#.#.###.#.###.#.#.#######.#.#.###.#.###.#.#.#.#########.#.#.#####.#.#.#.#.#.#.#.#.#.#####.#####.#.###.###.###.###.#.#.###.#.#.###.###.###.#.###.#.###
10+
#...#.....#.......#.#.#...#.............#...........#...........#.....#.#.......#.....#.......#.......#.#.....#.....#.....#.....#.#...#.......#.......#.#.....#.#...#.....#...#.#
11+
#####.###.###.#.###.###.#.#.#####.#.###.#####.#.#.#.#########.#.#.###.#.#.#.#.#.#.###.###.#.#.###.###.#.#####.#.###.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#####.#.#####.#####.###.#.#.#.#
12+
#.....#.........#.......#.#.......#.#.....#...#...#.............#.....#.......#.#...#...#...#...#.....#...#.....#.....#...#.....#...#.#...#...#...#.....#.....#.......#.#...#...#
13+
#.#.#.#.###.#.#.#.#.#.#.#.#.#####.###.###.#####.#.#.#####.#.###.#.#.#.###.#.#.###.#.###.#.#.#.###.###.#.#.#.#########.###.#.#.#.#.#.#.#.#####.#.###.#.#.#.#.###.#.#.#.#.#.#####.#
14+
#.....#.#.....#.....#.....#.#...............#.#.........#.....#.#.....#.....#...#.....#.#.#.#.#...#...#...............#...#.#...#...#.#.#...#.#.#.....#.....#...#...#.#...#...#.#
15+
#.###.#.#.#.#.#.#.#.#.#####.#.#.###.#.#.###.#.#######.#.#.#.#.#.#.#########.###.#.###.###.#.#.#.#########.#.#.###.#.#.#.###.###.###.###.#.###.#.#.#.#.#.###.#.#.###.#.#.#.#.#.#.#
16+
#.#.#.#.#.#...#...#...#.........#...........#...#...#...#.....#...#.#.#.......#.#.....#...........#.........#.......#...#.#.#...#.........#1#.#...#.#.............#...#.........#
17+
###.#.#.###.#.#.#.#.#.#####.#########.###.#.#.#####.###.###.#.#.#.#.#.#.#.###.#.#.#.#.#.#####.#.#.#####.#######.###.#.#.#.#.#.#.#.###.#.#.#.#.###.#.#.#.#.#.###.#.#####.###.#####
18+
#...#.#.....#.....#.........#...#...#...#...#...#.......#.......#.....#.........#.#...#.....#.#...#.....#...#...#.#.#.......#.....#.....#.#...#.#.............#.#...........#.#.#
19+
#.#.#.#.#.###.###.#.#.###.###.###.#.#.#.#####.#.###.###.#.###.#######.###.#.###.#.#.#.#.#.#.#####.###.#.#.#.#.#.#.###########.#.#.#.#.#.#.#.#.#.#.###.###.#.###.#.#.#.###.#.#.#.#
20+
#.........#...#.....#.#...#...#...............#.....#.......#...#.....#...#.#...#.#...#.#.........#...#.#.#.....#.....#.......#.......#.....#.#...........#.....#.#...........#.#
21+
#####.#.#.#.#.#.###.#.#.#.#.#.#.#####.###.#.#.#.###.#.###.#.#.#.#.#.#.###.#.#.###.#.#.#.#.###.#.#.#.#####.#.#####.###.#.#.#.###.#.#.###.###.###.#####.#.#.#.#.#.#.#.###.#####.#.#
22+
#2..........#...#...#.#...#.#.............#.#.#.......#...#.....#...........#.#...#.#.#.....#.#...#.#.#.........#...#...#.#...#.....#.........#...#...#...#.....#.....#.....#...#
23+
#####.#.#.###.#.#####.###.#.#.#.###.###.###.###.#.###.#.#.#####.#.#.#########.#.###.#.#####.#.#####.#.#.###.#.#.#.#.###.###.#.###.#######.#############.###.###.###.###.###.#.###
24+
#.#.......#...#...#...#.......#...........#...#.#.....#...#.......#.....#...#.....#.#.......#.........#...#.#.....#.#...#.............#.....#.............#.#...#.#...#.......#.#
25+
#.#.#.#####.#.#.###.###.###.#.#.#.#.#.#####.#.#.#.#.###.###.#.#####.#.#####.#.#.###.#.#####.#.###.###.###.#.###.#.#.#.#.#.#########.###.###.#######.###.#.#.#.###.#####.###.#.#.#
26+
#.....#.#...#.#.....#...#.....#...#...#...#.#...#...#.#.......#.#...#.......#.#.#.............#...#...#...#.....#.#.........#...#.#.#...#...#...........#...#.#7#.#.....#.....#.#
27+
###.#.#.#.#.#.#.#.#.#.#.#.###.#.#.###.#.#.#.###.#.#.#.#.#.#####.#.#######.#.###.#.#.#.#.#.#####.###.#.#.#.#.#.#.#.#.#.#.#.###.###.#.#.#.#.###.#.#.###.#.#.#.###.#.###.#.#.###.#.#
28+
#.#...#.....#.#...#.#.......#.#.......#.........#.#.#.#...............#.....#...#.#.........#.......#.#.#.#...#...#.#.#.....#.....#.#.#...#...#...#.#.#.....#...#.....#...#...#.#
29+
#.#####.#######.#.#########.#.#.###.###.#.#####.#.#.#.#.#.#.#.#.#.#.#.#.#.#.#.###.#.#####.###.#.#.#.#.#.###.#######.#.#.###.#.#.#.###.###.#.###.###.#.#.###.#.###.#.#.#.#####.#.#
30+
#.#.........#.#.#...#.....#.....#...#.............#...#...#.#.......#...#.#.......#.......#...#.....#...#...#.............#.#.........#...#...#...........#...#.......#.#.....#.#
31+
#.#.#.#######.#.#.#.###.#.#.#.#.###.#.#.#.#.#.#.#.#.#####.#.#.###.#.#.#.#.#.#.#.#.###.###.###.#####.#.#.#####.#.#######.#.#.#.#.###.#.#.#.#.###.#.#.#.#.#.#####.#.#.#.#######.#.#
32+
#...#.#.......#.#...#...........#...#.......#.....#...#.....#.........#.....#...#.........#.#...#...#...#.....#.......#...#.............#.#.........#.....#.....#.....#.#...#.#.#
33+
#.###.#.###.#.###.#.###########.#######.###.#########.#.#.#.#.#.###.###.#.#.#.###.#.###.###.#.#.#.#.#.#.#.###.#.#####.#####.#.###.###.#.#.#.#######.###.#.#.#####.###.#.#.###.#.#
34+
#.#.....#.....#.#.....#4........#...#...#.#.....#...#.#.#...#.#.........#.....#.......#.#.......#.#.....#.....#...............#.....#.#.#.#...#.........#...#...#.........#...#.#
35+
###.#####.#.#.#.#########.#.#.#####.#.#.#.###.###.#.#.#.#.#.###.#.#.###.###.#.###.###.#.###########.###.#.#.#.#.###.#####.#.#.#.#.###.###.#.#.#.#.#.#.#.#.###.###.#.###.#.#.#.#.#
36+
#.........#.#.....#.....#.#.....#.#.#.#...#.....#.......#...#.#...#.....#.......#.....#.........#.......#...#.#.#.......#.......#...#...#...#...#.#.#...#.....#...#...#...#.....#
37+
#.#.###.###.###.###.#.###.#.#.###.###.###.#.#.#.#.#####.###.#.#.#.#.#.###.#.#####.#.###.#.#.#.#.#.#.###.#.###.#.#.#.#.#########.#.#####.#.#.#.#.#####.#.#.#######.#.#.###.###.#.#
38+
#.............#.#...#.#.....#.#...#.........#.....#...#.......#...#.......#.......#...........#...#...#.#...#...#.....#...........#.....#.#...#.....#.....#.#.........#.....#.#.#
39+
#.#.#.###.#.#.#####.###.#.#.#.#.#.#.#.#####.###.#.#.#####.#.#.#.#.#####.#.###.#####.#############.#.#.###.#.#.#.#####.#.#.#.#.#.#.#.###.###.###.#.#.#.###.#.#.#.#.#.#.###.#.###.#
40+
#.#...#.#.#.....#.............#...#.........#...#.#...#...#...#...#...........#.#.#.#.....#...#.....#.#.#...#...#.#.........#.....#.........#...........#.....#...#...#.......#.#
41+
#.#.#.#.#.###.#.#.###.#.#######.#.#.#.#.#.#.#.#.#.###.#.###.###.###.#####.#.###.#.#.###.#.#.#######.#.#.#.###.###.#.###.#####.#.#.#####.#.#.#.#.#####.#.#.#####.#.#####.#########
42+
#.......#.....#...#...#.....#...........#...#.#...#.........#...#.....#...........#.....#.#.....#...#...#.#...#...#...#.....#...........#...#.#...#...#.....#.....#...........#6#
43+
#.#.#.#.#######.###.#.#.###.#.#.###.#####.#.###.#.#######.#.#####.#.#.#.#########.#.###.#.#.#####.#.#.###.#.#########.###.#.#####.###.#.###.#.#.#.###.#.###.#.#.#.#.#.#.#.#####.#
44+
#.....#.............#...#.#...#...#.#5......#...#...#.....#.............#.#.......#.....#.....#...........#.........#...#.#.....#.#.#.#...#...........#.#.#.......#.............#
45+
#################################################################################################################################################################################

2016/day-24/day-24-part-1.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'set'
4+
5+
file_path = File.expand_path("../day-24-input.txt", __FILE__)
6+
input = File.read(file_path)
7+
8+
maze = input.split("\n").map { |line| line.chars }
9+
10+
targets = {}
11+
12+
maze.each_with_index do |row, y|
13+
row.each_with_index do |cell, x|
14+
targets[cell] = [x, y] if cell =~ /\d/
15+
end
16+
end
17+
18+
distances = Hash.new { |h, k| h[k] = {} }
19+
20+
(targets.to_a).product(targets.to_a).each do |(s, (sx, sy)), (g, (gx, gy))|
21+
next if s == g || distances[s][g]
22+
23+
visited = Set.new
24+
queue = [[sx, sy, 0]]
25+
26+
visited.add(queue.first[0..1])
27+
reached_goal = false
28+
29+
while !reached_goal
30+
current = queue.shift
31+
32+
if current[0..1] == [gx, gy]
33+
distances[s][g] = current[2]
34+
distances[g][s] = current[2]
35+
36+
reached_goal = true
37+
end
38+
39+
x = current[0]
40+
y = current[1]
41+
step = current[2]
42+
43+
[[x+1, y], [x-1, y], [x, y+1], [x, y-1]].each do |x, y|
44+
next if x < 0 || y < 0
45+
next if maze[y][x] == "#"
46+
next if visited.include?([x, y])
47+
48+
visited.add([x, y])
49+
new_location = [x, y, step + 1]
50+
queue << new_location
51+
end
52+
end
53+
end
54+
55+
lowest = 1_000
56+
57+
(1..7).map(&:to_s).permutation.each do |sequence|
58+
sequence.unshift("0")
59+
60+
sum = sequence.each_cons(2).
61+
map { |start, goal| distances[start][goal] }.
62+
inject(:+)
63+
64+
lowest = [lowest, sum].min
65+
end
66+
67+
puts lowest

2016/day-24/day-24-part-2.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'set'
4+
5+
file_path = File.expand_path("../day-24-input.txt", __FILE__)
6+
input = File.read(file_path)
7+
8+
maze = input.split("\n").map { |line| line.chars }
9+
10+
targets = {}
11+
12+
maze.each_with_index do |row, y|
13+
row.each_with_index do |cell, x|
14+
targets[cell] = [x, y] if cell =~ /\d/
15+
end
16+
end
17+
18+
distances = Hash.new { |h, k| h[k] = {} }
19+
20+
(targets.to_a).product(targets.to_a).each do |(s, (sx, sy)), (g, (gx, gy))|
21+
next if s == g || distances[s][g]
22+
23+
visited = Set.new
24+
queue = [[sx, sy, 0]]
25+
26+
visited.add(queue.first[0..1])
27+
reached_goal = false
28+
29+
while !reached_goal
30+
current = queue.shift
31+
32+
if current[0..1] == [gx, gy]
33+
distances[s][g] = current[2]
34+
distances[g][s] = current[2]
35+
36+
reached_goal = true
37+
end
38+
39+
x = current[0]
40+
y = current[1]
41+
step = current[2]
42+
43+
[[x+1, y], [x-1, y], [x, y+1], [x, y-1]].each do |x, y|
44+
next if x < 0 || y < 0
45+
next if maze[y][x] == "#"
46+
next if visited.include?([x, y])
47+
48+
visited.add([x, y])
49+
new_location = [x, y, step + 1]
50+
queue << new_location
51+
end
52+
end
53+
end
54+
55+
lowest = 1_000
56+
57+
(1..7).map(&:to_s).permutation.each do |sequence|
58+
sequence.unshift("0")
59+
sequence << "0"
60+
61+
sum = sequence.each_cons(2).
62+
map { |start, goal| distances[start][goal] }.
63+
inject(:+)
64+
65+
lowest = [lowest, sum].min
66+
end
67+
68+
puts lowest

0 commit comments

Comments
 (0)