this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

part 1!

+148 -11
+37
2023/day07/deno.lock
··· 1 + { 2 + "version": "3", 3 + "remote": { 4 + "https://deno.land/std@0.208.0/assert/_constants.ts": "8a9da298c26750b28b326b297316cdde860bc237533b07e1337c021379e6b2a9", 5 + "https://deno.land/std@0.208.0/assert/_diff.ts": "58e1461cc61d8eb1eacbf2a010932bf6a05b79344b02ca38095f9b805795dc48", 6 + "https://deno.land/std@0.208.0/assert/_format.ts": "a69126e8a469009adf4cf2a50af889aca364c349797e63174884a52ff75cf4c7", 7 + "https://deno.land/std@0.208.0/assert/assert.ts": "9a97dad6d98c238938e7540736b826440ad8c1c1e54430ca4c4e623e585607ee", 8 + "https://deno.land/std@0.208.0/assert/assert_almost_equals.ts": "e15ca1f34d0d5e0afae63b3f5d975cbd18335a132e42b0c747d282f62ad2cd6c", 9 + "https://deno.land/std@0.208.0/assert/assert_array_includes.ts": "6856d7f2c3544bc6e62fb4646dfefa3d1df5ff14744d1bca19f0cbaf3b0d66c9", 10 + "https://deno.land/std@0.208.0/assert/assert_equals.ts": "d8ec8a22447fbaf2fc9d7c3ed2e66790fdb74beae3e482855d75782218d68227", 11 + "https://deno.land/std@0.208.0/assert/assert_exists.ts": "407cb6b9fb23a835cd8d5ad804e2e2edbbbf3870e322d53f79e1c7a512e2efd7", 12 + "https://deno.land/std@0.208.0/assert/assert_false.ts": "0ccbcaae910f52c857192ff16ea08bda40fdc79de80846c206bfc061e8c851c6", 13 + "https://deno.land/std@0.208.0/assert/assert_greater.ts": "ae2158a2d19313bf675bf7251d31c6dc52973edb12ac64ac8fc7064152af3e63", 14 + "https://deno.land/std@0.208.0/assert/assert_greater_or_equal.ts": "1439da5ebbe20855446cac50097ac78b9742abe8e9a43e7de1ce1426d556e89c", 15 + "https://deno.land/std@0.208.0/assert/assert_instance_of.ts": "3aedb3d8186e120812d2b3a5dea66a6e42bf8c57a8bd927645770bd21eea554c", 16 + "https://deno.land/std@0.208.0/assert/assert_is_error.ts": "c21113094a51a296ffaf036767d616a78a2ae5f9f7bbd464cd0197476498b94b", 17 + "https://deno.land/std@0.208.0/assert/assert_less.ts": "aec695db57db42ec3e2b62e97e1e93db0063f5a6ec133326cc290ff4b71b47e4", 18 + "https://deno.land/std@0.208.0/assert/assert_less_or_equal.ts": "5fa8b6a3ffa20fd0a05032fe7257bf985d207b85685fdbcd23651b70f928c848", 19 + "https://deno.land/std@0.208.0/assert/assert_match.ts": "c4083f80600bc190309903c95e397a7c9257ff8b5ae5c7ef91e834704e672e9b", 20 + "https://deno.land/std@0.208.0/assert/assert_not_equals.ts": "9f1acab95bd1f5fc9a1b17b8027d894509a745d91bac1718fdab51dc76831754", 21 + "https://deno.land/std@0.208.0/assert/assert_not_instance_of.ts": "0c14d3dfd9ab7a5276ed8ed0b18c703d79a3d106102077ec437bfe7ed912bd22", 22 + "https://deno.land/std@0.208.0/assert/assert_not_match.ts": "3796a5b0c57a1ce6c1c57883dd4286be13a26f715ea662318ab43a8491a13ab0", 23 + "https://deno.land/std@0.208.0/assert/assert_not_strict_equals.ts": "4cdef83df17488df555c8aac1f7f5ec2b84ad161b6d0645ccdbcc17654e80c99", 24 + "https://deno.land/std@0.208.0/assert/assert_object_match.ts": "d8fc2867cfd92eeacf9cea621e10336b666de1874a6767b5ec48988838370b54", 25 + "https://deno.land/std@0.208.0/assert/assert_rejects.ts": "45c59724de2701e3b1f67c391d6c71c392363635aad3f68a1b3408f9efca0057", 26 + "https://deno.land/std@0.208.0/assert/assert_strict_equals.ts": "b1f538a7ea5f8348aeca261d4f9ca603127c665e0f2bbfeb91fa272787c87265", 27 + "https://deno.land/std@0.208.0/assert/assert_string_includes.ts": "b821d39ebf5cb0200a348863c86d8c4c4b398e02012ce74ad15666fc4b631b0c", 28 + "https://deno.land/std@0.208.0/assert/assert_throws.ts": "63784e951475cb7bdfd59878cd25a0931e18f6dc32a6077c454b2cd94f4f4bcd", 29 + "https://deno.land/std@0.208.0/assert/assertion_error.ts": "4d0bde9b374dfbcbe8ac23f54f567b77024fb67dbb1906a852d67fe050d42f56", 30 + "https://deno.land/std@0.208.0/assert/equal.ts": "9f1a46d5993966d2596c44e5858eec821859b45f783a5ee2f7a695dfc12d8ece", 31 + "https://deno.land/std@0.208.0/assert/fail.ts": "c36353d7ae6e1f7933d45f8ea51e358c8c4b67d7e7502028598fe1fea062e278", 32 + "https://deno.land/std@0.208.0/assert/mod.ts": "37c49a26aae2b254bbe25723434dc28cd7532e444cf0b481a97c045d110ec085", 33 + "https://deno.land/std@0.208.0/assert/unimplemented.ts": "d56fbeecb1f108331a380f72e3e010a1f161baa6956fd0f7cf3e095ae1a4c75a", 34 + "https://deno.land/std@0.208.0/assert/unreachable.ts": "4600dc0baf7d9c15a7f7d234f00c23bca8f3eba8b140286aaca7aa998cf9a536", 35 + "https://deno.land/std@0.208.0/fmt/colors.ts": "34b3f77432925eb72cf0bfb351616949746768620b8e5ead66da532f93d10ba2" 36 + } 37 + }
+99 -5
2023/day07/part1.ts
··· 1 + export function answer(input: string): number { 2 + const handsAndBids = input.split("\n").map(parseHand); 3 + 4 + handsAndBids.sort(([handA], [handB]) => { 5 + const [typeA, typeB] = [handType(handA), handType(handB)]; 6 + if (typeA !== typeB) { 7 + return HAND_TYPES.indexOf(typeA) > HAND_TYPES.indexOf(typeB) ? 1 : -1; 8 + } else { 9 + for (let i = 0; i < handA.length; i++) { 10 + if (handA[i] !== handB[i]) { 11 + return CARDS.indexOf(handA[i]) > CARDS.indexOf(handB[i]) ? 1 : -1; 12 + } 13 + } 14 + } 15 + throw `hands are the same!: ${handA} ${handB}`; 16 + }); 17 + 18 + return handsAndBids.reduce( 19 + (sum, [_hand, bid], rank) => sum + bid * (rank + 1), 20 + 0, 21 + ); 22 + } 23 + 24 + const CARDS = "23456789TJQKA"; 25 + const HAND_TYPES = [ 26 + "high card", 27 + "1 pair", 28 + "2 pair", 29 + "3 of a kind", 30 + "full house", 31 + "4 of a kind", 32 + "5 of a kind", 33 + ]; 34 + 35 + function parseHand(l: string): [string, number] { 36 + const [hand, bidStr] = l.split(" "); 37 + return [hand, parseInt(bidStr)]; 38 + } 39 + 40 + const cardCounts = (hand: string[]) => ({ 41 + "2": hand.filter((c) => c === "2").length, 42 + "3": hand.filter((c) => c === "3").length, 43 + "4": hand.filter((c) => c === "4").length, 44 + "5": hand.filter((c) => c === "5").length, 45 + "6": hand.filter((c) => c === "6").length, 46 + "7": hand.filter((c) => c === "7").length, 47 + "8": hand.filter((c) => c === "8").length, 48 + "9": hand.filter((c) => c === "9").length, 49 + T: hand.filter((c) => c === "T").length, 50 + J: hand.filter((c) => c === "J").length, 51 + Q: hand.filter((c) => c === "Q").length, 52 + K: hand.filter((c) => c === "K").length, 53 + A: hand.filter((c) => c === "A").length, 54 + }); 55 + 56 + const isFiveOfAKind = (cards: string[]): boolean => new Set(cards).size === 1; 57 + 58 + const isFourOfAKind = (cards: string[]): boolean => { 59 + const uniqueCards = new Set(cards); 60 + return ( 61 + uniqueCards.size >= 2 && 62 + [...uniqueCards].some((c) => cards.filter((cc) => cc === c).length === 4) 63 + ); 64 + }; 65 + 66 + const isThreeOfAKind = (cards: string[]): boolean => { 67 + const uniqueCards = new Set(cards); 68 + return ( 69 + uniqueCards.size >= 2 && 70 + [...uniqueCards].some((c) => cards.filter((cc) => cc === c).length === 3) 71 + ); 72 + }; 73 + 74 + const isFullHouse = (cards: string[]): boolean => { 75 + const uniqueCards = new Set(cards); 76 + return ( 77 + uniqueCards.size === 2 && 78 + Object.values(cardCounts(cards)).filter((c) => c > 1).length === 2 79 + ); 80 + }; 81 + 82 + const pairs = (cards: string[]): number => 83 + Object.values(cardCounts(cards)).filter((c) => c === 2).length; 84 + const isTwoPair = (cards: string[]): boolean => pairs(cards) === 2; 85 + const isOnePair = (cards: string[]): boolean => pairs(cards) === 1; 86 + 87 + function handType(hand: string): string { 88 + const cards = hand 89 + .split("") 90 + .sort((a, b) => CARDS.indexOf(a) - CARDS.indexOf(b)); 91 + if (isFiveOfAKind(cards)) return "5 of a kind"; 92 + if (isFourOfAKind(cards)) return "4 of a kind"; 93 + if (isFullHouse(cards)) return "full house"; 94 + if (isThreeOfAKind(cards)) return "3 of a kind"; 95 + if (isTwoPair(cards)) return "2 pair"; 96 + if (isOnePair(cards)) return "1 pair"; 97 + return "high card"; 98 + } 99 + 1 100 if (import.meta.main) { 2 101 const input = ( 3 102 await Deno.readFile("input").then((bytes) => ··· 6 105 ).trim(); 7 106 console.log(answer(input)); 8 107 } 9 - 10 - export function answer(input: string): number { 11 - console.log(input); 12 - return 42; 13 - }
+12 -6
2023/day07/test.ts
··· 3 3 import * as p2 from "./part2.ts"; 4 4 5 5 Deno.test("part1", () => { 6 - const examples = ["abc", "def"].join("\n"); 7 - assertEquals(p1.answer(examples), 42); 6 + const examples = [ 7 + "32T3K 765", 8 + "T55J5 684", 9 + "KK677 28", 10 + "KTJJT 220", 11 + "QQQJA 483", 12 + ].join("\n"); 13 + assertEquals(p1.answer(examples), 6440); 8 14 }); 9 15 10 - Deno.test("part2", () => { 11 - const examples = ["abc", "def"].join("\n"); 12 - assertEquals(p2.answer(examples), 42); 13 - }); 16 + // Deno.test("part2", () => { 17 + // const examples = ["abc", "def"].join("\n"); 18 + // assertEquals(p2.answer(examples), 42); 19 + // });