Compare commits

...

3 Commits

Author SHA1 Message Date
Steph
c54ea32ddc Day 8! Took me longer than I'd like to admit 2022-12-08 22:09:59 +01:00
Steph
2284a4e9e4 Day 7 done! YES! Today actually was quite the challenge 2022-12-08 22:09:58 +01:00
Steph
9c3befc0c8 Day 6! 2022-12-08 22:09:56 +01:00
13 changed files with 1326 additions and 15 deletions

View File

@ -1,22 +1,12 @@
cabal-version: 2.4
name: aoc2022
version: 0.1.0.0
-- A short (one-line) description of the package.
-- synopsis:
-- A longer description of the package.
-- description:
-- A URL where users can report bugs.
-- bug-reports:
author: Steph
maintainer: noreply+steph@code.steph.tools
executable aoc2022
main-is: Main.hs
build-depends: base ^>=4.16.3.0, split ^>=0.2.3.5, regex-tdfa ^>=1.3.2, containers ^>=0.6.6
build-depends: base ^>=4.16.2.0, split ^>=0.2.3.5, regex-tdfa ^>=1.3.2, containers ^>=0.6.6
hs-source-dirs: app
default-language: Haskell2010
other-modules: Day1.Main Day2.Main Day3.Main Day4.Main Day5.Main
other-modules: Day1.Main Day2.Main Day3.Main Day4.Main Day5.Main Day6.Main Day7.Main, Day8.Main

View File

@ -1,4 +1,4 @@
module Day4.Main where
module Day4.Main (main) where
import System.IO
import Data.List.Split

View File

@ -1,4 +1,4 @@
module Day5.Main where
module Day5.Main (main) where
import System.IO
import Data.List

35
2022/app/Day6/Main.hs Normal file
View File

@ -0,0 +1,35 @@
module Day6.Main (main) where
import System.IO
import Data.List
main :: IO ()
main = do
putStrLn "Day 6"
handle <- openFile "app/Day6/input" ReadMode
contents <- hGetContents handle
let r1 = part1 contents
putStrLn $ "part 1: " ++ show r1
let r2 = part2 contents
putStrLn $ "part 2: " ++ show r2
distinct xs = length (nub xs) == length xs
part1 :: String -> Int
part1 contents = go contents 0
where
go [] _ = 0
go (a:b:c:d:es) n | distinct [a,b,c,d] = n + 4
go (a:es) n = go es (n + 1)
part2 :: String -> Int
part2 contents = go contents 0
where
go [] _ = 0
go (e:es) n = if distinct (e : take 13 es)
then n + 14
else go es (n + 1)

1
2022/app/Day6/input Normal file
View File

