Created
December 7, 2023 05:16
-
-
Save bluepichu/705f5f7d161403a530bf096252b69227 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Advent, f, fm, chr, ord } from "advent"; | |
import { Set, Map } from "immutable"; | |
const { compute, computeCheck } = await Advent({ day: 7 }); | |
compute(2, async (input) => { | |
let data = input.parse(f.nl(f.dis(" ", f.str(), f.int(), (l, r) => [l, r]))) as [string, number][]; | |
data = data.sort(([a], [b]) => compare(a, b)); | |
console.log(data); | |
let ans = 0; | |
for (let i = 0; i < data.length; i++) { | |
ans += (i + 1) * data[i][1]; | |
} | |
return ans; | |
}); | |
const cards = ["J", "2", "3", "4", "5", "6", "7", "8", "9", "T", "Q" , "K", "A"]; | |
function compare(a: string, b: string) { | |
let aLevel = level(a); | |
let bLevel = level(b); | |
if (aLevel !== bLevel) { | |
return aLevel - bLevel; | |
} | |
for (let i = 0; i < a.length; i++) { | |
let aInd = cards.indexOf(a[i]); | |
let bInd = cards.indexOf(b[i]); | |
if (aInd !== bInd) { | |
return aInd - bInd; | |
} | |
} | |
return 0; | |
} | |
function level(a: string) { | |
let counts = Map<string, number>(); | |
for (const c of a) { | |
counts = counts.set(c, (counts.get(c) ?? 0) + 1); | |
} | |
let jCount = counts.get("J") ?? 0; | |
counts = counts.delete("J"); | |
const keys = counts.keySeq().sort((a, b) => counts.get(b)! - counts.get(a)!).toArray(); | |
if (counts.get(keys[0], 0) + jCount === 5) { | |
return 6; | |
} else if (counts.get(keys[0], 0) + jCount === 4) { | |
return 5; | |
} else if (counts.get(keys[0], 0) + jCount=== 3 && counts.get(keys[1]) === 2) { | |
return 4; | |
} else if (counts.get(keys[0], 0) + jCount === 3) { | |
return 3; | |
} else if (counts.get(keys[0], 0) + jCount === 2 && counts.get(keys[1]) === 2) { | |
return 2; | |
} else if (counts.get(keys[0], 0) + jCount === 2) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment