From 7757fd2b498f7aa59a0756a38c7c553fe3902219 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 12 Dec 2020 22:09:28 -0800 Subject: [PATCH] Add day 13 solution. --- day13.cr | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day13.cr diff --git a/day13.cr b/day13.cr new file mode 100644 index 0000000..fbc29e5 --- /dev/null +++ b/day13.cr @@ -0,0 +1,34 @@ +require "advent" +INPU = input(2020, 13).lines +INPUT = {INPU[0].to_i32, INPU[1].split(",")} + +def part1(input) + early, busses = input + busses.reject! &.==("x") + busses = busses.map &.to_i32 + bbus = busses.min_by do |b| + (early / b).ceil * b + end + diff = bbus * (((early/bbus).ceil * bbus).to_i32 - early) +end + +def part2(input) + _, busses = input + busses = busses.map_with_index do |x, i| + x.to_i32?.try { |n| { n, i } } + end + busses = busses.compact + n = 0_i64 + iter = 1_i64 + busses.each do |m, i| + while (n + i) % m != 0 + n += iter + end + iter *= m + end + puts n + puts busses +end + +puts part1(INPUT.clone) +puts part2(INPUT.clone)