@ -0,0 +1 @@
dvgdvvbpbtbhbdhbhmmmcctmcmccggtrgghnhmnnqffpcprrqssnhsnhnshhsrsqqhchcdcfcqqcncrrzpppmzpzzhjzzzvrvnnpbpzzswzwswnngjgjgcjcfcllhffjbfjfhhppvnppfmfcmmcnnmnfnzfnzffphpnnnsvswvvnwnfntftjftfvvztzqzhzddttjpjbpphhlnnwgnwggdmmczclczzqddlcdldrlrccfflwwgqwwrjwrwzrrsdrrfssddcmmvrvlvhvfhfzhzbhbzzdmdvvsppwswmwdwjjzmzhhvghgthghvvtcthcttgsttpqpbbhthppzznntpnttshtshthhwrrgbbjzbjblblzbllcblcblbvvqfqqnjqqhfhftftwfftvvmddgzzmdzmdmtttttqmqmnqmqpmqpqqjlldpllfvlffjdfjjvlvjjjjbsjbjnnhmnhnrrwtrrfvvtppwmmpnnjbnjjcncjcttrcrjjlqjljccpzccvqvzzscctgccsmmnznhzhnnjggmjgmgjgtgtlgglhhrlhhrvhvrvtvvlvvwmmrjjzqqbzqqtgtzgttmgggwpgwgbwggwwprwwvswszznczncnccjnccjdjdjssqmmgcmcjmmhwhswhwdwrddtccmzmjmsmrmrddhrhhqbbspbsppqvqmvqmmclcddzjzvvslsffdhhqgqrqjrqjqjrrmpmrppcjpjffwhfwwppqvvzjzqqtftgfghgpglgnglldlvvwzwmmfzzhzmzhhswhhnvhvphpmphphrrwwfccmgmqgmmzgmgngznnzbzttjwttgngzzdndffwhfwhhlmllwqlwwgfgfpfnnldnldndbdcdddmbdmbbdpbpwbpptrrpggtrtrnttsmmcrmmlhltlgttblttbhttjzzvnnsbsqbqwbbtwtmtbbcjcljjslscswwglgqlgqgmmcpctcrttprpjjfvjfjqqljlvjljnnfhhfzhzmhmghhjnhjjbppmqqwlwswwgbbzszspzsppcscvvnhndhdwdmdrdssqrqdqsddnbnpplqppjbjdbdqdcccffsnntzntnmnlnbbrzzzvgvvqttbbzpbzpzdpdvppjmjpjbbdrbbgwgffzpzzjddmldlwwjdwwscwcfccnznmmfttvvmpmvpmvmcvmmjmwjwbbwqwjjzqjzjbbjqbjqqqthhsssmrsshvvprvvwccjlcctwwmgwgpwgpgcpptrppjppltlhthctttfptppwgpwprpbbqzbzznssfcsffbnnndffqmmqwmmpvvpvdppjqqttfpttjccbnbddvtdvddlsstjtccphchsswbbrqqgtqgqpqjqnjjvccwscwssjwjttqvtvmtmjjghghccpnpsplslmmfjfjtffgtgrtrftfhttpsttzzmhmllwqwhhnffdpffwbwnnbrrfbrrnjrjlljqqnjqnqqwlqqbbbtvvwgvgbgddpwdwvdwvdvvwzvwwtzwttppnttqccjtjrttqbbprpsrpssfjsffmdfmfbmbggjhggcjgcgvvjqqbhbllhrlhlclzzplpqqdtddrhhfssdvsvrrscsjjnwjjfpjfffcbfcfmmlpmphhhlpltptcpttppfssppgwppjgjnnblbhbtbrbgbpplnlvlsvlvwvwpptmtsswzswsnscsfcsfsfggzhghvvgbbfddgbbrmrtrqtthvhzhzthtbtjjcljjlcllzglzzdszdzjzjfzzzblbslbljjdnndvvnwnqnwqqscscmczcwwvrrlclttbtdtvddbrddvllnppvpmvmbbvfvttfcttggwgffrfwwwpwcpchppcrrgprgprgrjgjdgdgvddzndzdbzzswsvvtgtccrrnggbwggpjpnnsjjwfjjqfqvffrrhdrrsfrfjjfzfdzzqfqwbztszjqtttfdqvzmznzjlsjnwdthtwdtfslgdmgfpwsqcsqdhnsnsmghttfvlzqgspzdtlstdmthzftwmnqrznldpmwqbtthggjwcgjjmbpqgrnwspggjvrlcmtvpchmqhlwwtswqgpdjpbznqnssqhdjzgbjnfmgssrvnmmcvvhgmcvqbfdhgrhnqqzdmttmdzwgtprzqhplwnhhmlrvcbwpllqprtltdvqrwhvwzvlqsvfqsfjwmrnzlqpdgfpmtfdczqdnfjjbjmrdnffcmtwlzcmvnwmlpmqhvggdhptnzlvzwzwjbcszsnzgpwncfgvzfgbzwclvrbmllzpltzwjrftmppsfwhvvvhvqjtstnnczgtdbmpjjsscbdwplftgcgmtrnrnzplzhghrqgdtjwntwfstjwqjjrlhtwhnfqwfqgsjptjfpsrbnvvlgsltnvtfvscttwvrfzblzmfmnfrlrnzrrzhclggtntpjbbcphdvrfhnrtzvdmwbwgbftgzwlcqztghdhdmzwlmjbgptfnnzbmwsnzlzcpprqzmbbdsplmhpgmzthqcsfjcnfbfvsdsqzfvfcnpgqsvpgwsdbgjmsglrwmfjfpddczwvgdppfmrtszbtfdwbmlmzhqvvwmvlzvjfpffjnhwwhssfjnbzlqwqvjbjbhfntmhgswntdpbzlwwfbdbhrfhzfjsjbtlrqhlnrpfbwtpmrfvbhlmmsgtvcmrqmdpwvhqfqpgmfgnfrbvprhprtnpzjcnltndfsvjgndwblhwphtpsmnczgbtpwdvjsrctjbvbfslvslzlwbtstqvgcrqmfphwztpjqdmvcjpjqmjbdndfpzwsfwplchsmqwwbggptjdtztszmpfwgfwnqpdwfcpgrrhmfglsctjllflfltbcfvcpfcnqbwrvzmcmjpwptgsrlbrdchngwsdstfmcbrqvdsvvbnppdmnfwcgvpjjzqwcpvqfncvqlsfnjzprvhpgqscshqwsttdrsmqjfwlhcwlvnzvgvclqfjdgctvsrbwzflcldmrwlfhbgdtstqsqlblndnpgqlfbjzslcpcwvdwdffshhrzvhqwdsdmwtmtvcnrhmstvrnscppmbpmjbfjhljmsjnbjlhjhmnmcvvfgbdrblwbzrcctrjwjjwjtgnfjhhqbsmdjvdrdjtjbscfrsljnvqjlgjwqrvfmdttsvqjwdbswdtcfqsrpbvzrbsdqlqfjlrgcwzbqtqrpsrfcmbzcvjngcsmvqlbnghllcqcztbtvdrfcmpgfdprghsmbjvzbdnrdqnjdzslclgdsqglvpvcjpzqfwztlssljtmcdfcqdnqzwcttvpqfdpvzlhjfvvsgphgqrmzppvnjznqmdzfnfztjppstjfwddftcpcjnssznqbrvlvrzfhbvsjrwghttwlwfrptsvsrwfnvjtthwrppbngbgqvbsdgcrjcwjjljcwptrvgmbjpjtdbmhmzcfhzbsbrmzhdsrjbbmnwbsntpffdrrlgcrcgbcfwvlpmrzvsmvpjthtdjdvcspdsdvshlrwzcqnjmcnrgzbqzhfzbmtrvzzmjwbnjggtrtgcsnrmzbtjzgdmffdntspdhgnvgrmpbtnsspcqhsrvppjbrmdbggjbftnnbrgdsmdscqthdzflldfnplqccthpwccsfsnstttwztqnmnfshntqngmcndbsbftmgnhhwjvhchdfqzzgpdnfgvnjzjzfdzvsvtdqqcftrvmdcszcwpfrbcsmlqqfprrjgncwcvcngmrnwntcvzzlnwrhrznnldslhqdscbgsrqnvnmdtqvlttwqljmvbpbfldtbgzhvwzghnhwrwdqphhhgjpnmtlcmvfbdffnsvcswtmffzsrvczbntfpdsmwbqphvvcflpwgsrmjhrljlvzdgrcwpfphmvtwqwhjmrvmjzjlzlbflhzrdrzcdwhblpqwjljbvprddtvnccmchgctncwbpnmlqppfmhwchvjvpmblqhccfhlprdrczdfhmnsqhddbqlppgsnvhhfrwhqhfdpvsfcvzbqhgswtmnpmzrwsvnmztnqwdrhllssmgtzbztsprpsj

110
2022/app/Day7/Main.hs Normal file
View File

@ -0,0 +1,110 @@
module Day7.Main (main) where
import System.IO
import Data.List
import Data.List.Split
import Data.Char
import System.IO.Unsafe
import Data.Maybe
main :: IO ()
main = do
putStrLn "Day 7"
handle <- openFile "app/Day7/input" ReadMode
contents <- hGetContents handle
let r1 = part1 contents
putStrLn $ "part 1: " ++ show r1
let r2 = part2 contents
putStrLn $ "part 2: " ++ show r2
type Name = String
type Size = Int
data FileSystem = Node Name [FileSystem]
| File Name Size
deriving (Show)
instance Read FileSystem where
readsPrec _ ('d':'i':'r':' ':xs) = [(Node xs [], "")]
readsPrec _ file = [(File name size, "")]
where
parts = splitOn " " file
name = parts !! 1
size = read $ parts !! 0
showFileSystemWithIndent :: Int -> FileSystem -> String
showFileSystemWithIndent n (File name size) = replicate (n*2) ' ' ++ "- " ++ name ++ " (size: " ++ show size ++ ")\n"
showFileSystemWithIndent n (Node name files) = replicate (n*2) ' ' ++ "- " ++ name ++ "/\n" ++ concatMap (showFileSystemWithIndent (n+1)) files
data Command = Cd Name
| Ls [FileSystem]
instance Show Command where
show (Cd name) = "$ cd " ++ show name ++ "\n"
show (Ls files) = "$ ls " ++ show files ++ "\n"
rstrip :: String -> String
rstrip = reverse . dropWhile isSpace . reverse
instance Read Command where
readsPrec _ ('c':'d':' ':name) = [(Cd name, "")]
readsPrec _ ('l':'s':files) = [(Ls items, "")]
where
lines = drop 1 $ splitOn "\n" files
items = map (read :: String -> FileSystem) lines
parseData :: String -> [Command]
parseData contents = map (read :: String -> Command) $ map rstrip $ drop 1 $ splitOn "$ " contents
getName :: FileSystem -> Name
getName (File n _) = n
getName (Node n _) = n
update :: Int -> a -> [a] -> [a]
update n item ls = a ++ (item:b)
where (a, (_:b)) = splitAt n ls
addToFs :: FileSystem -> [Name] -> FileSystem -> FileSystem
addToFs (Node name files) [] item = Node name (item:files)
addToFs (Node name files) (dir:path) item = Node name files'
where
index = fromJust $ findIndex ((== dir) . getName) files
file' = addToFs (files !! index) path item
files' = update index file' files
executeCommand :: [Command] -> [Name] -> FileSystem -> FileSystem
executeCommand [] pwd fs = fs
executeCommand ((Cd "/"):cs) _ fs = executeCommand cs [] fs
executeCommand ((Cd ".."):cs) (_:pwd) fs = executeCommand cs pwd fs
executeCommand ((Cd ".."):cs) [] fs = error "eyyy cd .. from root!"
executeCommand ((Cd path):cs) pwd fs = executeCommand cs (path : pwd) fs
executeCommand ((Ls files):cs) pwd fs = executeCommand cs pwd fs'
where fs' = foldr (\i f -> addToFs f (reverse pwd) i) fs files
commandsToFs :: [Command] -> FileSystem
commandsToFs commands = executeCommand commands [] (Node "root" [])
dirSize :: FileSystem -> Int
dirSize (Node _ files) = sum $ map dirSize files
dirSize (File _ size) = size
bigDirs :: FileSystem -> [Int]
bigDirs (Node _ files) = concatMap bigDirs files ++ [size]
where size = dirSize (Node "" files)
bigDirs (File _ _) = []
part1 :: String -> Int
part1 contents = sum $ filter (<= 100000) $ bigDirs results
where
results = commandsToFs $ parseData contents
part2 :: String -> Int
part2 contents = minimum candidates
where
results = commandsToFs $ parseData contents
fsSize = dirSize results
unused = 70000000 - fsSize
needed = 30000000 - unused
candidates = filter (>= needed) $ bigDirs results

23
2022/app/Day7/example Normal file
View File

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

983
2022/app/Day7/input Normal file
View File

