From 3e2478468c10188bbb50fde4041f84431e5c77fe Mon Sep 17 00:00:00 2001 From: Andinus Date: Sat, 12 Dec 2020 20:03:38 +0530 Subject: Add day-12 solution --- 2020/day-12/day-12.raku | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 2020/day-12/day-12.raku (limited to '2020/day-12/day-12.raku') diff --git a/2020/day-12/day-12.raku b/2020/day-12/day-12.raku new file mode 100755 index 0000000..4e66caa --- /dev/null +++ b/2020/day-12/day-12.raku @@ -0,0 +1,83 @@ +#!/usr/bin/env raku + +unit sub MAIN ( + Int $part where * == 1|2 = 1 #= part to run (1 or 2) +); + +my @instructions = "input".IO.lines; + +# east/west, north/south. east/north are positive. +my Int @ship[2] = 0, 0; + +my token Instruction { (N|S|E|W|L|R|F) (\d+) }; +if $part == 1 { + my Str @directions[4] = ; + my Int $facing = 0; + + for @instructions -> $instruction { + if $instruction ~~ &Instruction -> $match { + given $match[0] { + when 'N' { @ship[1] += $match[1]; } + when 'S' { @ship[1] -= $match[1]; } + when 'E' { @ship[0] += $match[1]; } + when 'W' { @ship[0] -= $match[1]; } + when 'L' { + for 1..($match[1] / 90).Int { + if $facing == 0 { + $facing = 3; + } else { + $facing -= 1; + } + } + } + when 'R' { + for 1..($match[1] / 90).Int { + if $facing == 3 { + $facing = 0; + } else { + $facing += 1; + } + } + } + when 'F' { + given @directions[$facing] { + when 'N' { @ship[1] += $match[1]; } + when 'S' { @ship[1] -= $match[1]; } + when 'E' { @ship[0] += $match[1]; } + when 'W' { @ship[0] -= $match[1]; } + } + } + } + } + } +} elsif $part == 2 { + # east/west, north/south. east/north are positive. + my Int @waypoint[2] = 10, 1; + + for @instructions -> $instruction { + if $instruction ~~ &Instruction -> $match { + given $match[0] { + when 'N' { @waypoint[1] += $match[1]; } + when 'S' { @waypoint[1] -= $match[1]; } + when 'E' { @waypoint[0] += $match[1]; } + when 'W' { @waypoint[0] -= $match[1]; } + when 'L' { + for 1..($match[1] / 90).Int { + @waypoint = -@waypoint[1], @waypoint[0]; + } + } + when 'R' { + for 1..($match[1] / 90).Int { + @waypoint = @waypoint[1], -@waypoint[0]; + } + } + when 'F' { + @ship[0] += @waypoint[0] * $match[1]; + @ship[1] += @waypoint[1] * $match[1]; + } + } + } + } +} + +say "Part $part: ", [+] @ship.map(*.abs); -- cgit 1.4.1-2-gfad0