From 4df30f4f37244271ea01f3e64c1fba6c36c20e94 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sat, 3 Dec 2022 08:32:18 +0000 Subject: [PATCH] Done day 3 --- advent-of-code22.cabal | 5 + advent03/Main.hs | 61 +++++++++ data/advent03.txt | 300 +++++++++++++++++++++++++++++++++++++++++ data/advent03a.txt | 6 + problems/day03.html | 165 +++++++++++++++++++++++ 5 files changed, 537 insertions(+) create mode 100644 advent03/Main.hs create mode 100644 data/advent03.txt create mode 100644 data/advent03a.txt create mode 100644 problems/day03.html diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 4da1499..f310294 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -99,3 +99,8 @@ executable advent02 import: common-extensions, build-directives main-is: advent02/Main.hs build-depends: text, attoparsec + +executable advent03 + import: common-extensions, build-directives + main-is: advent03/Main.hs + build-depends: containers, split diff --git a/advent03/Main.hs b/advent03/Main.hs new file mode 100644 index 0000000..409ca33 --- /dev/null +++ b/advent03/Main.hs @@ -0,0 +1,61 @@ +-- Writeup at https://work.njae.me.uk/2022/12/03/advent-of-code-2022-day-3/ + +import System.Environment +import Data.Char +import qualified Data.Set as S +import Data.List +import Data.List.Split + +type Contents = S.Set Char +data Rucksack = Rucksack String String deriving (Show, Eq) + +main :: IO () +main = + do dataFileName <- getDataFileName + text <- readFile dataFileName + let rucksacks = fmap mkRucksack $ lines text + print $ part1 rucksacks + print $ part2 rucksacks + +getDataFileName :: IO String +getDataFileName = + do args <- getArgs + progName <- getProgName + let baseDataName = if null args + then progName + else head args + let dataFileName = "data/" ++ baseDataName ++ ".txt" + return dataFileName + +part1 :: [Rucksack] -> Int +part1 = sum . fmap (priority . commonItem) + +part2 :: [Rucksack] -> Int +part2 rucksacks = sum $ fmap priority badges + where groups = chunksOf 3 rucksacks + badges = fmap badgeOf groups + +merge :: Rucksack -> Contents +merge (Rucksack contents1 contents2) = + S.union (S.fromList contents1) (S.fromList contents2) + +badgeOf :: [Rucksack] -> Char +badgeOf rucksacks = S.findMin $ intersections (fmap merge rucksacks) + +intersections :: [Contents] -> Contents +intersections sets = foldl1' S.intersection sets + + +mkRucksack :: String -> Rucksack +mkRucksack contents = Rucksack (take n contents) (drop n contents) + where n = length contents `div` 2 + +commonItem :: Rucksack -> Char +commonItem (Rucksack contents1 contents2) = S.findMin (c1 `S.intersection` c2) + where c1 = S.fromList contents1 + c2 = S.fromList contents2 + +priority :: Char -> Int +priority item + | item >= 'a' = ord item - ord 'a' + 1 + | otherwise = ord item - ord 'A' + 27 diff --git a/data/advent03.txt b/data/advent03.txt new file mode 100644 index 0000000..3e41034 --- /dev/null +++ b/data/advent03.txt @@ -0,0 +1,300 @@ +wgqJtbJMqZVTwWPZZT +LHcTGHQhzrTzBsZFPHFZWFFs +RnLRClzGzRGLGLGCNRjTMjJfgmffSffMqNgp +WPLgsfLmLgqZvZgSRR +RbwHdbDdQFFFMvvMjbhqhZZS +lzTdldBDszfGcRsr +ZjnhJjMjnbdnbHdFLmmfFLmnCCWFFl +PpNwtRsNsZSsRwCfzQQBfQszCBsC +PpwcqqVZRtbggggjcgJJ +ntczBcVcgnHzgBHnVntcBBFhgsmmmssqWNWNWqLvNhsqTN +bSSGdSDZbGSGdDmLmGTvTGmLFFhm +PlFbDpJDPbPdPbZQZDZlSCDBfMVRwBzBtBQzfzRHVMVRtH +fDVrmmrvcmCcVpfcfGlswpPwsttMpjJMPPjjtP +RgSTdndFLbJqqPssWWjPWjPjHS +FqgzQnTqJRRQqLLhTCDhDCDmcmlvvlhcVm +nnqVtHbfVHZVmtlvmHtZtrFSFTRRFhRccTbrLsLSGr +cCNJQJPJQgjjMQdDrGGsRhTFGFRFSpMS +gzdCwWdjNPgzcJgjwdZtVlHHmvvmZlvffHnz +FFgsgwNwWvggQsMWDwvQQvQcccdcJZDtJGBtVGGGtcVlzt +rjfTrbjpjRSRTbTpzldjjHBtJGBdltJG +RbrPTfpCfmbpmnfRRCvMvQWNBwFFgMsgBL +zzL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WGF +lmlhBRlDhhhDRRhwDmBpHJmsNCHmCgNHJCJLsc +jvsLgmqLgHvbPPVbNjSCjC +pwTcRpRWLRMLJJFwBBGWcFWNVlDDCSTVttNPblZZCVVDlP +GcdhccpcpRpGRhGmfsHHzLQQHrmsnh +FMmSRgtMltMnVgnmNvlrsJrsZWjspvsZJp +QbdhqwqbNqdHbTdcbcpsrpvjfWfLJLfwJrWp +DQBBQqQGccdTPGqqBNtFGRSMRSFGtnVSnnmM +fPjGrfFrrprprdrbQPZwlcZwZmlJwH +qvNnvWnvWDvSvqNtWSLWStqbcJBQwQJwQZHLBZbcmJbblb +DMtvqSvvDtntCRfwzGCgdzzFjG +TfdZgtmfDgqgvlLFFsFHvcvZ +pphWQMVjQVVBWWjRlHlHnlcLDDhcnF +JQwwWVPBwMJpJwpWwGBWNzrDzSSzfgTPqTSTTtSPgt \ No newline at end of file diff --git a/data/advent03a.txt b/data/advent03a.txt new file mode 100644 index 0000000..9919ffa --- /dev/null +++ b/data/advent03a.txt @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw \ No newline at end of file diff --git a/problems/day03.html b/problems/day03.html new file mode 100644 index 0000000..986ad4f --- /dev/null +++ b/problems/day03.html @@ -0,0 +1,165 @@ + + + + +Day 3 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 6*

   $year=2022;