@ -0,0 +1,983 @@
$ cd /
$ ls
dir gqcclj
dir lmtpm
dir nhqwt
dir qcq
dir vwqwlqrt
$ cd gqcclj
$ ls
62425 dqp.gjm
174181 hrtw.qsd
273712 pflp.mdw
169404 zlthnlhf.mtn
180878 zprprf
$ cd ..
$ cd lmtpm
$ ls
dir clffsvcw
163587 cvcl.jqh
dir dcqnblb
dir dtpwln
dir fvt
dir hrcrw
dir jdqzmqn
236754 nrdmlj
205959 pflp.mdw
dir qcq
dir rsn
129926 vdgcqdn.sqd
dir zprprf
$ cd clffsvcw
$ ls
6997 dcqnblb.wbh
145711 dqp
159225 pflp.mdw
$ cd ..
$ cd dcqnblb
$ ls
dir dcqnblb
dir gfn
dir lpswsp
dir lvt
dir zprprf
$ cd dcqnblb
$ ls
2020 grpdmd.ggz
dir zpswzfvg
$ cd zpswzfvg
$ ls
206998 zprprf.gnw
$ cd ..
$ cd ..
$ cd gfn
$ ls
277530 rhbvtblc.mvw
$ cd ..
$ cd lpswsp
$ ls
173180 dcqnblb
$ cd ..
$ cd lvt
$ ls
dir hjllwsvl
dir ptbt
$ cd hjllwsvl
$ ls
dir wqnc
$ cd wqnc
$ ls
64695 grpdmd.ggz
$ cd ..
$ cd ..
$ cd ptbt
$ ls
150880 vvbt.gtp
$ cd ..
$ cd ..
$ cd zprprf
$ ls
dir ldzslndn
dir qftt
$ cd ldzslndn
$ ls
dir bwqqsbhg
129454 vbn
$ cd bwqqsbhg
$ ls
108701 zprprf.gss
$ cd ..
$ cd ..
$ cd qftt
$ ls
64268 cvcl.jqh
$ cd ..
$ cd ..
$ cd ..
$ cd dtpwln
$ ls
196215 cvcl.jqh
dir dpwg
dir ldzslndn
dir znnsqqh
$ cd dpwg
$ ls
192388 gmh
47754 grgzh.qdl
99449 hqsh
dir pbmf
50061 pflp.mdw
192902 qcq.pgg
dir rmpvj
dir scgc
$ cd pbmf
$ ls
210083 wpfnwbl.mgf
$ cd ..
$ cd rmpvj
$ ls
125738 nmlnbvrd
226214 zprprf.jnp
114257 zprprf.srs
$ cd ..
$ cd scgc
$ ls
182115 rrc.rcc
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
201992 qcrm.cpd
$ cd ..
$ cd znnsqqh
$ ls
85635 cvcl.jqh
$ cd ..
$ cd ..
$ cd fvt
$ ls
dir dcqnblb
dir gnc
75864 vfn
$ cd dcqnblb
$ ls
dir dcqnblb
dir lbnflwsh
$ cd dcqnblb
$ ls
269901 cvcl.jqh
$ cd ..
$ cd lbnflwsh
$ ls
33336 grpdmd.ggz
42861 phg.wmc
$ cd ..
$ cd ..
$ cd gnc
$ ls
dir jhjbjsp
dir jjppr
$ cd jhjbjsp
$ ls
96177 ldzslndn
$ cd ..
$ cd jjppr
$ ls
181016 dqp
$ cd ..
$ cd ..
$ cd ..
$ cd hrcrw
$ ls
261376 dtjfpppr.dww
54658 vsrgvw.pfn
$ cd ..
$ cd jdqzmqn
$ ls
52342 dcpndc.vlg
171946 gggpchh.tbb
dir ldzslndn
11156 nbfrfvv.gzw
$ cd ldzslndn
$ ls
107873 cvcl.jqh
216034 gfdjrbz
68844 pqllfrrh.jcf
$ cd ..
$ cd ..
$ cd qcq
$ ls
152886 ldzslndn.ltn
105125 vwplh.vbf
$ cd ..
$ cd rsn
$ ls
15385 hqcmjdgv.jjv
105735 qcq.bzg
58805 snczcsp
26668 vbn
$ cd ..
$ cd zprprf
$ ls
dir chbmq
dir dcqnblb
dir dqp
dir nfspb
89506 zprprf.hnt
$ cd chbmq
$ ls
dir cnjvw
dir dqp
151434 frsvrdnt
dir msztjvcb
240689 qcq.jlh
dir sjzrcg
97312 vnr.zfr
dir zprprf
$ cd cnjvw
$ ls
dir bpbs
252403 cqhtshc
dir djmjhn
10935 fhqmswr
6582 pdwml.ldd
dir qcq
219282 rfmd
$ cd bpbs
$ ls
147582 bnhwsnsj.gdm
61362 cvcl.jqh
152857 vdgcqdn.sqd
$ cd ..
$ cd djmjhn
$ ls
dir bjdbcjbb
dir dcqnblb
dir dqp
dir lgdwtt
$ cd bjdbcjbb
$ ls
110710 cvcl.jqh
252792 hmshctr.lgz
dir mjhtmbj
189745 shsswcgr
dir tfnhp
194940 vbn
dir zprprf
$ cd mjhtmbj
$ ls
dir dqp
dir hbthpcmb
$ cd dqp
$ ls
200832 sbcrz.qgw
$ cd ..
$ cd hbthpcmb
$ ls
55191 ffcntg
$ cd ..
$ cd ..
$ cd tfnhp
$ ls
276825 dqp
161538 gqmr.wgb
$ cd ..
$ cd zprprf
$ ls
287638 dcqnblb.ssp
41274 hgmrvj.mwf
249118 sbb.gsf
105141 wwrg.gqz
$ cd ..
$ cd ..
$ cd dcqnblb
$ ls
1957 btmmc
32386 dtzbzg.dhm
dir mmrbj
98283 ntmhfgtl.pmf
dir zprprf
$ cd mmrbj
$ ls
273194 wnsq
251527 zprprf
$ cd ..
$ cd zprprf
$ ls
27678 ldzslndn.rrl
62866 ljf.fdj
148502 qcq.dlg
dir rvgqvm
179231 tllnmhn.pjp
64033 vbn
dir zcdrj
$ cd rvgqvm
$ ls
dir ntbv
262324 prhgj.szz
dir qbvdh
$ cd ntbv
$ ls
116608 cgv.fvj
175200 swpswq.twt
$ cd ..
$ cd qbvdh
$ ls
160353 sdhfrb.wjn
$ cd ..
$ cd ..
$ cd zcdrj
$ ls
283262 ctl
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir jfzm
111438 rdrgb.mjf
64194 wgtmqrq
dir zprprf
$ cd jfzm
$ ls
158774 pflp.mdw
$ cd ..
$ cd zprprf
$ ls
215264 sgsstcp
$ cd ..
$ cd ..
$ cd lgdwtt
$ ls
dir qcq
$ cd qcq
$ ls
165461 ldzslndn.vvb
$ cd ..
$ cd ..
$ cd ..
$ cd qcq
$ ls
dir dpd
165044 grpdmd.ggz
82343 ldzslndn
dir mwg
176689 psjcwp.wct
44404 qcq.zwd
$ cd dpd
$ ls
84087 dqp
227386 zprprf.gfs
$ cd ..
$ cd mwg
$ ls
214086 pflp.mdw
dir sjjsdn
225859 wcdt
158892 zprprf.frs
$ cd sjjsdn
$ ls
260121 gplgp.dfn
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir hcrwclpg
dir zphd
$ cd hcrwclpg
$ ls
dir cmqntjj
16393 ldzslndn.qbm
91152 qqdtc.zdq
$ cd cmqntjj
$ ls
272266 ldzslndn.pll
$ cd ..
$ cd ..
$ cd zphd
$ ls
165711 chftwcsw.fqw
256871 cvcl.jqh
251168 zprprf.gfv
$ cd ..
$ cd ..
$ cd msztjvcb
$ ls
206231 brzn.lmn
dir dcqnblb
21571 dqp
dir fmn
45779 mlfctz.cjr
288827 pflp.mdw
220578 qcq.fqf
$ cd dcqnblb
$ ls
198121 ghbwgs
93681 nmqhl.vpq
$ cd ..
$ cd fmn
$ ls
29407 mdfws.qvs
$ cd ..
$ cd ..
$ cd sjzrcg
$ ls
155120 ddclvsjr.rpq
136029 ldzslndn.dcm
dir vhzh
$ cd vhzh
$ ls
212446 vbn
$ cd ..
$ cd ..
$ cd zprprf
$ ls
240335 crt.gqh
185363 gnmm.qgh
dir ldzslndn
dir nwl
dir qll
277043 vbn
217796 vtvgpdl.vtm
$ cd ldzslndn
$ ls
273570 cvcl.jqh
68510 fgdmz.hrc
dir npq
dir swjrzzrm
$ cd npq
$ ls
97923 dzcjsqwt
$ cd ..
$ cd swjrzzrm
$ ls
180599 tmpgn.bjf
$ cd ..
$ cd ..
$ cd nwl
$ ls
171833 dlwrfhh.qgn
$ cd ..
$ cd qll
$ ls
219926 dcqnblb.bvn
$ cd ..
$ cd ..
$ cd ..
$ cd dcqnblb
$ ls
dir lvpb
276198 tbgcm.qct
$ cd lvpb
$ ls
142590 bvhjlld
268259 gnjfg.sgb
dir qcq
206220 qcq.zsg
258137 rrsw.dnb
dir tmr
215549 vbn
$ cd qcq
$ ls
dir mmpgd
dir tdsz
dir tmfvsjwc
$ cd mmpgd
$ ls
70793 jwbnpwnn
$ cd ..
$ cd tdsz
$ ls
246310 tdvrhhg.bzq
$ cd ..
$ cd tmfvsjwc
$ ls
103899 grpdmd.ggz
287850 ldzslndn
125930 llhr
$ cd ..
$ cd ..
$ cd tmr
$ ls
83344 fbtfcg.hqp
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir lbgmcbv
dir nbg
$ cd lbgmcbv
$ ls
81776 wzdzzdp
$ cd ..
$ cd nbg
$ ls
dir mfsgjp
155574 pflp.mdw
$ cd mfsgjp
$ ls
199400 vdgcqdn.sqd
$ cd ..
$ cd ..
$ cd ..
$ cd nfspb
$ ls
262412 csrdtbs
73867 vbn
136389 zqps.hjt
$ cd ..
$ cd ..
$ cd ..
$ cd nhqwt
$ ls
123766 cvcl.jqh
dir dhrtvctp
222086 grpdmd.ggz
dir gzg
26005 lhpmz.tgz
dir mcnjwwfr
117122 msn.gst
$ cd dhrtvctp
$ ls
224079 vdgcqdn.sqd
$ cd ..
$ cd gzg
$ ls
124395 dqp
dir wqdbtqm
$ cd wqdbtqm
$ ls
237354 pflp.mdw
212019 vdgcqdn.sqd
$ cd ..
$ cd ..
$ cd mcnjwwfr
$ ls
92504 cshdztf
dir dctl
dir dqp
dir flcrmhlj
161879 grpdmd.ggz
dir gtt
dir hlbnhchz
220093 mdtdsgvm.zgg
dir twntr
287192 vbn
$ cd dctl
$ ls
dir bbhch
155396 hrrj.jzm
164971 pblqmwj.vdb
dir wnlgfpvf
$ cd bbhch
$ ls
dir dpqtp
dir jvdrcw
$ cd dpqtp
$ ls
174135 gwb.qrb
$ cd ..
$ cd jvdrcw
$ ls
215993 dcqnblb.cqp
200800 stjttf.ngc
$ cd ..
$ cd ..
$ cd wnlgfpvf
$ ls
135978 cvcl.jqh
dir dqp
54018 lbrfmt
$ cd dqp
$ ls
270516 dcqnblb.jqw
dir dqp
144626 grpdmd.ggz
157731 hvcv.rhp
133773 lnnt
76250 vdgcqdn.sqd
$ cd dqp
$ ls
41504 zprprf.cmc
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir dqp
dir ldzslndn
236737 mqzcvm.fjh
239746 nhcdz.ncj
dir rpchqq
248824 vdgcqdn.sqd
250937 zrchht.mwg
$ cd dqp
$ ls
203381 qcq.djm
$ cd ..
$ cd ldzslndn
$ ls
dir dqp
dir fptnzlv
dir gmbnpm
dir vhvblt
$ cd dqp
$ ls
19579 qcq.lhg
$ cd ..
$ cd fptnzlv
$ ls
209930 dcqnblb
$ cd ..
$ cd gmbnpm
$ ls
dir ldzslndn
dir qcq
$ cd ldzslndn
$ ls
11075 pflp.mdw
$ cd ..
$ cd qcq
$ ls
dir tdp
$ cd tdp
$ ls
40741 vdgcqdn.sqd
$ cd ..
$ cd ..
$ cd ..
$ cd vhvblt
$ ls
dir lzr
$ cd lzr
$ ls
62245 gbnj.llg
$ cd ..
$ cd ..
$ cd ..
$ cd rpchqq
$ ls
dir bcs
dir dcqnblb
dir fvjzn
dir lrphzrv
$ cd bcs
$ ls
179794 bbn.dzb
242069 cmjdmzjf.zgf
1703 cvcl.jqh
dir gnmhwj
dir ldzslndn
152520 qltpsz.jsj
dir sqqjfps
$ cd gnmhwj
$ ls
dir gvs
201600 hptn.ftf
dir hzrnb
dir qcq
dir sqhl
$ cd gvs
$ ls
152358 zprprf.mlh
$ cd ..
$ cd hzrnb
$ ls
94290 gplsfd
$ cd ..
$ cd qcq
$ ls
91909 vmqd.bmg
$ cd ..
$ cd sqhl
$ ls
238673 vdgcqdn.sqd
262885 zmdvr.nfg
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
240461 mdz
84303 qtj
$ cd ..
$ cd sqqjfps
$ ls
88753 fwn.tff
$ cd ..
$ cd ..
$ cd dcqnblb
$ ls
dir dqp
189996 dqp.pvp
$ cd dqp
$ ls
dir qvfjz
196506 vbn
$ cd qvfjz
$ ls
209316 pflp.mdw
107459 rwpbh.vpt
$ cd ..
$ cd ..
$ cd ..
$ cd fvjzn
$ ls
241464 cvcl.jqh
dir dqp
dir ldzslndn
dir msp
125 pflp.mdw
131895 vbn
$ cd dqp
$ ls
34019 pflp.mdw
202957 vbn
$ cd ..
$ cd ldzslndn
$ ls
147492 cvcl.jqh
248719 spc.rfv
$ cd ..
$ cd msp
$ ls
184407 cvcl.jqh
$ cd ..
$ cd ..
$ cd lrphzrv
$ ls
dir bbwqmbg
81858 cvcl.jqh
dir dqp
248670 gqqsww.tsn
199141 grpdmd.ggz
dir ldzslndn
34514 ldzslndn.ctw
dir tln
214615 zprprf.fwm
$ cd bbwqmbg
$ ls
129750 flf
dir pvlw
dir qcq
126 sqcqphz.tbm
$ cd pvlw
$ ls
198005 jfvj.hdv
$ cd ..
$ cd qcq
$ ls
dir wgdzws
$ cd wgdzws
$ ls
253522 ldzslndn.qwt
$ cd ..
$ cd ..
$ cd ..
$ cd dqp
$ ls
281993 cvcl.jqh
dir hwqjlwcb
50532 msccz.qgm
102187 trv.tnq
111 wplnmj.bfl
$ cd hwqjlwcb
$ ls
267580 dhjqb.dsb
153195 ldzslndn.jqv
41526 mvwcwc.zsc
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
58666 cvcl.jqh
79950 dqp.tmc
242217 hns.lrb
dir njswzh
240692 vdgcqdn.sqd
dir zvmjvcdm
52909 zzh
$ cd njswzh
$ ls
149732 cvcl.jqh
dir rnmfd
$ cd rnmfd
$ ls
75368 dqp.hmv
14350 vbn
$ cd ..
$ cd ..
$ cd zvmjvcdm
$ ls
dir jgczt
$ cd jgczt
$ ls
dir qcq
95941 qzvvwshv.jwc
$ cd qcq
$ ls
273942 pflp.mdw
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd tln
$ ls
dir bmcng
1518 lrg
dir vnjfrhp
$ cd bmcng
$ ls
38917 fqcrt
$ cd ..
$ cd vnjfrhp
$ ls
dir dcqnblb
dir dqp
247186 grpdmd.ggz
dir ldzslndn
169216 pflp.mdw
206487 vdgcqdn.sqd
16976 vlsrzjmb.mmc
257938 wjl
$ cd dcqnblb
$ ls
dir dqp
$ cd dqp
$ ls
184133 qcq
$ cd ..
$ cd ..
$ cd dqp
$ ls
dir dcqnblb
31612 dqp.pnt
212283 ldzslndn
61600 vdbfc.ddj
197189 wpv.wff
$ cd dcqnblb
$ ls
62412 tfzllmrj
dir zprprf
$ cd zprprf
$ ls
dir bqnpsl
dir dszrvpzc
$ cd bqnpsl
$ ls
261548 spbsbbsw.cmn
$ cd ..
$ cd dszrvpzc
$ ls
188232 sggpqslr.smn
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ldzslndn
$ ls
dir bgnhd
dir pgvcdzwz
dir qgzhm
$ cd bgnhd
$ ls
56989 cvcl.jqh
$ cd ..
$ cd pgvcdzwz
$ ls
110034 qhgnndv
$ cd ..
$ cd qgzhm
$ ls
247232 grpdmd.ggz
269292 ldzslndn
153843 tpz
dir vnschqwr
162392 wnq.btb
$ cd vnschqwr
$ ls
43005 fvtvzfqm.jvc
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd flcrmhlj
$ ls
245668 dcqnblb.sdj
dir lffj
229909 pflp.mdw
280176 vbn
$ cd lffj
$ ls
116451 jmzz.jdd
dir pjlwb
162815 pmhlqq.snr
226183 zffth
$ cd pjlwb
$ ls
67518 qcq.hjq
$ cd ..
$ cd ..
$ cd ..
$ cd gtt
$ ls
52105 grpdmd.ggz
126869 zprprf.fgj
$ cd ..
$ cd hlbnhchz
$ ls
3064 dqp.lrw
278756 grpdmd.ggz
177208 ldzslndn.wlv
141685 vbn
$ cd ..
$ cd twntr
$ ls
63747 cvcl.jqh
$ cd ..
$ cd ..
$ cd ..
$ cd qcq
$ ls
226858 cwblp.zgp
dir jjqsmfhr
dir rjbqtrq
dir vwmpnbts
141715 wdbhdch
286381 zprprf
$ cd jjqsmfhr
$ ls
dir btmm
dir fqndtlgq
$ cd btmm
$ ls
4031 dqp.lrr
dir fzdd
$ cd fzdd
$ ls
dir vnwpn
$ cd vnwpn
$ ls
dir bzlgsl
dir ztvzrrbv
$ cd bzlgsl
$ ls
9294 ldzslndn.sqr
$ cd ..
$ cd ztvzrrbv
$ ls
256017 cvcl.jqh
$ cd ..
$ cd ..
$ cd ..
$ cd ..
$ cd fqndtlgq
$ ls
271528 ccbmgp.bwd
$ cd ..
$ cd ..
$ cd rjbqtrq
$ ls
122150 ldzslndn
46467 tpdvp.pjf
$ cd ..
$ cd vwmpnbts
$ ls
47518 fcrwfzvm
263343 gmc.lrt
212764 qcq
$ cd ..
$ cd ..
$ cd vwqwlqrt
$ ls
dir psrs
$ cd psrs
$ ls
281998 zprprf.hml

