From 09d1588b70673a0d368ad903efc6ff53b2d09ee7 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Sat, 14 Dec 2024 15:17:34 -0500 Subject: [PATCH] Day fourteen --- 14/input | 500 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 14/main.rb | 109 ++++++++++++ 2 files changed, 609 insertions(+) create mode 100644 14/input create mode 100755 14/main.rb diff --git a/14/input b/14/input new file mode 100644 index 0000000..1aeb857 --- /dev/null +++ b/14/input @@ -0,0 +1,500 @@ +p=81,85 v=-35,88 +p=89,76 v=-33,35 +p=79,21 v=92,-25 +p=64,67 v=-30,42 +p=18,21 v=31,52 +p=1,23 v=10,-13 +p=36,24 v=60,13 +p=89,34 v=-38,-51 +p=70,91 v=-82,96 +p=28,52 v=31,-5 +p=96,102 v=-39,86 +p=29,32 v=-71,52 +p=72,71 v=-5,-65 +p=93,26 v=-64,-55 +p=40,97 v=73,-50 +p=39,2 v=39,21 +p=45,49 v=-80,72 +p=82,29 v=-9,37 +p=94,70 v=-67,95 +p=31,77 v=5,84 +p=23,19 v=47,23 +p=85,0 v=91,97 +p=72,4 v=42,47 +p=89,89 v=-25,29 +p=50,51 v=-86,40 +p=1,93 v=35,-22 +p=39,70 v=-74,-34 +p=31,67 v=-98,61 +p=56,80 v=49,-95 +p=83,35 v=42,14 +p=74,54 v=25,-63 +p=1,24 v=-97,-94 +p=61,69 v=45,65 +p=61,50 v=-54,91 +p=96,26 v=-62,58 +p=83,25 v=-9,21 +p=89,46 v=15,-51 +p=56,59 v=11,62 +p=47,3 v=-24,-72 +p=3,68 v=-73,-73 +p=25,77 v=-97,-34 +p=39,69 v=-28,-95 +p=63,48 v=65,48 +p=80,26 v=92,14 +p=27,73 v=-73,53 +p=68,14 v=69,-59 +p=38,65 v=-24,98 +p=13,1 v=-12,-8 +p=55,79 v=97,-57 +p=88,19 v=41,82 +p=14,102 v=54,-10 +p=21,38 v=-95,-47 +p=31,100 v=-74,-83 +p=36,48 v=7,-75 +p=55,65 v=41,-31 +p=39,67 v=-74,61 +p=72,85 v=86,-37 +p=32,97 v=28,-83 +p=28,13 v=-68,-77 +p=25,37 v=-17,-93 +p=40,41 v=-66,4 +p=96,89 v=-73,-80 +p=5,36 v=-15,-32 +p=37,78 v=77,-53 +p=34,70 v=-95,-42 +p=79,48 v=42,-96 +p=75,75 v=18,-53 +p=29,25 v=-26,51 +p=32,85 v=16,-36 +p=75,3 v=17,36 +p=96,36 v=-90,83 +p=24,43 v=28,-74 +p=39,64 v=-81,12 +p=91,42 v=-38,-13 +p=32,35 v=-24,37 +p=57,1 v=-4,89 +p=5,75 v=84,-64 +p=56,41 v=49,98 +p=49,24 v=-26,2 +p=97,13 v=72,52 +p=72,19 v=-33,6 +p=61,76 v=37,-38 +p=80,81 v=-63,80 +p=58,44 v=47,-24 +p=85,17 v=91,75 +p=11,3 v=-74,-88 +p=47,96 v=65,-23 +p=66,67 v=-2,84 +p=15,91 v=-67,65 +p=37,33 v=-24,-66 +p=53,2 v=23,-33 +p=60,89 v=97,9 +p=27,42 v=-21,-54 +p=32,27 v=57,71 +p=84,45 v=89,18 +p=53,4 v=47,48 +p=8,52 v=-93,-39 +p=22,85 v=81,-26 +p=57,19 v=-78,82 +p=38,18 v=26,48 +p=18,64 v=56,28 +p=43,100 v=-50,-83 +p=40,6 v=91,76 +p=28,72 v=-23,-92 +p=71,91 v=35,45 +p=2,41 v=61,41 +p=80,64 v=16,99 +p=77,69 v=21,15 +p=16,94 v=83,35 +p=30,48 v=-73,38 +p=69,84 v=83,6 +p=98,17 v=-39,-13 +p=50,45 v=-27,10 +p=8,6 v=9,5 +p=88,50 v=87,-99 +p=21,100 v=-65,-47 +p=75,102 v=64,-63 +p=83,34 v=62,-81 +p=40,31 v=-32,47 +p=88,13 v=92,-93 +p=71,82 v=-61,10 +p=16,38 v=39,62 +p=23,16 v=47,59 +p=18,30 v=-48,22 +p=16,21 v=84,-63 +p=12,9 v=-87,-14 +p=84,57 v=90,49 +p=44,23 v=6,-14 +p=82,90 v=51,31 +p=48,12 v=24,74 +p=28,69 v=24,77 +p=68,61 v=-82,61 +p=91,98 v=-36,-22 +p=75,62 v=-59,72 +p=17,81 v=86,85 +p=75,55 v=68,-16 +p=21,23 v=-16,-17 +p=85,45 v=-59,-66 +p=24,74 v=-68,95 +p=97,21 v=-35,40 +p=54,65 v=7,8 +p=43,24 v=-27,-21 +p=23,100 v=33,59 +p=88,64 v=-33,-94 +p=22,63 v=4,19 +p=44,89 v=74,-30 +p=55,83 v=96,-34 +p=55,30 v=49,-13 +p=53,48 v=-25,33 +p=33,91 v=-5,97 +p=43,50 v=-72,57 +p=35,95 v=-81,-56 +p=65,91 v=-4,12 +p=67,37 v=-81,-89 +p=38,86 v=-96,23 +p=10,95 v=8,-45 +p=2,102 v=-33,10 +p=12,31 v=-68,-1 +p=78,82 v=-10,-30 +p=18,76 v=-94,-22 +p=10,99 v=12,-71 +p=43,76 v=77,-61 +p=23,32 v=-18,53 +p=26,86 v=-90,83 +p=28,54 v=40,-37 +p=61,7 v=-81,67 +p=66,23 v=-53,18 +p=1,52 v=-64,-96 +p=4,52 v=-95,18 +p=66,36 v=41,25 +p=68,0 v=-53,13 +p=10,55 v=-16,-12 +p=76,30 v=69,29 +p=45,5 v=-22,-79 +p=46,46 v=-25,-58 +p=73,1 v=-54,-88 +p=97,98 v=-14,-18 +p=36,98 v=56,74 +p=38,80 v=-53,-61 +p=93,80 v=38,12 +p=97,52 v=37,-87 +p=7,96 v=-91,-75 +p=85,0 v=-90,24 +p=23,22 v=-45,71 +p=82,72 v=42,-65 +p=71,71 v=68,46 +p=75,36 v=65,49 +p=2,63 v=86,-65 +p=78,28 v=-35,48 +p=27,87 v=76,-45 +p=5,65 v=89,23 +p=9,51 v=37,-47 +p=68,61 v=44,38 +p=2,60 v=57,-69 +p=11,52 v=58,45 +p=43,10 v=-99,-13 +p=31,67 v=-21,99 +p=6,91 v=-45,-79 +p=82,32 v=-42,-60 +p=79,29 v=17,71 +p=73,66 v=93,-35 +p=97,21 v=-13,90 +p=59,11 v=-84,-76 +p=94,40 v=13,56 +p=11,7 v=8,-71 +p=2,67 v=-41,10 +p=87,5 v=12,-56 +p=68,98 v=-60,-25 +p=49,81 v=48,50 +p=88,64 v=-87,91 +p=98,59 v=-13,-54 +p=61,94 v=94,8 +p=83,53 v=-60,-27 +p=90,60 v=-64,27 +p=73,37 v=72,-32 +p=93,5 v=9,-52 +p=75,43 v=-57,29 +p=92,3 v=88,28 +p=53,10 v=47,51 +p=12,34 v=6,-9 +p=67,92 v=-53,32 +p=96,89 v=84,-56 +p=66,54 v=41,79 +p=75,31 v=94,45 +p=92,33 v=88,-47 +p=40,91 v=76,81 +p=91,34 v=-61,28 +p=4,85 v=-66,-42 +p=69,69 v=15,15 +p=43,10 v=45,69 +p=27,9 v=-21,-71 +p=83,30 v=65,-78 +p=27,56 v=-25,-1 +p=20,21 v=8,7 +p=0,64 v=80,-58 +p=70,57 v=94,-77 +p=42,73 v=80,-97 +p=37,99 v=-56,-95 +p=61,87 v=66,58 +p=33,33 v=-96,21 +p=17,26 v=-15,-2 +p=62,87 v=46,-30 +p=22,6 v=-49,-67 +p=85,78 v=-86,-95 +p=13,78 v=-24,-92 +p=99,58 v=-88,-71 +p=8,74 v=54,-32 +p=3,3 v=35,97 +p=2,63 v=10,45 +p=32,68 v=-47,-84 +p=23,97 v=-21,70 +p=61,94 v=-26,66 +p=41,22 v=3,22 +p=73,5 v=6,52 +p=55,97 v=23,66 +p=52,41 v=-54,18 +p=72,94 v=-11,97 +p=79,45 v=66,68 +p=95,98 v=-29,75 +p=75,63 v=17,-8 +p=85,21 v=61,-58 +p=49,49 v=78,98 +p=53,67 v=19,-11 +p=15,13 v=31,55 +p=51,91 v=46,12 +p=58,46 v=39,79 +p=45,79 v=-51,84 +p=68,63 v=44,23 +p=71,96 v=-31,66 +p=65,60 v=-2,22 +p=81,92 v=-59,16 +p=75,35 v=-7,52 +p=97,31 v=-70,-40 +p=43,63 v=-72,-34 +p=4,34 v=53,53 +p=45,98 v=-22,24 +p=32,74 v=26,23 +p=24,99 v=29,-37 +p=28,4 v=29,97 +p=20,101 v=57,-18 +p=82,23 v=-96,-43 +p=85,10 v=8,-89 +p=98,78 v=88,-91 +p=77,2 v=41,-40 +p=50,88 v=-28,54 +p=77,17 v=-4,-58 +p=31,1 v=3,85 +p=97,52 v=-35,80 +p=80,67 v=-36,-69 +p=61,44 v=-28,76 +p=66,82 v=-2,-95 +p=18,88 v=83,-7 +p=46,6 v=-55,74 +p=99,51 v=-64,-77 +p=33,25 v=50,97 +p=71,29 v=17,71 +p=99,47 v=-96,-20 +p=45,57 v=1,68 +p=98,81 v=10,27 +p=70,0 v=-33,85 +p=2,32 v=-70,25 +p=71,93 v=43,47 +p=86,63 v=-11,57 +p=33,11 v=-21,32 +p=33,70 v=28,65 +p=11,44 v=65,-60 +p=57,22 v=46,-29 +p=72,40 v=92,-28 +p=16,58 v=3,-77 +p=63,58 v=24,91 +p=52,37 v=-6,87 +p=39,61 v=90,-50 +p=88,79 v=-81,17 +p=19,5 v=-88,-13 +p=31,81 v=53,96 +p=27,63 v=73,-48 +p=82,56 v=-89,60 +p=44,13 v=-42,68 +p=35,73 v=36,-79 +p=1,92 v=-38,-45 +p=51,31 v=84,30 +p=48,54 v=-77,-46 +p=45,82 v=-80,96 +p=50,38 v=-36,3 +p=52,87 v=-70,15 +p=99,26 v=12,36 +p=11,93 v=-35,24 +p=40,89 v=-3,85 +p=24,69 v=31,-4 +p=53,10 v=-28,-67 +p=20,10 v=-40,93 +p=87,53 v=96,-5 +p=65,85 v=-85,-53 +p=87,52 v=-28,-74 +p=56,31 v=-27,-93 +p=86,9 v=-87,51 +p=80,31 v=-83,48 +p=7,101 v=81,-18 +p=78,97 v=92,-41 +p=81,15 v=65,21 +p=87,99 v=18,-7 +p=94,82 v=38,65 +p=98,25 v=89,-6 +p=80,73 v=94,53 +p=1,29 v=-26,12 +p=56,60 v=-90,-34 +p=51,41 v=-77,22 +p=43,21 v=29,-93 +p=30,41 v=-50,-55 +p=87,26 v=41,-62 +p=99,99 v=35,-18 +p=95,53 v=37,-31 +p=78,86 v=15,96 +p=65,25 v=-81,10 +p=18,33 v=55,-32 +p=51,58 v=97,-57 +p=24,17 v=59,70 +p=48,69 v=-28,-84 +p=50,100 v=-34,-99 +p=29,65 v=-74,99 +p=26,74 v=-99,-3 +p=17,97 v=81,-45 +p=96,66 v=-60,34 +p=49,14 v=7,-16 +p=0,78 v=-15,-57 +p=87,73 v=-37,42 +p=45,69 v=25,-38 +p=44,42 v=-24,22 +p=95,27 v=-95,-52 +p=5,59 v=-38,51 +p=13,66 v=32,84 +p=95,47 v=-39,-31 +p=87,5 v=-55,-17 +p=83,80 v=-60,31 +p=29,80 v=-22,-61 +p=49,59 v=99,-92 +p=53,18 v=-53,48 +p=48,75 v=-85,52 +p=80,99 v=-34,-91 +p=87,64 v=39,-92 +p=91,90 v=44,83 +p=74,89 v=83,45 +p=75,85 v=64,-15 +p=48,82 v=-25,69 +p=29,79 v=-29,17 +p=79,50 v=-81,-73 +p=9,8 v=-68,74 +p=78,95 v=-84,1 +p=33,2 v=56,63 +p=35,49 v=66,-82 +p=47,64 v=-65,79 +p=43,5 v=-49,-45 +p=55,55 v=-77,-16 +p=14,57 v=76,57 +p=67,96 v=19,24 +p=14,62 v=-50,-86 +p=21,67 v=31,-65 +p=62,16 v=96,86 +p=48,41 v=-9,-73 +p=49,89 v=-23,-87 +p=82,40 v=-35,-70 +p=85,52 v=11,-39 +p=73,69 v=-59,-88 +p=25,102 v=30,-48 +p=47,32 v=-57,44 +p=89,53 v=-57,-21 +p=43,74 v=-86,-28 +p=35,72 v=77,-38 +p=71,29 v=22,-9 +p=86,50 v=-84,19 +p=97,25 v=-28,83 +p=56,33 v=73,10 +p=6,6 v=-89,-68 +p=17,63 v=-62,12 +p=93,23 v=-14,2 +p=40,21 v=27,-59 +p=78,50 v=-9,-16 +p=67,20 v=4,94 +p=27,58 v=-55,56 +p=79,0 v=93,28 +p=30,8 v=54,59 +p=92,49 v=71,-13 +p=89,17 v=-62,-17 +p=44,72 v=-77,46 +p=46,42 v=-27,83 +p=64,11 v=-28,-52 +p=63,53 v=-8,-61 +p=73,100 v=-83,51 +p=55,27 v=22,-5 +p=46,65 v=46,92 +p=41,53 v=51,-16 +p=100,90 v=-4,55 +p=40,39 v=-98,-9 +p=59,49 v=-54,53 +p=65,59 v=-80,-15 +p=24,20 v=79,26 +p=8,10 v=80,-25 +p=99,84 v=88,-87 +p=36,83 v=-23,-30 +p=64,75 v=-55,-61 +p=32,42 v=-99,-5 +p=74,57 v=41,-1 +p=57,76 v=65,-75 +p=51,67 v=-78,-4 +p=89,74 v=-40,-78 +p=37,41 v=28,-20 +p=66,26 v=-57,-74 +p=57,49 v=22,-92 +p=29,6 v=29,-83 +p=41,88 v=51,96 +p=53,24 v=72,58 +p=4,73 v=64,57 +p=12,69 v=26,-46 +p=35,93 v=42,60 +p=41,49 v=26,45 +p=59,79 v=-91,-70 +p=23,87 v=-45,39 +p=55,32 v=46,90 +p=19,44 v=6,72 +p=17,76 v=80,-27 +p=62,97 v=-59,24 +p=30,26 v=93,-66 +p=62,72 v=95,-61 +p=54,55 v=71,-16 +p=85,84 v=-90,78 +p=74,6 v=69,89 +p=19,45 v=98,-29 +p=42,84 v=10,-37 +p=88,84 v=61,-98 +p=7,17 v=10,70 +p=23,71 v=93,88 +p=43,0 v=29,40 +p=11,34 v=84,-36 +p=46,76 v=59,-61 +p=76,46 v=93,-24 +p=8,93 v=59,-68 +p=4,2 v=60,17 +p=84,83 v=-9,8 +p=25,44 v=82,98 +p=14,75 v=49,84 +p=91,97 v=60,-41 +p=27,14 v=53,-51 +p=14,7 v=38,62 +p=6,24 v=-92,40 +p=36,74 v=53,88 +p=97,30 v=62,3 +p=53,86 v=92,-73 +p=99,14 v=85,-29 +p=81,1 v=-24,60 +p=67,96 v=-16,10 +p=42,75 v=-83,-87 +p=5,73 v=29,40 +p=78,36 v=-87,-54 +p=69,88 v=35,52 +p=27,38 v=-96,60 +p=21,0 v=56,82 +p=67,48 v=71,-16 +p=97,8 v=-41,-75 +p=96,64 v=-96,-93 +p=52,101 v=-79,9 diff --git a/14/main.rb b/14/main.rb new file mode 100755 index 0000000..8a7b445 --- /dev/null +++ b/14/main.rb @@ -0,0 +1,109 @@ +#!/usr/bin/env ruby + +require "debug" +require "ostruct" + +MOVES = 100 + +input = (if ARGV.first.nil? + WIDTH = 11 + HEIGHT = 7 + + DATA +else + WIDTH = 101 + HEIGHT = 103 + + ARGF +end) + +def print_grid(grid) + grid.each_with_index do |row, y| + row.each_with_index do |val, x| + if val.size > 0 + print grid[y][x].size + elsif x == WIDTH / 2 || y == HEIGHT / 2 + print " " + else + print " " + end + end + puts + end + puts + puts +end + +def move(grid, ticks = 1) + new_grid = Array.new(HEIGHT) { Array.new(WIDTH) { Array.new } } + grid.each_with_index do |row, y| + row.each_with_index do |val, x| + movements = val.map do |robot| + new_x = (x + robot.first * ticks) % WIDTH + new_y = (y + robot.last * ticks) % HEIGHT + + new_grid[new_y][new_x] << robot + end + end + end + + new_grid +end + +def safety_factor(grid) + [ + [[0, 0], [HEIGHT / 2 - 1, WIDTH / 2 - 1]], + [[0, WIDTH / 2 + 1], [HEIGHT / 2 - 1, WIDTH - 1]], + [[HEIGHT / 2 + 1, 0], [HEIGHT - 1, WIDTH / 2 - 1]], + [[HEIGHT / 2 + 1, WIDTH / 2 + 1], [HEIGHT - 1, WIDTH - 1]], + ].map do |(top_left, bottom_right)| + (top_left.first..bottom_right.first).map do |y| + (top_left.last..bottom_right.last).map do |x| + grid[y][x].size + end.sum + end.sum + end.inject(:*) +end + +def count_top(grid) + (0...HEIGHT / 2).map do |y| + grid[y].select { !_1.empty? }.size + end.sum +end + +grid = Array.new(HEIGHT) { Array.new(WIDTH) { Array.new } } +input = input.readlines(chomp: true) + .map do |line| + x, y, vx, vy = line.scan(/(-?\d+)/).flatten.map(&:to_i) + grid[y][x] << [vx, vy] + end + +new_grid = move(grid.clone.map(&:clone), MOVES) +part_1 = safety_factor(new_grid) + +new_grid = grid.dup.map(&:dup) +candidates = (WIDTH * HEIGHT).times.map.with_index do |moves| + new_grid = move(new_grid) + + [moves + 1, safety_factor(new_grid)] +end.sort_by(&:last) + +part_2 = candidates.first.first + +print_grid(move(grid, candidates.first.first)) +p part_1 +p part_2 + +__END__ +p=0,4 v=3,-3 +p=6,3 v=-1,-3 +p=10,3 v=-1,2 +p=2,0 v=2,-1 +p=0,0 v=1,3 +p=3,0 v=-2,-2 +p=7,6 v=-1,-3 +p=3,0 v=-1,-2 +p=9,3 v=2,3 +p=7,3 v=-1,2 +p=2,4 v=2,-3 +p=9,5 v=-3,-3