From 5f97f44af4bee6c06535dcbb2d6014279f7f2981 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 2 Dec 2022 23:48:23 -0800 Subject: [PATCH] Add solutions for day two and three --- day2.chpl | 33 +++++++++++++++++++++++++++++++++ day2.cr | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ day3.chpl | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ day3.cr | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 day2.chpl create mode 100644 day2.cr create mode 100644 day3.chpl create mode 100644 day3.cr diff --git a/day2.chpl b/day2.chpl new file mode 100644 index 0000000..e8510c6 --- /dev/null +++ b/day2.chpl @@ -0,0 +1,33 @@ +use Map; +use IO; + +var winsOne = new map(string, int); +winsOne["AX"] = 3 + 1; +winsOne["AY"] = 6 + 2; +winsOne["AZ"] = 0 + 3; +winsOne["BX"] = 0 + 1; +winsOne["BY"] = 3 + 2; +winsOne["BZ"] = 6 + 3; +winsOne["CX"] = 6 + 1; +winsOne["CY"] = 0 + 2; +winsOne["CZ"] = 3 + 3; + +var winsTwo = new map(string, int); +winsTwo["AX"] = 0 + 3; +winsTwo["AY"] = 3 + 1; +winsTwo["AZ"] = 6 + 2; +winsTwo["BX"] = 0 + 1; +winsTwo["BY"] = 3 + 2; +winsTwo["BZ"] = 6 + 3; +winsTwo["CX"] = 0 + 2; +winsTwo["CY"] = 3 + 3; +winsTwo["CZ"] = 6 + 1; + +iter scores(map) { + for line in stdin.lines() { + yield map[line.strip().replace(" ", "")]; + } +} + +config const part = 1; +writeln(+ reduce (scores(if part == 1 then winsOne else winsTwo))); diff --git a/day2.cr b/day2.cr new file mode 100644 index 0000000..76383b8 --- /dev/null +++ b/day2.cr @@ -0,0 +1,51 @@ +require "advent" + +INPUT = input(2022, 2).lines.map do |l| + a, b = l.split(" ") + {a, b} +end + +WINS1 = { + "AX" => 3 + 1, + "AY" => 6 + 2, + "AZ" => 0 + 3, + + "BX" => 0 + 1, + "BY" => 3 + 2, + "BZ" => 6 + 3, + + "CX" => 6 + 1, + "CY" => 0 + 2, + "CZ" => 3 + 3, +} + +WINS2 = { + "AX" => 0+3, + "AY" => 3+1, + "AZ" => 6+2, + + "BX" => 0+1, + "BY" => 3+2, + "BZ" => 6+3, + + "CX" => 0+2, + "CY" => 3+3, + "CZ" => 6+1, +} + +def part1(input) + input.sum do |x| + a,b = x + WINS1[a+b] + end +end + +def part2(input) + input.sum do |x| + a,b = x + WINS2[a+b] + end +end + +puts part1(INPUT) +puts part2(INPUT) diff --git a/day3.chpl b/day3.chpl new file mode 100644 index 0000000..b163148 --- /dev/null +++ b/day3.chpl @@ -0,0 +1,54 @@ +use Set; +use IO; + +const lowercase: [1..26] string = "abcdefghijklmnopqrstuvwxyz".these(); +const uppercase: [27..52] string = "abcdefghijklmnopqrstuvwxyz".toUpper().these(); + +proc letterScore(letter: string): int { + var (isLower, score1) = lowercase.find(letter); + if isLower then return score1; + var (isUpper, score2) = uppercase.find(letter); + if isUpper then return score2; + halt("Should not happen"); +} + +proc charSet(str: string): set(string) { + return new set(string, str); +} + +proc groupScore(group): int { + var inAll = new set(string, lowercase) | new set(string, uppercase); + for chars in charSet(group) { + inAll &= chars; + } + return + reduce letterScore(inAll.these()); +} + +iter inputLines() { + for line in stdin.lines() do yield line.strip(); +} + +iter inputLineHalves() { + for line in inputLines() { + yield line[..