From 4386d976f17746e1da8bb81348548cfe552beeec Mon Sep 17 00:00:00 2001 From: s00ner Date: Fri, 15 Jul 2022 08:13:57 -0400 Subject: [PATCH] Formatting --- day06/solution.rb | 70 +++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 33 deletions(-) diff --git a/day06/solution.rb b/day06/solution.rb index 2c514a6..05974d7 100644 --- a/day06/solution.rb +++ b/day06/solution.rb @@ -5,51 +5,55 @@ input = File.readlines('./input').map(&:strip) lights = Array.new(1000) { Array.new(1000, false) } -instructions = Hash.new +instructions = {} -instructions = input.map.with_index do |line, i| - line.slice!('turn') - line = line.split(' ') - { Command: line[0], Start: line[1].split(',').map!(&:to_i), End: line[3].split(',').map!(&:to_i) } +instructions = input.map.with_index do |line, _i| + line.slice!('turn') + line = line.split(' ') + { Command: line[0], Start: line[1].split(',').map!(&:to_i), End: line[3].split(',').map!(&:to_i) } end -instructions.each do | instruction | - x = instruction[:End][0] - instruction[:Start][0] + 1 - y = instruction[:End][1] - instruction[:Start][1] + 1 - y.times do |y| - x.times do |x| - case instruction[:Command] - when 'on' - lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = true - when 'off' - lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = false - when 'toggle' - lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = !(lights[instruction[:Start][0] + x][ instruction[:Start][1] + y]) - end - end +instructions.each do |instruction| + x = instruction[:End][0] - instruction[:Start][0] + 1 + y = instruction[:End][1] - instruction[:Start][1] + 1 + y.times do |y| + x.times do |x| + case instruction[:Command] + when 'on' + lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = true + when 'off' + lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = false + when 'toggle' + lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = + !(lights[instruction[:Start][0] + x][ instruction[:Start][1] + y]) + end end + end end puts "Part 1 Solution: #{lights.flatten.count(true)}" # Part 2 (sloppy style) lights = Array.new(1000) { Array.new(1000, 0) } -instructions.each do | instruction | - x = instruction[:End][0] - instruction[:Start][0] + 1 - y = instruction[:End][1] - instruction[:Start][1] + 1 - y.times do |y| - x.times do |x| - case instruction[:Command] - when 'on' - lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] += 1 - when 'off' - lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] -= 1 - lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = 0 if lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] < 0 - when 'toggle' - lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] += 2 - end +instructions.each do |instruction| + x = instruction[:End][0] - instruction[:Start][0] + 1 + y = instruction[:End][1] - instruction[:Start][1] + 1 + y.times do |y| + x.times do |x| + case instruction[:Command] + when 'on' + lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] += 1 + when 'off' + lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] -= 1 + if lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] < 0 + lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] = + 0 end + when 'toggle' + lights[instruction[:Start][0] + x][ instruction[:Start][1] + y] += 2 + end end + end end puts "Part 2 Solution: #{lights.flatten.sum}"