Skip to content

Commit 0cc712b

Browse files
committed
Day 18 part 1 solutions
1 parent abc6b55 commit 0cc712b

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Diff for: 18-1.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
import numpy
4+
5+
6+
class Map:
7+
def __init__(self, max_x, max_y, max_z):
8+
self.size_x = max_x + 1
9+
self.size_y = max_y + 1
10+
self.size_z = max_z + 1
11+
self.map = numpy.zeros((self.size_x, self.size_y, self.size_z), dtype=numpy.uint8)
12+
13+
def count_exposed_faces(self):
14+
count = 0
15+
for x in range(self.size_x):
16+
for y in range(self.size_y):
17+
for z in range(self.size_z):
18+
if self.map[x, y, z] == 1:
19+
if x == 0 or self.map[x - 1, y, z] == 0:
20+
count += 1
21+
if x == self.size_x - 1 or self.map[x + 1, y, z] == 0:
22+
count += 1
23+
if y == 0 or self.map[x, y - 1, z] == 0:
24+
count += 1
25+
if y == self.size_y - 1 or self.map[x, y + 1, z] == 0:
26+
count += 1
27+
if z == 0 or self.map[x, y, z - 1] == 0:
28+
count += 1
29+
if z == self.size_z - 1 or self.map[x, y, z + 1] == 0:
30+
count += 1
31+
return count
32+
33+
34+
lines = [list(map(lambda c: int(c), line.strip().split(","))) for line in open('18.input').readlines()]
35+
36+
max_x = max([x for x, _, _ in lines])
37+
max_y = max([y for _, y, _ in lines])
38+
max_z = max([z for _, _, z in lines])
39+
40+
obj = Map(max_x, max_y, max_z)
41+
42+
for coords in lines:
43+
obj.map[coords[0], coords[1], coords[2]] = 1
44+
45+
print(obj.count_exposed_faces())

Diff for: 18-1.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'numo/narray'
4+
5+
class Map
6+
def initialize(max_x, max_y, max_z)
7+
@size_x = max_x + 1
8+
@size_y = max_y + 1
9+
@size_z = max_z + 1
10+
@map = Numo::UInt8.zeros(@size_x, @size_y, @size_z)
11+
end
12+
13+
def [](x, y, z)
14+
@map[x, y, z]
15+
end
16+
17+
def []=(x, y, z, val)
18+
@map[x, y, z] = val
19+
end
20+
21+
def count_exposed_faces
22+
count = 0
23+
@size_x.times do |x|
24+
@size_y.times do |y|
25+
@size_z.times do |z|
26+
next unless @map[x, y, z] == 1
27+
28+
count += 1 if x == 0 or @map[x - 1, y, z] == 0
29+
count += 1 if x == @size_x - 1 or @map[x + 1, y, z] == 0
30+
count += 1 if y == 0 or @map[x, y - 1, z] == 0
31+
count += 1 if y == @size_y - 1 or @map[x, y + 1, z] == 0
32+
count += 1 if z == 0 or @map[x, y, z - 1] == 0
33+
count += 1 if z == @size_z - 1 or @map[x, y, z + 1] == 0
34+
end
35+
end
36+
end
37+
count
38+
end
39+
end
40+
41+
input = File.read('18.input').lines.map(&:strip).map { |l| l.split(',').map(&:to_i) }
42+
43+
max_x = input.map(&:first).max
44+
max_y = input.map { |c| c[1] }.max
45+
max_z = input.map(&:last).max
46+
47+
map = Map.new(max_x, max_y, max_z)
48+
49+
input.each do |coords|
50+
map[coords[0], coords[1], coords[2]] = 1
51+
end
52+
53+
print map.count_exposed_faces, "\n"

0 commit comments

Comments
 (0)