Last active
December 6, 2015 20:42
-
-
Save LOZORD/1a79605c47c70ec0ecbd to your computer and use it in GitHub Desktop.
My solutions to adventofcode.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flr = 0 | |
cc = 1 | |
basement_char = nil | |
File.open('parens.txt', 'r') do |file| | |
file.each_line do |line| | |
line.chars.each do |c| | |
if c == '(' | |
flr += 1 | |
elsif c == ')' | |
flr -= 1 | |
else | |
fail '!!!unknown input ' + c | |
end | |
if flr < 0 && !basement_char | |
basement_char = cc | |
end | |
cc += 1 | |
end | |
end | |
file.close | |
end | |
puts flr.to_s + ' FLR' | |
puts basement_char.to_s + ' BCH' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Gift | |
attr_reader :l, :w, :h, :smallest_side_dims | |
def initialize(s) | |
dims = s.split('x').map(&:to_i) | |
fail 'BAD INPUT' unless dims.size == 3 | |
@l = dims[0] | |
@w = dims[1] | |
@h = dims[2] | |
@smallest_side_dims = dims.sort.take 2 | |
end | |
# paper stuff | |
def surface_area | |
2*l*w + 2*w*h + 2*h*l | |
end | |
def extra_paper | |
smallest_side_dims.reduce(:*) | |
end | |
def paper_needed | |
surface_area + extra_paper | |
end | |
# ribbon stuff | |
def ribbon_wrap | |
smallest_side_dims.map { |dim| dim * 2 }.reduce(:+) | |
end | |
def ribbon_bow | |
l * w * h | |
end | |
def ribbon_needed | |
ribbon_wrap + ribbon_bow | |
end | |
end | |
$gifts = [] | |
File.open('giftdims.txt', 'r') do |file| | |
file.each_line do |line| | |
$gifts << Gift.new(line.chomp) | |
end | |
file.close | |
end | |
total_paper_needed = $gifts.map(&:paper_needed).reduce(:+) | |
total_ribbon_needed = $gifts.map(&:ribbon_needed).reduce(:+) | |
puts total_paper_needed | |
puts total_ribbon_needed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.Set as Set | |
type Coord = (Int, Int) | |
travelToHouses :: [Char] -> Set.Set Coord -> Coord -> (Set.Set Coord, Coord) | |
travelToHouses [] set pos = (set, pos) | |
travelToHouses (c:rest) set (x, y) | |
| c == '^' = travelToHouses rest (Set.union set (Set.singleton (x, y + 1))) (x, y + 1) | |
| c == 'v' = travelToHouses rest (Set.union set (Set.singleton (x, y - 1))) (x, y - 1) | |
| c == '>' = travelToHouses rest (Set.union set (Set.singleton (x + 1, y))) (x + 1, y) | |
| c == '<' = travelToHouses rest (Set.union set (Set.singleton (x - 1, y))) (x - 1, y) | |
| otherwise = undefined | |
runRoute :: [Char] -> (Set.Set Coord) | |
runRoute route = fst $ travelToHouses route (Set.singleton (0, 0)) (0, 0) | |
main :: IO() | |
main = do | |
contents <- readFile "house-directions.txt" | |
putStrLn $ show $ length $ contents | |
let houseSet = runRoute contents | |
putStrLn $ show $ Set.size houseSet | |
let zipped = zip [0..] contents | |
let santaRoute = map (snd) $ filter (even.fst) zipped | |
let robotRoute = map (snd) $ filter (odd.fst) zipped | |
let santaSet = runRoute santaRoute | |
let robotSet = runRoute robotRoute | |
putStrLn $ show $ (Set.size) $ (Set.union santaSet robotSet) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' | |
KEY = 'ckczppom' | |
def hacked?(hex_str, prefix_size) | |
hex_str[0, prefix_size].chars.all? { |c| c == '0' } | |
end | |
def hack(prefix_size, report = false) | |
myMD5 = Digest::MD5.new | |
num = 1 | |
loop do | |
num_s = num.to_s | |
puts ('loop ' + num_s) if (num % 100 == 0) && report | |
hash = myMD5.hexdigest(KEY + num_s) | |
if hacked?(hash, prefix_size) | |
break | |
else | |
num += 1 | |
end | |
end | |
return num | |
end | |
puts 'ANSWER 1: ' + hack(5).to_s | |
puts 'ANSWER 2: ' + hack(6).to_s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def count_vowels | |
self.count "aeiou" | |
end | |
def has_repeat? | |
self.chars.chunk { |c| c }.map {|_, ary| ary.size >= 2}.any? | |
end | |
def contains_naughty_pair? | |
%w(ab cd pq xy).any? { |p| self.include? p } | |
end | |
def is_nice? | |
(self.count_vowels >= 3) && (self.has_repeat?) && (!self.contains_naughty_pair?) | |
end | |
end | |
File.open('word-list.txt', 'r') do |file| | |
nice_count = 0 | |
file.each_line do |line| | |
# puts line.chomp | |
nice_count += (line.chomp.is_nice? ? 1 : 0) | |
end | |
puts nice_count | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def satisfies_rule1? | |
self.chars.each_with_index do |c, i| | |
return false if self.size == i + 1 | |
pair = self[i] + self[i + 1] | |
rind = self.rindex(pair) | |
if !rind.nil? && (i + 1) < rind | |
return true | |
end | |
end | |
return false | |
end | |
def satisfies_rule2? | |
self.chars.each_with_index do |c, i| | |
if self[i + 2] == c | |
return true | |
end | |
end | |
return false | |
end | |
def is_nice? | |
self.satisfies_rule1? && self.satisfies_rule2? | |
end | |
end | |
File.open('word-list.txt', 'r') do |file| | |
nice_count = 0 | |
file.each_line do |line| | |
nice_count += (line.chomp.is_nice? ? 1 : 0) | |
end | |
puts nice_count | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def parse_command(str) | |
rgx = /(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)/ | |
matches = rgx.match str | |
fail 'NO MATCH' if matches.nil? | |
return { | |
op: matches[1], | |
coord1: { | |
x: matches[2].to_i, | |
y: matches[3].to_i | |
}, | |
coord2: { | |
x: matches[4].to_i, | |
y: matches[5].to_i | |
} | |
} | |
end | |
def perform_op1(op, matrix, i, j) | |
if op == 'turn on' | |
matrix[i][j] = 1 | |
elsif op == 'turn off' | |
matrix[i][j] = 0 | |
elsif op == 'toggle' | |
old_val = matrix[i][j] | |
matrix[i][j] = old_val == 1 ? 0 : 1 | |
else | |
fail 'UNKNOWN OP' | |
end | |
matrix | |
end | |
def perform_op2(op, matrix, i, j) | |
if op == 'turn on' | |
matrix[i][j] += 1 | |
elsif op == 'turn off' | |
matrix[i][j] = [0, matrix[i][j] - 1].max | |
elsif op == 'toggle' | |
matrix[i][j] += 2 | |
else | |
fail 'UNKNOWN OP' | |
end | |
matrix | |
end | |
def exec_command(cmd, matrix, mode = 1) | |
x_range = (cmd[:coord1][:x]..cmd[:coord2][:x]) | |
y_range = (cmd[:coord1][:y]..cmd[:coord2][:y]) | |
op = cmd[:op] | |
x_range.each do |j| | |
y_range.each do |i| | |
perform_op1(op, matrix, i, j) if mode == 1 | |
perform_op2(op, matrix, i, j) if mode == 2 | |
end | |
end | |
end | |
DIM = 1000 | |
$M = Array.new(DIM) { Array.new(DIM, 0) } | |
File.open('light-instructions.txt', 'r') do |file| | |
ctr = 0 | |
file.each_line do |line| | |
cmd = parse_command(line.chomp) | |
# change mode (last arg) here for part of challenge | |
exec_command(cmd, $M, 2) | |
# puts (ctr += 1).to_s | |
end | |
end | |
onCount = $M.map { |row| row.reduce(:+) }.reduce(:+) | |
puts 'ANSWER: ' + onCount.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment