~mcf/adventofcode

8f1cd3b1c300d362dedcc80661ca8b465f842970 — Michael Forney 1 year, 5 days ago e82ba20
More day 7 tweaks
2 files changed, 13 insertions(+), 12 deletions(-)

M 2023/7.lua
M 2023/Makefile
M 2023/7.lua => 2023/7.lua +12 -11
@@ 1,21 1,22 @@
local T, J, Q, K, A = string.byte('TJQKA', 1, 5)
local value = {
	['2']=2, ['3']=3, ['4']=4, ['5']=5, ['6']=6, ['7']=7, ['8']=8, ['9']=9,
	T=10, J=11, Q=12, K=13, A=14,
	[0x32]=2, [0x33]=3, [0x34]=4, [0x35]=5, [0x36]=6, [0x37]=7, [0x38]=8, [0x39]=9,
	[T]=10, [J]=11, [Q]=12, [K]=13, [A]=14,
}
function calcscore(hand, joker)
	local score, seen, uniq, most = 0, {}, 0, 0
	for card in hand:gmatch('.') do
	local score, count, uniq, most = 0, {}, 0, 0
	for i = 1, #hand do
		local card = string.byte(hand, i)
		local num = (count[card] or 0) + 1
		score = score << 4
		if joker and card == 'J' then
			joker = joker + 1
		else
			local num = (seen[card] or 0) + 1
			seen[card] = num
			score = score | value[card]
		if card ~= J or not joker then
			if num > most then most = num end
			if num == 1 then uniq = uniq + 1 end
			score = score | value[card]
		end
		count[card] = num
	end
	joker = joker and count[J]
	if joker then
		most = most + joker
		if uniq == 0 then uniq = 1 end


@@ 27,7 28,7 @@ for line in io.lines() do
	for hand, bid in line:gmatch('(%w+) (%d+)') do
		bid = tonumber(bid)
		table.insert(hands1, calcscore(hand) << 10 | bid)
		table.insert(hands2, calcscore(hand, 0) << 10 | bid)
		table.insert(hands2, calcscore(hand, true) << 10 | bid)
	end
end
table.sort(hands1)

M 2023/Makefile => 2023/Makefile +1 -1
@@ 4,7 4,7 @@
LUA?=lua

.PHONY: all
all: 1 2 3 4 5 6
all: 1 2 3 4 5 6 7

.DEFAULT:
	@printf 'day %s\t' $< && $(LUA) $<.lua < $<.txt