aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNat Lasseter <user@4574.co.uk>2024-11-07 22:27:13 +0000
committerNat Lasseter <user@4574.co.uk>2024-11-07 22:27:13 +0000
commit7d86157dfe52a76e03694be4592e32d9dec69ed9 (patch)
tree2e322463a0c5a15c09a78954334753d374316e31
parentd7d62da5e9c2ca991885641e0a55c2907eb0fb7e (diff)
Do a bunch more in rubyHEADmaster
-rw-r--r--rb/day15.input4
-rwxr-xr-xrb/day15.rb64
-rw-r--r--rb/day17.input20
-rwxr-xr-xrb/day17.rb21
-rw-r--r--rb/day19.input45
-rwxr-xr-xrb/day19.rb48
-rw-r--r--rb/day20.input1
-rwxr-xr-xrb/day20.rb25
8 files changed, 228 insertions, 0 deletions
diff --git a/rb/day15.input b/rb/day15.input
new file mode 100644
index 0000000..6281182
--- /dev/null
+++ b/rb/day15.input
@@ -0,0 +1,4 @@
+Sprinkles: capacity 5, durability -1, flavor 0, texture 0, calories 5
+PeanutButter: capacity -1, durability 3, flavor 0, texture 0, calories 1
+Frosting: capacity 0, durability -1, flavor 4, texture 0, calories 6
+Sugar: capacity -1, durability 0, flavor 0, texture 2, calories 8
diff --git a/rb/day15.rb b/rb/day15.rb
new file mode 100755
index 0000000..1400862
--- /dev/null
+++ b/rb/day15.rb
@@ -0,0 +1,64 @@
+#!/usr/bin/env ruby
+
+class Ingredient
+ def initialize(nam, cap, dur, fla, tex, cal)
+ @name = nam
+ @capacity = cap
+ @durability = dur
+ @flavour = fla
+ @texture = tex
+ @calories = cal
+ end
+
+ attr_reader :capacity, :durability, :flavour, :texture, :calories
+
+ def self.parse(line)
+ name, data = line.strip.split(": ")
+ new(name, *data.scan(/(-?\d+)/).flatten.map(&:to_i))
+ end
+end
+
+def cookiescore(zip)
+ cap = zip.map { |qty, ing| qty * ing.capacity }.sum
+ cap = 0 if cap < 0
+ dur = zip.map { |qty, ing| qty * ing.durability }.sum
+ dur = 0 if dur < 0
+ fla = zip.map { |qty, ing| qty * ing.flavour }.sum
+ fla = 0 if fla < 0
+ tex = zip.map { |qty, ing| qty * ing.texture }.sum
+ tex = 0 if tex < 0
+
+ cap * dur * fla * tex
+end
+
+def cookiecal(zip)
+ zip.map { |qty, ing| qty * ing.calories }.sum
+end
+
+
+ingredients = File
+ .readlines("day15.input")
+ .map { |line| Ingredient.parse(line) }
+
+recipies = (0..100).to_a
+ .permutation(ingredients.length)
+ .select { |s| s.sum == 100 }
+
+
+bestscore = 0
+recipies.each do |recipe|
+ zip = recipe.zip(ingredients)
+ score = cookiescore(zip)
+ bestscore = score if score > bestscore
+end
+puts "Part 1: #{bestscore}"
+
+
+bestscore = 0
+recipies.each do |recipe|
+ zip = recipe.zip(ingredients)
+ next unless cookiecal(zip) == 500
+ score = cookiescore(zip)
+ bestscore = score if score > bestscore
+end
+puts "Part 2: #{bestscore}"
diff --git a/rb/day17.input b/rb/day17.input
new file mode 100644
index 0000000..588b253
--- /dev/null
+++ b/rb/day17.input
@@ -0,0 +1,20 @@
+50
+44
+11
+49
+42
+46
+18
+32
+26
+40
+21
+7
+18
+43
+10
+47
+36
+24
+22
+40
diff --git a/rb/day17.rb b/rb/day17.rb
new file mode 100755
index 0000000..e47768a
--- /dev/null
+++ b/rb/day17.rb
@@ -0,0 +1,21 @@
+#!/usr/bin/env ruby
+
+STORE = 150
+
+jugs = File.readlines("day17.input").map(&:to_i)
+
+numsols = 0
+(1..jugs.length).each do |i|
+ numsols += jugs.combination(i).map(&:sum).count(STORE)
+end
+
+puts "Part 1: #{numsols}"
+
+minjugs = jugs.length + 1
+(1..jugs.length).each do |i|
+ min = jugs.combination(i).select { |s| s.sum == STORE }.map(&:length).min
+ minjugs = min if min && min < minjugs
+end
+
+numsols = jugs.combination(minjugs).map(&:sum).count(STORE)
+puts "Part 2: #{numsols}"
diff --git a/rb/day19.input b/rb/day19.input
new file mode 100644
index 0000000..b0515c0
--- /dev/null
+++ b/rb/day19.input
@@ -0,0 +1,45 @@
+Al => ThF
+Al => ThRnFAr
+B => BCa
+B => TiB
+B => TiRnFAr
+Ca => CaCa
+Ca => PB
+Ca => PRnFAr
+Ca => SiRnFYFAr
+Ca => SiRnMgAr
+Ca => SiTh
+F => CaF
+F => PMg
+F => SiAl
+H => CRnAlAr
+H => CRnFYFYFAr
+H => CRnFYMgAr
+H => CRnMgYFAr
+H => HCa
+H => NRnFYFAr
+H => NRnMgAr
+H => NTh
+H => OB
+H => ORnFAr
+Mg => BF
+Mg => TiMg
+N => CRnFAr
+N => HSi
+O => CRnFYFAr
+O => CRnMgAr
+O => HP
+O => NRnFAr
+O => OTi
+P => CaP
+P => PTi
+P => SiRnFAr
+Si => CaSi
+Th => ThCa
+Ti => BP
+Ti => TiTi
+e => HF
+e => NAl
+e => OMg
+
+CRnSiRnCaPTiMgYCaPTiRnFArSiThFArCaSiThSiThPBCaCaSiRnSiRnTiTiMgArPBCaPMgYPTiRnFArFArCaSiRnBPMgArPRnCaPTiRnFArCaSiThCaCaFArPBCaCaPTiTiRnFArCaSiRnSiAlYSiThRnFArArCaSiRnBFArCaCaSiRnSiThCaCaCaFYCaPTiBCaSiThCaSiThPMgArSiRnCaPBFYCaCaFArCaCaCaCaSiThCaSiRnPRnFArPBSiThPRnFArSiRnMgArCaFYFArCaSiRnSiAlArTiTiTiTiTiTiTiRnPMgArPTiTiTiBSiRnSiAlArTiTiRnPMgArCaFYBPBPTiRnSiRnMgArSiThCaFArCaSiThFArPRnFArCaSiRnTiBSiThSiRnSiAlYCaFArPRnFArSiThCaFArCaCaSiThCaCaCaSiRnPRnCaFArFYPMgArCaPBCaPBSiRnFYPBCaFArCaSiAl
diff --git a/rb/day19.rb b/rb/day19.rb
new file mode 100755
index 0000000..d9e4133
--- /dev/null
+++ b/rb/day19.rb
@@ -0,0 +1,48 @@
+#!/usr/bin/env ruby
+
+def getreps(str, from, to)
+ re = Regexp.new(from)
+ md = str.match(re)
+
+ reps = []
+ until md.nil?
+ reps << str[0...md.begin(0)] + to + str[md.end(0)..-1]
+ md = str.match(re, md.end(0))
+ end
+
+ reps
+end
+
+*istrs, _, init = File.readlines("day19.input").map(&:strip)
+istrs.map! { |istr| istr.split(" => ") }
+
+results = Set.new
+istrs.each do |from, to|
+ results += getreps(init, from, to)
+end
+
+puts "Part 1: #{results.length}"
+
+
+istrs.map! { |from, to| [to, from] }
+
+space = [[init, 0]]
+mindis = init.length
+
+loop do
+ break if space.empty?
+ this, thisdis = space.pop
+
+ if this == ?e
+ mindis = thisdis if thisdis < mindis
+ next
+ end
+
+ istrs.each do |from, to|
+ getreps(this, from, to).each do |rep|
+ space.push([rep, thisdis + 1])
+ end
+ end
+end
+
+puts "Part 2: #{mindis}"
diff --git a/rb/day20.input b/rb/day20.input
new file mode 100644
index 0000000..510a390
--- /dev/null
+++ b/rb/day20.input
@@ -0,0 +1 @@
+29000000
diff --git a/rb/day20.rb b/rb/day20.rb
new file mode 100755
index 0000000..d71f7db
--- /dev/null
+++ b/rb/day20.rb
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+
+require "prime"
+
+def factors(m)
+ return [1] if 1 == m
+ primes, powers = Prime.prime_division(m).transpose
+ ranges = powers.map { |n| (0..n).to_a }
+ ranges[0]
+ .product( *ranges[1..-1] )
+ .map { |es| primes
+ .zip(es)
+ .map { |p, e| p ** e }
+ .reduce(:*)
+ }.sort
+end
+
+target = File.read("day20.input").strip.to_i
+
+i = 1
+loop do
+ break if factors(i).sum * 10 >= target
+ i += 1
+end
+puts i