+ + + +
+

--- Day 3: Rucksack Reorganization ---

One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged.

+

Each rucksack has two large compartments. All items of a given type are meant to go into exactly one of the two compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack.

+

The Elves have made a list of all of the items currently in each rucksack (your puzzle input), but they need your help finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, a and A refer to different types of items).

+

The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment.

+

For example, suppose you have the following list of contents from six rucksacks:

+
vJrwpWtwJgWrhcsFMMfFFhFp
+jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
+PmmdzqPrVvPwwTWBwg
+wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
+ttgJtRGJQctTZtZT
+CrZsJsPPZsGzwwsLwLmpwMDw
+
+
    +
  • The first rucksack contains the items vJrwpWtwJgWrhcsFMMfFFhFp, which means its first compartment contains the items vJrwpWtwJgWr, while the second compartment contains the items hcsFMMfFFhFp. The only item type that appears in both compartments is lowercase p.
  • +
  • The second rucksack's compartments contain jqHRNqRjqzjGDLGL and rsFMfFZSrLrFZsSL. The only item type that appears in both compartments is uppercase L.
  • +
  • The third rucksack's compartments contain PmmdzqPrV and vPwwTWBwg; the only common item type is uppercase P.
  • +
  • The fourth rucksack's compartments only share item type v.
  • +
  • The fifth rucksack's compartments only share item type t.
  • +
  • The sixth rucksack's compartments only share item type s.
  • +
+

To help prioritize item rearrangement, every item type can be converted to a priority:

+
    +
  • Lowercase item types a through z have priorities 1 through 26.
  • +
  • Uppercase item types A through Z have priorities 27 through 52.
  • +
+

In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (p), 38 (L), 42 (P), 22 (v), 20 (t), and 19 (s); the sum of these is 157.

+

Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item types?

+
+

Your puzzle answer was 7727.

--- Part Two ---

As you finish identifying the misplaced items, the Elves come to you with another issue.

+

For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For efficiency, within each group of three Elves, the badge is the only item type carried by all three Elves. That is, if a group's badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of the Elves will be carrying any other item type.

+

The problem is that someone forgot to put this year's updated authenticity sticker on the badges. All of the badges need to be pulled out of the rucksacks so the new authenticity stickers can be attached.

+

Additionally, nobody wrote down which item type corresponds to each group's badges. The only way to tell which item type is the right one is by finding the one item type that is common between all three Elves in each group.

+

Every set of three lines in your list corresponds to a single group, but each group can have a different badge item type. So, in the above example, the first group's rucksacks are the first three lines:

+
vJrwpWtwJgWrhcsFMMfFFhFp
+jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
+PmmdzqPrVvPwwTWBwg
+
+

And the second group's rucksacks are the next three lines:

+
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
+ttgJtRGJQctTZtZT
+CrZsJsPPZsGzwwsLwLmpwMDw
+
+

In the first group, the only item type that appears in all three rucksacks is lowercase r; this must be their badges. In the second group, their badge item type must be Z.

+

Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (r) for the first group and 52 (Z) for the second group. The sum of these is 70.

+

Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those item types?

+
+

Your puzzle answer was 2609.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1