Add solutions for day one

This commit is contained in:
Danila Fedorin 2022-11-30 22:46:47 -08:00
commit 1dcecf5dd4
2 changed files with 120 additions and 0 deletions

74
day1.chpl Normal file
View File

@ -0,0 +1,74 @@
use IO;
use List;
iter linesWithEnding() {
for line in stdin.lines() do yield line;
yield "";
}
iter elves() {
var current = 0;
for line in linesWithEnding() {
const trimmedLine = line.strip();
if trimmedLine == "" {
yield current;
current = 0;
} else {
current += trimmedLine : int;
}
}
}
class MaxThree : ReduceScanOp {
type eltType;
var value: 3*eltType;
proc identity {
var val: value.type;
return val;
}
proc accumulate(x: eltType) { accumulateOntoState(value, x); }
proc accumulateOntoState(ref state: 3*eltType, x: eltType) { accumulateOntoState(state, (0, 0, x)); }
proc accumulate(x: 3*eltType) { accumulateOntoState(value, x); }
proc accumulateOntoState(ref state: 3*eltType, x: 3*eltType) {
var result: state.type;
var ptr1, ptr2: int = 3-1;
for param idx in (0..<3 by -1) {
if x[ptr1] > state[ptr2] {
result[idx] = x[ptr1];
ptr1 -= 1;
} else {
result[idx] = state[ptr2];
ptr2 -= 1;
}
}
state = result;
}
proc combine(other: MaxThree(eltType)) {
accumulate(other.value);
}
proc clone() return new unmanaged MaxThree(eltType=eltType);
proc generate() return value;
}
config const part = 1;
config const parallel = false;
if part == 1 {
writeln(max reduce elves());
} else if part == 2 {
if parallel {
writeln(+ reduce (MaxThree reduce elves()));
} else {
// Parallel
var max3 = (0,0,0);
// Need to read all the numbers into memory to make sure we can distribute
var elfList = new list(elves());
// To make a reduction parallel, we use a forall loop with a reduce intent
forall elf in elfList with (MaxThree(int) reduce max3) {
max3 reduce= elf;
}
writeln(+ reduce max3);
}
}

46
day1.cr Normal file
View File

@ -0,0 +1,46 @@
require "advent"
INPUT = input(2022, 1).lines#.lines.map(&.to_i32)
def part1(input)
list = [] of Array(String)
current = [] of String
input.each do |line|
if line.empty?
list << current
current = [] of String
else
current << line
end
end
if !current.empty?
list << current
end
list.max_of do |list|
list.map(&.to_i32).sum
end
end
def part2(input)
list = [] of Array(String)
current = [] of String
input.each do |line|
if line.empty?
list << current
current = [] of String
else
current << line
end
end
if !current.empty?
list << current
end
data = list.map(&.map(&.to_i32).sum)
data.sort!
data[-1] + data[-2] + data[-3]
end
puts part1(INPUT.clone)
puts part2(INPUT.clone)