Last active
December 17, 2025 16:47
-
-
Save paulmr/049047700380cc46b418d57a220cd7db to your computer and use it in GitHub Desktop.
aoc 2025
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
| struct Dial | |
| getter pos = 50 | |
| getter zeros = 0 | |
| def click(dir) | |
| if dir == "L" | |
| @pos -= 1 | |
| @pos = 99 if @pos < 0 | |
| else | |
| @pos += 1 | |
| @pos = 0 if @pos > 99 | |
| end | |
| @zeros += 1 if @pos == 0 | |
| end | |
| end | |
| dial = Dial.new | |
| p1 = File.read_lines(ARGV[0]? || "ex.txt").reduce(0) do |acc, turn| | |
| turn[1..].to_i.times do | |
| dial.click(turn[0..0]) | |
| end | |
| dial.pos == 0 ? acc + 1 : acc | |
| end | |
| puts "Part 1: #{p1}" | |
| puts "Part 2: #{dial.zeros}" |
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 invalid(s) | |
| return false unless s.size % 2 == 0 | |
| mid = s.size // 2 | |
| s[0...mid] == s[mid..] | |
| end | |
| def invalid_2(s) | |
| chars = s.each_char.to_a | |
| (1...chars.size).each do |l| | |
| next unless chars.size % l == 0 | |
| groups = chars.in_groups_of(l) | |
| return true if groups[1..].all? { |g| g == groups[0] } | |
| end | |
| return false | |
| end | |
| input = File.read(ARGV[0]? || "ex.txt") | |
| .strip | |
| .split(",") | |
| .map do |r| | |
| limits = r.split("-").map(&.to_u64) | |
| limits[0]..limits[1] | |
| end | |
| p1 = input.flat_map do |r| | |
| r.select { |n| invalid(n.to_s) } | |
| end.sum | |
| p2 = input.flat_map do |r| | |
| r.select { |n| invalid_2(n.to_s) } | |
| end.sum | |
| puts "Part 1: #{p1}" | |
| puts "Part 2: #{p2}" |
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 search(bank, len, acc = [] of Char) | |
| return acc.join.to_u64 if len == 0 | |
| idx = (0..bank.size - len).max_by { |i| bank[i] } | |
| search(bank[idx + 1..], len - 1, acc + [bank[idx]]) | |
| end | |
| def solve(banks, len) | |
| banks.map { |bank| search(bank, len) }.sum | |
| end | |
| input = File.read_lines(ARGV[0]? || "ex.txt") | |
| puts solve(input, 2) | |
| puts solve(input, 12) |
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 count_ns(map, x, y) | |
| count = 0 | |
| (y - 1..y + 1).each do |y| | |
| next if y < 0 || y >= map.size | |
| (x - 1..x + 1).each do |x| | |
| # puts "#{x}, #{y} => #{map[y][x]}" | |
| next if x < 0 || x >= map[y].size | |
| count += 1 if map[y][x] | |
| end | |
| end | |
| count - 1 | |
| end | |
| def remove_rolls(map) | |
| next_map = map.clone | |
| count = 0 | |
| (0...map.size).each do |y| | |
| (0...map[y].size).each do |x| | |
| if map[y][x] && count_ns(map, x, y) < 4 | |
| count += 1 | |
| next_map[y][x] = false | |
| end | |
| end | |
| end | |
| { count, next_map } | |
| end | |
| input = File.read_lines(ARGV[0]? || "ex.txt").map do |line| | |
| line.each_char.map { |c| c == '@' }.to_a | |
| end | |
| p1, _ = remove_rolls(input) | |
| puts "Part 1: #{p1}" | |
| map = input | |
| p2 = 0 | |
| while true | |
| c, map = remove_rolls(map) | |
| p2 += c | |
| break if c == 0 | |
| end | |
| puts "Part 2: #{p2}" |
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
| alias R = Range(UInt64, UInt64) | |
| def insert(ranges, ra) | |
| res = [] of R | |
| ranges.each do |rb| | |
| if rb.includes?(ra.begin) || rb.includes?(ra.end) || ra.includes?(rb.begin) || ra.includes?(rb.end) | |
| ra = Math.min(ra.begin, rb.begin)..Math.max(ra.end, rb.end) | |
| else | |
| res << rb | |
| end | |
| end | |
| res << ra | |
| res | |
| end | |
| input = File.read_lines(ARGV[0]? || "ex.txt") | |
| sep = input.index(&.empty?).not_nil! | |
| ranges = input[0...sep].map do |r| | |
| limits = r.split("-").map(&.to_u64) | |
| limits[0]..limits[1] | |
| end.reduce([] of R) { |acc, r| insert(acc, r) } | |
| nums = input[sep+1..].map(&.to_u64) | |
| p nums.select { |n| ranges.any?(&.includes?(n)) }.size | |
| p ranges.map { |r| r.end - r.begin + 1}.sum |
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
| alias I = UInt64 | |
| def solve(prob) | |
| op = case prob[-1] | |
| when "+" | |
| ->(a : I, b : I) { a + b } | |
| when "*" | |
| ->(a : I, b : I) { a * b } | |
| end.not_nil! | |
| prob[0...-1].map(&.strip.to_u64).reduce(&op) | |
| end | |
| def solve2(prob) | |
| last = prob.last[0...-1] | |
| op = prob.last[-1..-1] | |
| prob = prob[0...-1] + [last, op] | |
| solve(prob) | |
| end | |
| input = File.read_lines(ARGV[0]? || "ex.txt") | |
| probs1 = input.map(&.strip.split /\s+/) | |
| p probs1.transpose.map { |p| solve(p) }.sum | |
| p input.map(&.each_char.to_a) | |
| .transpose | |
| .map(&.join) | |
| .reverse | |
| .chunks { |cell| cell.strip.empty? && Enumerable::Chunk::Drop } | |
| .map { |p| solve2(p.last) } | |
| .sum |
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
| record Beam, x : Int32, y : Int32 | |
| class Splitter | |
| getter x : Int32, y : Int32 | |
| property timelines = 0_u64 | |
| def initialize(@x, @y); end | |
| end | |
| def process_beam(spls, b) | |
| spl = spls.find { |s| s.x == b.x && s.y > b.y } | |
| if spl | |
| # puts "#{b} splits at #{spl.x},#{spl.y}" | |
| if spl.timelines == 0 | |
| spl.timelines = process_beam(spls, Beam.new(spl.x-1, spl.y)) + process_beam(spls, Beam.new(spl.x+1, spl.y)) | |
| end | |
| spl.timelines | |
| else | |
| 1_u64 | |
| end | |
| end | |
| spls = [] of Splitter | |
| beam : Beam? = nil | |
| File.read_lines(ARGV[0]? || "ex.txt").map(&.each_char.to_a).each_with_index do |l,y| | |
| l.each_with_index do |c,x| | |
| spls << Splitter.new(x, y) if c == '^' | |
| beam = Beam.new x: x, y: y if c == 'S' | |
| end | |
| end | |
| p2 = process_beam(spls, beam.not_nil!) # part 2 | |
| p1 = spls.select { |s| s.timelines > 0 }.size # part 1 | |
| puts "Part 1: #{p1}" | |
| puts "Part 2: #{p2}" |
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
| alias Circuit = Set(Junction) | |
| class Junction | |
| getter coords : Tuple(Int64, Int64, Int64) | |
| property circuit = Circuit.new | |
| def initialize(@coords) | |
| @circuit << self | |
| end | |
| def dist(other) | |
| coords.zip(other.coords).map { |a, b| (a - b) * (a - b) }.sum | |
| end | |
| end | |
| class State | |
| getter js : Array(Junction) | |
| getter cs : Array(Circuit) | |
| def initialize(input) | |
| @js = input.map do |l| | |
| c = l.split(",").map(&.to_i64) | |
| Junction.new({c[0], c[1], c[2]}) | |
| end | |
| @cs = @js.map(&.circuit) | |
| end | |
| def join(j1, j2) | |
| c1, c2 = {j1.circuit, j2.circuit} | |
| return if c1.same? c2 | |
| c1.concat(c2) # add circuit 2 junctions to circuit 1 | |
| c2.each { |j| j.circuit = c1 } # move them into the new circuit | |
| cs.delete(c2) # and delete that old circuit | |
| end | |
| def all_paths | |
| js.combinations(2).sort_by { |js| js[0].dist js[1] } | |
| end | |
| def join(count) | |
| all_paths[0...count].each do |p| | |
| join(p[0], p[1]) | |
| end | |
| end | |
| def join_all_until_unit | |
| paths = all_paths | |
| while true | |
| p = paths.shift | |
| join(p[0], p[1]) | |
| return p if cs.size == 1 | |
| end | |
| end | |
| end | |
| test_mode = ARGV.size == 0 | |
| input = File.read_lines(test_mode ? "ex.txt" : ARGV[0]) | |
| st = State.new(input) | |
| st.join(test_mode ? 10 : 1000) | |
| p st.cs.map(&.size).sort.reverse[0...3].product # part 1 | |
| st = State.new(input) | |
| res = st.join_all_until_unit.map(&.coords) | |
| p res[0][0] * res[1][0] # part 2 |
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 area(rect) | |
| w = (rect[0][:x] - rect[1][:x]).abs + 1 | |
| h = (rect[0][:y] - rect[1][:y]).abs + 1 | |
| w * h | |
| end | |
| def scan(rect, vlines, hlines) | |
| minx = rect.map(&.[:x]).min | |
| maxx = rect.map(&.[:x]).max | |
| miny = rect.map(&.[:y]).min | |
| maxy = rect.map(&.[:y]).max | |
| [miny+1, maxy-1].each do |scany| | |
| return true if vlines.any? { |line| | |
| line[0][:y] < scany && line[1][:y] > scany && | |
| minx < line[0][:x] && maxx > line[0][:x] | |
| } | |
| end | |
| [minx+1, maxx-1].each do |scanx| | |
| return true if hlines.any? { |line| | |
| line[0][:x] < scanx && line[1][:x] > scanx && | |
| miny < line[0][:y] && maxy > line[0][:y] | |
| } | |
| end | |
| false | |
| end | |
| reds = File.read_lines(ARGV[0]? || "ex.txt").map do |l| | |
| cs = l.split(",").map(&.to_i64) | |
| { x: cs[0], y: cs[1] } | |
| end | |
| lines = (reds + [reds.first]).each_cons(2).to_a | |
| hlines, vlines = lines.partition { |l| l[0][:y] == l[1][:y] } | |
| hlines.each(&.sort_by!(&.[:x])) | |
| vlines.each(&.sort_by!(&.[:y])) | |
| rects = reds.combinations(2) | |
| p rects.map { |r| area(r) }.max | |
| p rects.reject { |r| scan(r, vlines, hlines) } | |
| .map { |r| area(r) } | |
| .max |
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
| record Machine, lights : Array(Bool), joltage : Array(Int32), buttons : Array(Array(Int32)) | |
| def parse(line) | |
| lights_target = line.match!(/\[(.*)\]/)[1].each_char.map(&.==('#')).to_a | |
| joltage_target = line.match!(/\{(.*)\}/)[1].split(",").map(&.to_i) | |
| buttons = line.scan(/\((.*?)\)/).map { |m| m[1].split(",").map(&.to_i) } | |
| Machine.new(lights_target, joltage_target, buttons) | |
| end | |
| def press(joltage, button, count = 1) | |
| joltage.clone.tap { |res| button.map { |j| res[j] += count } } | |
| end | |
| class Solver | |
| getter buttons : Array(Array(Int32)) | |
| @best : Int32 | Nil | |
| def initialize(@buttons) | |
| @best = nil | |
| @seen = {} of Array(Int32) => Int32 | Nil | |
| end | |
| def solve_lights(targ) | |
| stack = [ { [] of Array(Int32), targ } ] | |
| depth = 0 | |
| seen = Set(Array(Bool)).new | |
| while !stack.empty? | |
| seen.concat(stack.map { |_, st| st }) | |
| if !(res = stack.find { |_, st| st.all? { |b| !b } }).nil? | |
| return { depth, res[0] } | |
| end | |
| stack = stack.flat_map do |path, st| | |
| buttons.map do |b| | |
| { path + [b], st.map_with_index { |v, idx| b.includes?(idx) ? !v : v } } | |
| end | |
| end.reject { |_, st| seen.includes?(st) } | |
| depth += 1 | |
| end | |
| raise "unsolvable!" | |
| end | |
| def find_evens(targ, max_depth = 10) | |
| # thanks to Pascal for teaching me about this :-) | |
| candidates = (0..buttons.size).flat_map { |n| buttons.combinations(n) } | |
| candidates.map { |bs| {bs.size, bs.reduce(targ) { |st, b| press(st, b, -1) } } } | |
| .reject { |_, st| st.any? { |j| j < 0 } } | |
| .select { |_, st| st.all? { |j| j % 2 == 0 } } | |
| end | |
| def reduce(targ) | |
| return 0 if targ.sum == 0 # solved | |
| evns = find_evens(targ, 5).not_nil!("no evens for #{targ}") | |
| sols = evns.compact_map do |depth, evn| | |
| shrunk = evn.map(&.// 2) | |
| reduce(shrunk).try { |res| res * 2 + depth } | |
| end | |
| sols.min? | |
| end | |
| end | |
| input = File.read_lines(ARGV[0]? || "ex.txt") | |
| .map { |line| parse(line) } | |
| p input.map { |machine| | |
| Solver.new(machine.buttons).solve_lights(machine.lights)[0] | |
| }.sum | |
| p input.map_with_index { |machine, idx| | |
| solver = Solver.new(machine.buttons) | |
| solver.reduce(machine.joltage).not_nil! | |
| }.sum |
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 Graph | |
| getter graph | |
| def initialize(@graph : Hash(String, Array(String))) | |
| @memo = Hash(NamedTuple(from: String, seen1: Bool, seen2: Bool), Int64).new | |
| end | |
| def traverse(from) | |
| return 1 if from == "out" | |
| graph[from].map { |node| traverse(node) }.sum | |
| end | |
| def traverse_via(from, to = "out", seen1 = false, seen2 = false) : Int64 | |
| key = { from: from, seen1: seen1, seen2: seen2 } | |
| if !(res = @memo[key]?).nil? | |
| return res | |
| end | |
| return seen1 && seen2 ? 1_i64 : 0_i64 if from == to | |
| res = graph[from].map { |node| traverse_via(node, to, seen1 || from == "fft", seen2 || from == "dac") }.sum | |
| @memo[key] = res | |
| res | |
| end | |
| end | |
| graph = Graph.new( | |
| File.read_lines(ARGV[0]? || "ex.txt").map do |line| | |
| m = line.match!(/(.*): (.*)/) | |
| connections = m[2].split(" ") | |
| { m[1], connections } | |
| end.to_h | |
| ) | |
| p graph.traverse("you") | |
| p graph.traverse_via("svr") |
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
| alias Image = Array(Array(Bool)) | |
| def parse(input : Iterator(String)) | |
| shapes = [] of Image | |
| regions = [] of {w: Int32, h: Int32, shapes: Array(Int32)} | |
| while !(ln = input.next).is_a? Iterator::Stop | |
| if m = ln.match(/^([0-9]+):/) | |
| shapes << input.take_while { |s| !s.empty? }.map do |line| | |
| line.each_char.to_a.map { |c| c == '#' } | |
| end.to_a | |
| else | |
| m = ln.match!(/^([0-9]+)x([0-9]+): (.*)$/) | |
| quants = m[3].split(" ").map(&.to_i) | |
| regions << {w: m[1].to_i, h: m[2].to_i, shapes: quants} | |
| end | |
| end | |
| {shapes, regions} | |
| end | |
| def place_shape(shape : Image, start_field : Image) | |
| res = [] of Image | |
| (0..(start_field.size - shape.size)).each do |y| | |
| (0..(start_field.first.size - shape.first.size)).each do |x| | |
| # puts "Attempting to place: #{x},#{y}" | |
| field = start_field.clone | |
| placed = (0...shape.size).all? do |shape_y| | |
| (0...shape.first.size).all? do |shape_x| | |
| if (shape_x + x) >= field.first.size || (shape[shape_y][shape_x] && field[shape_y + y][shape_x + x]) | |
| false # can't fit | |
| else | |
| # puts "setting #{y + shape_y},#{x + shape_x} to #{shape[shape_y][shape_x]}" | |
| field[y + shape_y][x + shape_x] ||= shape[shape_y][shape_x] | |
| true | |
| end | |
| end | |
| end | |
| res << field if placed | |
| end | |
| end | |
| return res | |
| end | |
| class Solver | |
| SHAPE_SIZE = 3 | |
| def initialize | |
| @memo = {} of {Array(Image), Image} => Bool | |
| end | |
| def solve(remaining_shapes, start_field) | |
| @memo.put_if_absent({remaining_shapes, start_field}) { _solve(remaining_shapes, start_field) } | |
| end | |
| def _solve(remaining_shapes, field) : Bool | |
| return true if remaining_shapes.size.zero? | |
| required_blocks = remaining_shapes.flat_map { |sh| sh.flat_map { |row| row.count { |cell| cell } } }.sum | |
| remaining_blocks = field.flat_map { |row| row.count { |cell| !cell } }.sum | |
| # puts "Required blocks: #{required_blocks} ; remaining: #{remaining_blocks}" | |
| return false if required_blocks > remaining_blocks | |
| h = field.size | |
| w = field.first.size | |
| shape = remaining_shapes[0] | |
| os = (0...4).map { |n| (0...n).reduce(shape) { |b| b.transpose.reverse } } | |
| os.each do |sh| | |
| placed = place_shape(sh, field) | |
| return true if placed.any? { |pl| solve(remaining_shapes[1..], pl) } | |
| end | |
| return false | |
| end | |
| end | |
| shapes, regions = File.open(ARGV[0]? || "ex.txt") do |fd| | |
| parse(fd.each_line) | |
| end | |
| puts "thinking very hard" | |
| solver = Solver.new | |
| total_regions = regions.size | |
| p regions.each_with_index.count { |r, idx| | |
| r_shapes = r[:shapes].each_with_index.to_a.flat_map { |q, idx| Array.new(q) { shapes[idx] } } | |
| field = Array.new(r[:h]) { Array.new(r[:w]) { false } } | |
| solver.solve(r_shapes, field).tap { |res| | |
| emoji = res ? "✅" : "❌" | |
| puts("Region %5d / %5d => #{emoji}" % [idx+1, total_regions]) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment