From 3047c5925ea808b3b6dadfa23bcec7651efc5ec9 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 25 Dec 2022 16:37:28 +0000 Subject: [PATCH] Done day 25 --- advent-of-code22.cabal | 5 ++ advent25/Main.hs | 49 ++++++++++ data/advent25.txt | 135 ++++++++++++++++++++++++++++ data/advent25a.txt | 13 +++ problems/day25.html | 197 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 399 insertions(+) create mode 100644 advent25/Main.hs create mode 100644 data/advent25.txt create mode 100644 data/advent25a.txt create mode 100644 problems/day25.html diff --git a/advent-of-code22.cabal b/advent-of-code22.cabal index 532085c..3504e46 100644 --- a/advent-of-code22.cabal +++ b/advent-of-code22.cabal @@ -229,3 +229,8 @@ executable advent24 import: common-extensions, build-directives main-is: advent24/Main.hs build-depends: containers, pqueue, mtl, lens, linear, array + +executable advent25 + import: common-extensions, build-directives + main-is: advent25/Main.hs +-- build-depends: split diff --git a/advent25/Main.hs b/advent25/Main.hs new file mode 100644 index 0000000..39ba5c1 --- /dev/null +++ b/advent25/Main.hs @@ -0,0 +1,49 @@ +-- Writeup at https://work.njae.me.uk/2022/12/01/advent-of-code-2022-day-1/ + +import AoC +import Data.List + + +main :: IO () +main = + do dataFileName <- getDataFileName + numStrs <- readFile dataFileName + let fuels = fmap readSnafu $ lines numStrs + putStrLn $ showSnafu $ sum fuels + -- print $ part1 fuels + +readSnafu :: String -> Int +readSnafu cs = foldl' go 0 cs + where go acc c = acc * 5 + (snafuValue c) + +snafuValue :: Char -> Int +snafuValue '2' = 2 +snafuValue '1' = 1 +snafuValue '0' = 0 +snafuValue '-' = -1 +snafuValue '=' = -2 +snafuValue _ = error "Illegal digit in read" + +showSnafu :: Int -> String +showSnafu n = reverse $ packSnafu 0 $ toBase5R n + +toBase5R :: Int -> [Int] +toBase5R 0 = [] +toBase5R n = (r : (toBase5R k)) + where (k, r) = n `divMod` 5 + +packSnafu :: Int -> [Int] -> String +packSnafu 0 [] = "" +packSnafu carry [] = [snafuRep carry] +packSnafu carry (d:ds) + | d' <= 2 = ((snafuRep d') : (packSnafu 0 ds)) + | otherwise = ((snafuRep (d' - 5)) : (packSnafu 1 ds)) + where d' = d + carry + +snafuRep :: Int -> Char +snafuRep 2 = '2' +snafuRep 1 = '1' +snafuRep 0 = '0' +snafuRep -1 = '-' +snafuRep -2 = '=' +snafuRep _ = error "Illegal number in show" diff --git a/data/advent25.txt b/data/advent25.txt new file mode 100644 index 0000000..b5ab655 --- /dev/null +++ b/data/advent25.txt @@ -0,0 +1,135 @@ +1-- +1- +1--02 +1122-=2-==0-0=1-2 +2=1-==-1022=10 +1==-02211022=000 +101==210-21=2 +11010==0==221=0=02 +11=1101020=02-11 +1=== +10-2 +10=02=-=02 +1-1--112-202--0 +11-=-0-002=0112-1 +2--0-2=02=-22 +1001-2 +1=101=2=2102-201--1 +1-2==1100-- +2-01=--=1-11 +101-=0010-210== +1=-21-022121110 +1==21 +22==1 +2-0-=2=1 +2--0=10-00--2- +1--1=011-2 +1-100-2 +11-1 +112 +222200110-11110-2= +1==2-210-1122=-2022 +2==1-20-2-=-21 +21=1-=0=-1-=-=-12 +210---=-1===-===00 +1--21=01-022=2- +11-= +21-2 +2-22-0 +1==- +1-----01 +1-0-21- +211212-22-12--=2 +1=220=10=20200-0 +21-=11-==20=1 +102-2==2-==-0 +1=0=01- +22=1--0111 +1-----=1==00===0- +20==-1-121- +1=--2--21-202=21 +1-0122=--= +2==-2--===-=-1=-012 +2220=-00-10- +1==0=---10212-1- +1221=-022120 +100112 +2-=0=220-=01-1=12- +1=00 +2122121=2 +2-== +12-21===10220 +1=0 +22-1=--01-==0 +1-2-2202=1=02 +11-===21- +1-22=0102-=-222-1 +1021=0=21=0=11=2-2 +1===2222=0-0= +20=2-=0= +1=2==02-1-100200 +1=1211211---- +12=1=1---=20 +2--2=2=1100020200 +1=0-002-11-10---- +1=1=-1=-0 +1-21=1=02=21=20 +1-=0-002-0=--=0= +20-===-1= +220021012 +12===1001=--=2=1- +1=-02 +21 +2=200222122 +1-2=110111=012=1- +1=02= +101==1-=1=0001 +1=22--1-021 +120 +201-0211 +1-1012211--2=-1= +12=10202=20 +11-22=2=2=-0==1=00 +11=- +12-1--2222 +22 +1120--0== +10=-0=001- +1=00200200= +1-20 +1-0- +10112-202=-011 +11-2= +1-=10=1-0=-21==11200 +11 +12-0-=01--12 +1-=1121-=0 +120-21=-1=10121= +1102-22 +21= +1==--1==---1-== +12=0--=2210-1== +22210=1-=0 +1102-20-1 +1=1=2102 +11--=2==-0 +2=0=1= +21-0-10= +12-212-2=-==102-- +1==0100210-= +12210-0-1 +1=0-2-0= +10-1=211=1-2= +201-0-- +1--2=00=-1-211 +121=-22100=210-- +122-0-1- +2=211 +2=1-=2-2-0001=-1 +1102--11=0=1=10==2 +1==01121-2--0=12 +2=21-20=-0-0 +2-212=21-02=1-2-02 +2=1-0 +10-0-=20 +2=2=201 \ No newline at end of file diff --git a/data/advent25a.txt b/data/advent25a.txt new file mode 100644 index 0000000..237ef0c --- /dev/null +++ b/data/advent25a.txt @@ -0,0 +1,13 @@ +1=-0-2 +12111 +2=0= +21 +2=01 +111 +20012 +112 +1=-1= +1-12 +12 +1= +122 \ No newline at end of file diff --git a/problems/day25.html b/problems/day25.html new file mode 100644 index 0000000..71b6e08 --- /dev/null +++ b/problems/day25.html @@ -0,0 +1,197 @@ + + + + +Day 25 - Advent of Code 2022 + + + + + + + + +

