aboutsummaryrefslogtreecommitdiff
path: root/day13/part1
diff options
context:
space:
mode:
authorNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-13 23:40:17 +0000
committerNat Lasseter <Nat Lasseter nathan@bytemark.co.uk>2017-12-13 23:40:17 +0000
commit3d1ca6fb0a6741799b1e03c3ac15d94b85facbd5 (patch)
tree4a47a6c22154a77cca5373ab49fa0574ecd710b0 /day13/part1
parentd011cdb55f13c55ffc6bb4b854ef30c2561f2a7c (diff)
Day13, Part2 does not work
Diffstat (limited to 'day13/part1')
-rwxr-xr-xday13/part170
1 files changed, 70 insertions, 0 deletions
diff --git a/day13/part1 b/day13/part1
new file mode 100755
index 0000000..9af71a6
--- /dev/null
+++ b/day13/part1
@@ -0,0 +1,70 @@
+#!/usr/bin/env ruby
+
+class Firewall
+ class Layer
+ attr_reader :range
+
+ def initialize(range)
+ @scanner_location = 0
+ @scanner_velocity = 1
+ @range = range
+ end
+
+ def move_scanner!
+ if @scanner_location == (@range - 1) then
+ @scanner_velocity = -1
+ elsif @scanner_location == 0 then
+ @scanner_velocity = 1
+ end
+
+ @scanner_location += @scanner_velocity
+ end
+
+ def capturing?
+ return @scanner_location == 0
+ end
+ end
+
+ def initialize
+ @layers = []
+ end
+
+ def add_layer!(depth, range)
+ @layers[depth] = Layer.new(range)
+ end
+
+ def total_depth
+ return @layers.length
+ end
+
+ def severity(loc)
+ return 0 if @layers[loc].nil?
+ return 0 unless @layers[loc].capturing?
+ return loc * @layers[loc].range
+ end
+
+ def tick!
+ @layers.compact.each(&:move_scanner!)
+ end
+end
+
+
+input = $stdin.readlines.map(&:chomp)
+
+firewall = Firewall.new
+
+input.each do |line|
+ depth, range = line.split(': ').map(&:to_i)
+ firewall.add_layer!(depth, range)
+end
+
+myloc = -1
+total_severity = 0
+
+while myloc < firewall.total_depth do
+ myloc += 1
+ total_severity += firewall.severity(myloc)
+ firewall.tick!
+end
+
+puts total_severity