Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
A fun and small challenge.
First read all locks, transpose their profile and count the #s (-1 for the full row).
Then do the same for the keys.
Lastly find all keys for all locks that do not sum to more than 5 with their teeth:
Code
val lockRegex = Regex("""#{5}(\r?\n[.#]{5}){6}""")
val keyRegex = Regex("""([.#]{5}\r?\n){6}#{5}""")
fun parseLocksAndKeys(inputFile: String): Pair<List<IntArray>, List<IntArray>> {
val input = readResource(inputFile)
val locks = lockRegex
.findAll(input)
.map {
it
.value
.lines()
.map { line -> line.toList() }
.transpose()
.map { line -> line.count { c -> c == '#' } - 1 }
.toIntArray()
}
.toList()
val keys = keyRegex
.findAll(input)
.map {
it
.value
.lines()
.map { line -> line.toList() }
.transpose()
.map { line -> line.count { c -> c == '#' } - 1 }
.toIntArray()
}
.toList()
return locks to keys
}
fun part1(inputFile: String): String {
val (locks, keys) = parseLocksAndKeys(inputFile)
val matches = locks.map { lock ->
keys.filter { key ->
for (i in lock.indices) {
// Make sure the length of the key and lock do not exceed 5
if (lock[i] + key[i] > 5) {
return@filter false
}
}
true
}
}
.flatten()
.count()
return matches.toString()
}
Thanks to Eric and the team at Advent of Code, to @[email protected] and @[email protected] for giving us somewhere to share and discuss our solutions, and to everyone here for the friendly and supportive community.
See you all next year!
import 'package:collection/collection.dart';
import 'package:more/more.dart';
part1(List<String> lines) {
var (w, h) = (lines.first.length, lines.indexOf(''));
var (falsey: keys, truthy: locks) = (lines..insert(0, ''))
.splitBefore((l) => l.isEmpty)
.map((g) => [
for (var x in 0.to(w)) [for (var y in 1.to(h + 1)) g[y][x]]
])
.partition((g) => g[0][0] == '#');
return keys
.map((l) => locks.count((k) =>
0.to(w).every((r) => (l[r] + k[r]).count((e) => e == '#') < 8)))
.sum;
}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Data.Either
import Data.Text hiding (all, head, zipWith)
import Data.Text qualified as T
import Data.Text.IO as TIO
type Pins = [Int]
toKeyLock :: [Text] -> Either Pins Pins
toKeyLock v = (if T.head (head v) == '#' then Left else Right) $ fmap (pred . count "#") v
solve keys locks = sum [1 | k <- keys, l <- locks, fit k l]
where
fit a b = all (<= 5) $ zipWith (+) a b
main = TIO.getContents >>= print . uncurry solve . partitionEithers . fmap (toKeyLock . transpose . T.lines) . splitOn "\n\n"
Spent 10 minutes debugging my solution until I reread and found out they wanted the number of keys that fit, not the ones that overlapped. Reading comprehension is not it tonight.
const [locks, keys] = require('fs').readFileSync(0, 'utf-8').split(/\r?\n\r?\n/g).filter(v => v.length > 0).map(s => s.split(/\r?\n/g).filter(v => v.length > 0)).reduce((acc, s) => {
const lock = s[0].split('').every(v => v === '#');
const schema = s.slice(1, -1);
let rotated = [];
for (let i = 0; i < schema[0].length; i += 1) {
for (let j = 0; j < schema.length; j += 1) {
if (!rotated[i]) rotated[i] = [];
rotated[i].push(schema[j][i]);
}
}
if (!lock) {
rotated = rotated.map(v => v.reverse());
}
const pinHeights = [];
for (const row of rotated) {
const height = row.indexOf('.');
pinHeights.push(height !== -1 ? height : 5);
}
if (lock) {
acc[0].push(pinHeights);
} else {
acc[1].push(pinHeights);
}
return acc;
}, [[],[]]);
let fits = 0;
for (const lock of locks) {
for (const key of keys) {
let overlapped = false;
for (let i = 0; i < lock.length; i += 1) {
if ((lock[i] + key[i]) > 5) {
overlapped = true;
}
}
if (!overlapped) {
fits += 1;
}
}
}
console.log('Part One', fits);