Advent of Code

Neil Smith (AoC++) 50*

          2022

+ + + +
+

--- Day 25: Full of Hot Air ---

As the expedition finally reaches the extraction point, several large hot air balloons drift down to meet you. Crews quickly start unloading the equipment the balloons brought: many hot air balloon kits, some fuel tanks, and a fuel heating machine.

+

The fuel heating machine is a new addition to the process. When this mountain was a volcano, the ambient temperature was more reasonable; now, it's so cold that the fuel won't work at all without being warmed up first.

+

The Elves, seemingly in an attempt to make the new machine feel welcome, have already attached a pair of googly eyes and started calling it "Bob".

+

To heat the fuel, Bob needs to know the total amount of fuel that will be processed ahead of time so it can correctly calibrate heat output and flow rate. This amount is simply the sum of the fuel requirements of all of the hot air balloons, and those fuel requirements are even listed clearly on the side of each hot air balloon's burner.

+

You assume the Elves will have no trouble adding up some numbers and are about to go back to figuring out which balloon is yours when you get a tap on the shoulder. Apparently, the fuel requirements use numbers written in a format the Elves don't recognize; predictably, they'd like your help deciphering them.

+

You make a list of all of the fuel requirements (your puzzle input), but you don't recognize the number format either. For example:

+
1=-0-2
+12111
+2=0=
+21
+2=01
+111
+20012
+112
+1=-1=
+1-12
+12
+1=
+122
+
+

Fortunately, Bob is labeled with a support phone number. Not to be deterred, you call and ask for help.

+

"That's right, just supply the fuel amount to the-- oh, for more than one burner? No problem, you just need to add together our Special Numeral-Analogue Fuel Units. Patent pending! They're way better than normal numbers for--"

+

You mention that it's quite cold up here and ask if they can skip ahead.

+

"Okay, our Special Numeral-Analogue Fuel Units - SNAFU for short - are sort of like normal numbers. You know how starting on the right, normal numbers have a ones place, a tens place, a hundreds place, and so on, where the digit in each place tells you how many of that value you have?"

+

"SNAFU works the same way, except it uses powers of five instead of ten. Starting from the right, you have a ones place, a fives place, a twenty-fives place, a one-hundred-and-twenty-fives place, and so on. It's that easy!"

+

You ask why some of the digits look like - or = instead of "digits".

+

"You know, I never did ask the engineers why they did that. Instead of using digits four through zero, the digits are 2, 1, 0, minus (written -), and double-minus (written =). Minus is worth -1, and double-minus is worth -2."

+

"So, because ten (in normal numbers) is two fives and no ones, in SNAFU it is written 20. Since eight (in normal numbers) is two fives minus two ones, it is written 2=."

+

"You can do it the other direction, too. Say you have the SNAFU number 2=-01. That's 2 in the 625s place, = (double-minus) in the 125s place, - (minus) in the 25s place, 0 in the 5s place, and 1 in the 1s place. (2 times 625) plus (-2 times 125) plus (-1 times 25) plus (0 times 5) plus (1 times 1). That's 1250 plus -250 plus -25 plus 0 plus 1. 976!"

+

"I see here that you're connected via our premium uplink service, so I'll transmit our handy SNAFU brochure to you now. Did you need anything else?"

+

You ask if the fuel will even work in these temperatures.

+

"Wait, it's how cold? There's no way the fuel - or any fuel - would work in those conditions! There are only a few places in the-- where did you say you are again?"

+

Just then, you notice one of the Elves pour a few drops from a snowflake-shaped container into one of the fuel tanks, thank the support representative for their time, and disconnect the call.

+

The SNAFU brochure contains a few more examples of decimal ("normal") numbers and their SNAFU counterparts:

+
  Decimal          SNAFU
+        1              1
+        2              2
+        3             1=
+        4             1-
+        5             10
+        6             11
+        7             12
+        8             2=
+        9             2-
+       10             20
+       15            1=0
+       20            1-0
+     2022         1=11-2
+    12345        1-0---0
+314159265  1121-1110-1=0
+
+

Based on this process, the SNAFU numbers in the example above can be converted to decimal numbers as follows:

+
 SNAFU  Decimal
+1=-0-2     1747
+ 12111      906
+  2=0=      198
+    21       11
+  2=01      201
+   111       31
+ 20012     1257
+   112       32
+ 1=-1=      353
+  1-12      107
+    12        7
+    1=        3
+   122       37
+
+

In decimal, the sum of these numbers is 4890.

+

As you go to input this number on Bob's console, you discover that some buttons you expected are missing. Instead, you are met with buttons labeled =, -, 0, 1, and 2. Bob needs the input value expressed as a SNAFU number, not in decimal.

+

Reversing the process, you can determine that for the decimal number 4890, the SNAFU number you need to supply to Bob's console is 2=-1=0.

+

The Elves are starting to get cold. What SNAFU number do you supply to Bob's console?

+
+

Your puzzle answer was 20==1==12=0111=2--20.

--- Part Two ---

The hot air balloons quickly carry you to the North Pole. As soon as you land, most of the expedition is escorted directly to a small building attached to the reindeer stables.

+

The head smoothie chef has just finished warming up the industrial-grade smoothie blender as you arrive. It will take 50 stars to fill the blender. The expedition Elves turn their attention to you, and you begin emptying the fruit from your pack onto the table.

+

As you do, a very young Elf - one you recognize from the expedition team - approaches the table and holds up a single star fruit he found. The head smoothie chef places it in the blender.

+

Only 49 stars to go.

+
+

If you like, you can .

+

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

+

At this point, all that is left is for you to admire your Advent calendar.

+

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