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
Thanks to Eric and the team at Advent of Code, to @Ategon@programming.dev and @CameronDev@programming.dev 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;
}
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()
}
{-# 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"
ah well this year ends with a simple ~12.5 ms solve and not too much of a brain teaser. Well at least I got around to solving all of the challenges.
Code
from os.path import dirname,realpath,join
from collections.abc import Callable
def profiler(method) -> Callable[..., any]:
from time import perf_counter_ns
def wrapper_method(*args: any, **kwargs: any) -> any:
start_time = perf_counter_ns()
ret = method(*args, **kwargs)
stop_time = perf_counter_ns() - start_time
time_len = min(9, ((len(str(stop_time))-1)//3)*3)
time_conversion = {9: 'seconds', 6: 'milliseconds', 3: 'microseconds', 0: 'nanoseconds'}
print(f"Method {method.__name__} took : {stop_time / (10**time_len)} {time_conversion[time_len]}")
return ret
return wrapper_method
@profiler
def solver(locks_and_keys_str: str) -> int:
locks_list = []
keys_list = []
for schematic in locks_and_keys_str.split('\n\n'):
if schematic[0] == '#':
locks_list.append(tuple([column.index('.') for column in zip(*schematic.split())]))
else:
keys_list.append(tuple([column.index('#') for column in zip(*schematic.split())]))
count = 0
for lock_configuration in locks_list:
for key_configuration in keys_list:
for i,l in enumerate(lock_configuration):
if l>key_configuration[i]:
# break on the first configuration that is invalid
break
else:
# skipped when loop is broken
count += 1
return count
if __name__ == "__main__":
BASE_DIR = dirname(realpath(__file__))
with open(join(BASE_DIR, r'input'), 'r') as f:
input_data = f.read().replace('\r', '').strip()
result = solver(input_data)
print("Day 25 final solve:", result)
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);