From 11483b86ed5f70c9ead909d4fc4d3ffd682ce265 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 9 Dec 2018 22:54:11 -0800 Subject: [PATCH] Add day 10 and day 9 solutions. --- day10.cr | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ day9.cr | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 day10.cr create mode 100644 day9.cr diff --git a/day10.cr b/day10.cr new file mode 100644 index 0000000..74056b5 --- /dev/null +++ b/day10.cr @@ -0,0 +1,57 @@ +require "./common.cr" + +lines = File.read("day10_input").split "\n" +lines.pop + +points = [] of Tuple(Int64, Int64, Int64, Int64) + +REGEX = /position=<\s*(-?\d+),\s*(-?\d+)> velocity=<\s*(-?\d+), \s*(-?\d+)>/ +lines.map(&.match(REGEX)).each do |match| + match = match.not_nil! + points << { match[1].to_i64, match[2].to_i64, match[3].to_i64, match[4].to_i64 } +end + +def max_distance(points) + points.product(points).max_of do |left, right| + (left[0] - right[0]).abs + (left[1] - right[1]).abs + end +end + +def simulate(points, multiplier = 1) + points.size.times do |index| + old = points[index] + points[index] = { old[0] + multiplier * old[2], old[1] + multiplier * old[3], old[2], old[3] } + end +end + +def print_message(points) + max_x = points.max_of &.[0] + min_x = points.min_of &.[0] + max_y = points.max_of &.[1] + min_y = points.min_of &.[1] + + (max_y - min_y + 2).times do |yoff| + (max_x - min_x + 2).times do |xoff| + x = min_x + xoff - 1 + y = min_y + yoff - 1 + if points.any? { |point| (point[0] == x) && (point[1] == y) } + STDOUT << '▩' + else + STDOUT << '.' + end + end + puts + end +end + +d1 = max_distance(points) +simulate(points) +d2 = max_distance(points) +times = (d2 / (d2 - d1)).abs - 10 +simulate(points, times) + +20.times do |i| + simulate(points, multiplier = 1) + print_message(points) + puts "^ #{times + 1 + i + 1}" +end diff --git a/day9.cr b/day9.cr new file mode 100644 index 0000000..4e1488c --- /dev/null +++ b/day9.cr @@ -0,0 +1,36 @@ +require "./common.cr" + +lines = File.read("day9_input").split "\n" +lines.pop + +players = 463 +last_points = 400 + +marbles = [ 0 ] +scores = Array(Int32).new(players, 0) +player = 0 +pos = 0 + +(last_points).times do |i| + i = i + 1 + if (i % 23 == 0) + delete_at = (pos - 7 - marbles.size) % marbles.size + + puts "current: #{i}" + puts "popping: #{marbles[delete_at]}" + scores[player] += i + scores[player] += marbles[delete_at] + + pos = delete_at + marbles.delete_at delete_at + else + at = (pos + 2) % marbles.size + marbles.insert at, i + pos = marbles.index(i).not_nil! + end + + puts marbles + player = (player + 1) % players +end + +puts scores.max