59
2022/app/Day8/Main.hs Normal file
View File

@ -0,0 +1,59 @@
module Day8.Main where
import System.IO
import Data.List
main :: IO ()
main = do
putStrLn "Day 8"
handle <- openFile "app/Day8/input" ReadMode
contents <- hGetContents handle
let r1 = part1 contents
putStrLn $ "part 1: " ++ show r1
let r2 = part2 contents
putStrLn $ "part 2: " ++ show r2
type Tree = (Int,[Int],[Int],[Int],[Int]) -- Self, N, E, S, W
enumerate :: [a] -> [(Int, a)]
enumerate = zip [0..]
parseData :: String -> [[Int]]
parseData contents = (map . map) (read . singleton :: Char -> Int) $ lines contents
toTrees :: [[Int]] -> [Tree]
toTrees rows = [(tree, north r c, east r c, south r c, west r c) | (r, line) <- enumerate rows, (c, tree) <- enumerate line]
where
columns = transpose rows
east r c = drop (c + 1) $ rows !! r
west r c = reverse $ take c $ rows !! r
north r c = reverse $ take r $ columns !! c
south r c = drop (r + 1) $ columns !! c
isVisible :: Int -> [Int] -> Bool
isVisible _ [] = True
isVisible t ts = all (< t) ts
invisibleFromOutside :: Tree -> Bool
invisibleFromOutside (t,n,e,s,w) = all (not . isVisible t) [n,e,s,w]
part1 :: String -> Int
part1 = length . filter (not . invisibleFromOutside) . toTrees . parseData
-- Includive version of takeWhile (because lentgh takeWhile + 1 doesn't work for when the outer border is the reason you can't see further)
takeWhileI :: (a -> Bool) -> [a] -> [a]
takeWhileI _ [] = []
takeWhileI p (x:xs) =
x : if p x then takeWhileI p xs else []
viewingDistance :: Int -> [Int] -> Int
viewingDistance t ts = length $ takeWhileI (< t) ts
scenicScore :: Tree -> Int
scenicScore (t,n,e,s,w) = product $ map (viewingDistance t) [n,e,s,w]
part2 :: String -> Int
part2 = maximum . map scenicScore . toTrees . parseData

