Add solutions for day two and three

This commit is contained in:
Danila Fedorin 2022-12-02 23:48:23 -08:00
parent 5c25497318
commit 5f97f44af4
4 changed files with 175 additions and 0 deletions

33
day2.chpl Normal file
View File

@ -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)));

51
day2.cr Normal file
View File

@ -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)

54
day3.chpl Normal file
View File

@ -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[..<line.size/2];
yield line[line.size/2..];
}
}
proc solveByReshaping(it, size) {
const array = it;
const shaped = reshape(array, {1..(array.size / size), 1..size});
var total = 0;
forall i in shaped.dim(0) with (+ reduce total) {
total reduce= groupScore(shaped[i,..]);
}
return total;
}
config const part = 1;
if part == 1 {
writeln(solveByReshaping(inputLineHalves(), 2));
} else if part == 2 {
writeln(solveByReshaping(inputLines(), 3));
}

37
day3.cr Normal file
View File

@ -0,0 +1,37 @@
require "advent"
def letter_score(letter : Char)
if letter.uppercase?
letter.ord - 'A'.ord + 1 + 26
else
letter.ord - 'a'.ord + 1
end
end
INPUT = input(2022, 3).lines
def part1(input)
input = input.map do |line|
first = line[0, line.size//2]
second = line[line.size//2, line.size//2]
{first.chars.to_set, second.chars.to_set}
end
input.sum do |a,b|
puts (a & b)
(a & b).to_a.map do |l|
puts letter_score(l)
letter_score(l)
end.sum
end
end
def part2(input)
input
.map(&.chars.to_set)
.in_groups_of(3, Set(Char).new)
.map(&.reduce { |l,r| l & r }.sum { |l| letter_score l })
.sum
end
puts part1(INPUT.clone)
puts part2(INPUT.clone)