From 5f231facc6273c655162c0713ac4ca19978e0114 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 11 Dec 2020 21:19:04 -0800 Subject: [PATCH] Add day 12 solution. --- day12.cr | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 day12.cr diff --git a/day12.cr b/day12.cr new file mode 100644 index 0000000..20660f0 --- /dev/null +++ b/day12.cr @@ -0,0 +1,62 @@ +require "advent" +INPUT = input(2020, 12).lines.map do |l| + {l[0], l[1..].to_i32} +end + +def part1(input) + pos = {0, 0} + angle = 0 + input.each do |c, n| + case c + when 'N' + pos = pos.add({0, n}) + when 'S' + pos = pos.add({0, -n}) + when 'E' + pos = pos.add({n, 0}) + when 'W' + pos = pos.add({-n, 0}) + when 'L' + angle = (angle + n) % 360 + when 'R' + angle = (angle + (360-n)) % 360 + when 'F' + dists = [{1, 0}, {0, 1}, {-1, 0}, {0, -1}] + dx, dy = dists[angle//90] + pos = pos.add({dx * n, dy * n}) + end + end + pos.map(&.abs).sum +end + +def part2(input) + pos = {10, 1} + spos = {0, 0} + angle = 0 + input.each do |c, n| + case c + when 'N' + pos = pos.add({0, n}) + when 'S' + pos = pos.add({0, -n}) + when 'E' + pos = pos.add({n, 0}) + when 'W' + pos = pos.add({-n, 0}) + when 'L' + (n//90).times do + pos = {-pos[1], pos[0]} + end + when 'R' + (n//90).times do + pos = {pos[1], -pos[0]} + end + when 'F' + spos = spos.add({pos[0] * n, pos[1]*n}) + end + end + spos.map(&.abs).sum +end + +puts part1(INPUT.clone) +puts part2(INPUT.clone)