From ef8e420215359bbb0ea1398a9a70ed87c0a03115 Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Mon, 16 Dec 2024 21:13:27 -0800 Subject: [PATCH] [2024][ruby][17.1] --- 2024/ruby/day_16.rb | 32 +++++++++++------------ 2024/ruby/day_17.rb | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 2024/ruby/day_17.rb diff --git a/2024/ruby/day_16.rb b/2024/ruby/day_16.rb index 30b96f2..ae5f8cd 100644 --- a/2024/ruby/day_16.rb +++ b/2024/ruby/day_16.rb @@ -43,20 +43,18 @@ end pp scores.find {|(yx,_),_| yx == stop }[1] __END__ -################# -#...#...#...#..E# -#.#.#.#.#.#.#.#^# -#.#.#.#...#...#^# -#.#.#.#.###.#.#^# -#>>v#.#.#.....#^# -#^#v#.#.#.#####^# -#^#v..#.#.#>>>>^# -#^#v#####.#^###.# -#^#v#..>>>>^#...# -#^#v###^#####.### -#^#v#>>^#.....#.# -#^#v#^#####.###.# -#^#v#^........#.# -#^#v#^#########.# -#S#>>^..........# -################# +############### +#.......#....E# +#.#.###.#.###.# +#.....#.#...#.# +#.###.#####.#.# +#.#.#.......#.# +#.#.#####.###.# +#...........#.# +###.#.#####.#.# +#...#.....#.#.# +#.#.#.###.#.#.# +#.....#...#.#.# +#.###.#.#.#.#.# +#S..#.....#...# +############### diff --git a/2024/ruby/day_17.rb b/2024/ruby/day_17.rb new file mode 100644 index 0000000..4e61325 --- /dev/null +++ b/2024/ruby/day_17.rb @@ -0,0 +1,62 @@ +registers, program = DATA.read.split("\n\n") + +a, b, c = registers.scan(/\d+/).map(&:to_i) +program = program.scan(/\d+/).map(&:to_i) + +combo = ->(operand) { + case operand + when 0, 1, 2, 3 then operand + when 4 then a + when 5 then b + when 6 then c + else + fail + end +} + +ip = 0 +out = [] +while (0...program.size).cover?(ip) + opcode = program.fetch(ip) + operand = program.fetch(ip+1) + case opcode + when 0 + a = (a / 2**combo[operand]).to_i + ip += 2 + when 1 + b = b ^ operand + ip += 2 + when 2 + b = combo[operand] % 8 + ip += 2 + when 3 + if a.zero? + ip += 2 + else + ip = operand + end + when 4 + b = b ^ c + ip += 2 + when 5 + out << combo[operand] % 8 + ip += 2 + when 6 + b = (a / 2**combo[operand]).to_i + ip += 2 + when 7 + c = (a / 2**combo[operand]).to_i + ip += 2 + else + fail + end +end + +puts out.join(?,) + +__END__ +Register A: 729 +Register B: 0 +Register C: 0 + +Program: 0,1,5,4,3,0