5
2022/app/Day8/example Normal file
View File

@ -0,0 +1,5 @@
30373
25512
65332
33549
35390

99
2022/app/Day8/input Normal file
View File

@ -0,0 +1,99 @@
011201001000123232122300403211332202122424332111151523152310403222120311202224013311113203311122001
102201211220103202323002411123324024134251423232343141424431144431022143443131141222323122010012220
002022232232102013311311301321044421352134513235443522243113311323213013301030440423203311103101020
002212021110130112400112141310424411421435324412235335534245212325313312413434430114302030310200000
202000312203303110042324124431114212153432535412115323131323541522511253002131401212012131031321110
120203002030304244102444143021314124552253521343321513511124144454343454102412220143321013220111212
012133330030200444414333325255125142322121412245551424242135123513411533153323130432231002121211210
012300220003100211444401144353345522135453435223464636451554321531323513234331131114224102232333310
000201310302031301344341134512444312432566266533252332656654251123535525135213300040211242011230031
121002133124022203240512452433331223524622643252532325346635555552514352434233454024134201432001111
112213110140442401434531255323512525265655352543623565556265545555235551143135124203142220020302000
033001202421424143555311243322416262636322654432542644633525453666655354514351121150231101002002310
133303044321221102531154151541224245536464535622566355532543644552554644312231415325142303020031312
112333344411024045335422225146442243536464622553556234523445362524655623435243115242532313102020021
123023422310204444121415255225254435644446326664473564435422565325224635623542352154120001224144312
102303024142131515433355242322223363453633455657436436574636544336652564626641215232314243234004001
021344140434455245134415553542352643537665534644574553367446664254532523624521243554252411433032112
100140101131444142414254644644434663647646555637737475474473636562243265243536422253531252202124322
111043301134142345422324342454235374733474673456744577746755765343355535622342432251442244140231224
232120141224122324244532255543256467566454337465647735433663734756446566663442525425254525142212311
102310340244433433553362546534535757453376643763344566566557634664535744445254533545223521213213014
330111232252114431226334564636467643777466445537346735563633337744734474466254662225321443542401243
344311321253412213666443354643647347656735478884748547564466577774737677363226422622515344334010341
222340105234132533423442424376674757757675647476557547564445666676437755355626455436313331225343402
334241221121414455435265324646757456774784864846746867457546648735456677555624445353555434433340341
333430021343121666326363537633737767788775478777445558858444854755434544735362564626265444342330020
332224425333452333556456745375454657668644877658744844547774765844454475434765533256545152435214410
411322431421116436336347456774356844455867864644844878664785864444475567664473365652253123453220003
130345455115155526426437663665665668576686587847778476557678655576775553535745643522532344433523042
241333521455132466523664543556384776587787745658797755565556576546847447434775733626365325451531301
202135433134452323646546565545646885747548679876767658578565476458486465755545465664435422532454541
344025243154435524235475655536845545775797795959686975588855546544875685664377465455353661444452422
120511445324333353265334766654858656857755669695597899959957757545465687447345336332322634434414522
041425252513666643357433333888555778459755889566967799659865567757457567576463765323236366123221323
142343534134652443554434768544658774587669979986775985989879787975445856456554364645323653454511323
443155144123223566654765746758688875856899955996667659755976778586644774777456744653322566354532232
013242522162435447764573774877757879868788876799896979576555597985885468586473474464455426633333452
101431333345553353553337548577478656967866886877887889699999856979976485874455544477534422463331314
422325541533664573334736464857446675995799977868697688998787757585668584775766563477536636621514142
313315332565562573637376866486547776557979868979996867779966955885566785455767644565336236452541411
231122455364326235655644685888786685587796897688777698977986787596655865857448333576665523356143332
314415154446535337443754887576795586569678967879977696896667866768856785586774365336636426455324541
251324556334435537567468544665859588888869687767876697967887867856679658788866355353346553442543353
134353225423362456734666455757877769577678987866887877799886886885859796767664444665534363236114533
251241346545626367667578575687955957988796678987999979977897978877579787844755554533763646262143315
353543555544353753474346565789555799696999667888997899777669899867697878476845756644734243432245123
221521465632632737777766744748678869769669668898887888987898666789869785645678734643552655525624335
351552456455622567337644647486678857766866879798779999878779869885869788874885873744772665326352324
153242153665224557353455465857685566797887699998988788877767696668668675666664465757646436665554315
321113152666424547657348876876678597898779877779998999978978777885976658558676664444674243656333212
421511245364456777655754476646885699898678788797778788778778999795687589454774563737333365434325525
213532265252653474345766566877669898969767779777879797899977669868595997784466766655547455234435452
125232235356525353764645486577668857868788699799787978898988669878688867586564457353673236664223343
441242562562223643773466848558599657669669968987979987787969676777796877655876456745562442232541525
135122156635652746574578664849659556998698797778797889988788869797687977855447474475546334662522444
144315413442434666777478887855885568968799966889978797797867786966685589646488554737453225335213325
511213234443636433646678657654587788667696879878989799867699988765895758788675765445656425425334544
052145146243336363474565857687976889776768867898889967686967668565998666768575534675445244546424155
235513424332444756467574556745997958679678999799789777998968876679956958746554677637762355253554335
331155256654246447545776676487485655676699678967897779688898989999768655488874564764434223445132152
152152253254255234473347878744485766678897879689897888667968795675655864748584644653323363464154251
245154344235223346354765457746867586969968687876867899766986679689585778787473555663424264553541344
235253541263666477544377746867785766579566868896689699876766695956667455774677646576554255521144355
033522343456423455634354575764856898657669957896677776766596688857787474488436433455326554242331222
321532413246333226435563556578864759978657759897896799958558598588975755858856537673332523244522141
101422552442534324467345475684574877989759655788785857697957976679848768474665346435664653421242444
103425135453353666736674677847548667967885667765576995797857589796866646565443377775564436443333543
041131245522253556554636537878457787468577768876778599999689667978487755747676763666266652435545351
424232344536333364556466767555646468677996965895798968676966868665587584664535544755645232211324114
334034423343366322247357747635787857555667756956768657779596877655676555367754633345545442422145333
424344345121532564223643563774744574746844686888885779565686455664565643634566756543362551431252401
232123114442152332546734767375678475664877645798695799654765488755876545334345452455463633534433233
414423121145222454332477456654436757485685646546884446667744645744675377357654436554353221451345404
234130223314353235262655676637577858558554556467788664555466588778677546456334642623666524333250443
414001252524152463224524734765775677644765886656877578666575855788653533364773455446565352432424320
024323134311552535243564667476365737764685474446876486685546667685363735646643655435355533243440034
331141044111451522226436327536773637654887585478885586588745847444466365767232265352211455413531431
241321311312323233252665352455436545735388846445565487667644555744667753556266645332452431541341044
234030032312154414563442653244476576544654676884777445766434443476675746365245632242515342521301442
202112004552434344564624422526757574735736566643654737673776743354737744242533546523434111443034202
323422334452422412134566522262537334374446646774476534347655656336555632626663364214333341322443300
031243421344143515452456444553266345437737337675574563437533573575456463665443432215213541123241240
002114441013421154331235436253442576764555535635775735635377573353666526333623554524421352131443020
111342301013125124521245222663363363467436357434736773473777464564655622533254352444544553311114233
222323000331131531351433363334345533653474663466435557365553376535246564324662454422135233103003200
002103300342403311123432563445263435443255635353333564444573456633466424623313351545133022013122320
300304042333200223123432352234525626625234236453763536563235242224645265643515312312520000242200000
210121244033001141551213125554433445222532265463624234562324443435344362451243522525243002401410013
303323342040431313415255233251226626334264553655346266323665655652522363324341123235410333230402230
300112203021311400115231251543524354554262365526643622264625434236554412121342322522123434120120203
011001023222004142422445443143212526654365444643442536365453622222634154551441553400220414300301332
230032020342121242113341435444543253355352433246243543645634456552525315313143243431133302112320020
232132202131312144424224413332214322543242664652323324456222615212244414145113442320023403200012111
112222030320432400004224215552515412325221525356363643351335211141344255554442403013303120331312202
110321230332323212301432002424211134155124241552254334221154111545254253122214132321141212032221001
021020121321302320401011302153145251455114411155452245422422135453412153112033343412340101230302010
122203012232330222213342133012334131225335225325343355122151154444535354414423434423220001330003020
221120213213221333210131242440001425551432522221141211124544343151315104244333311124033221102312011
202201033331203122413021242022211002455531321354252412214332215513311231404032244320020330332301120

View File

@ -5,6 +5,9 @@ import qualified Day2.Main as Day2
import qualified Day3.Main as Day3
import qualified Day4.Main as Day4
import qualified Day5.Main as Day5
import qualified Day6.Main as Day6
import qualified Day7.Main as Day7
import qualified Day8.Main as Day8
main :: IO ()
main = do
@ -13,3 +16,6 @@ main = do
Day3.main
Day4.main
Day5.main
Day6.main
Day7.main
Day8.main

View File

@ -1,6 +1,6 @@
{
pkgs ? import <nixpkgs> {},
hc ? "ghc924"
hc ? "ghc923"
}:
pkgs.stdenv.mkDerivation rec {