From bb4e947b1e698e036b6b5462b7c0d22d091014fd Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sat, 2 Dec 2017 11:49:45 +0000 Subject: [PATCH] Done day 2 --- adventofcode1702/README.md | 1 + adventofcode1702/Setup.hs | 2 + adventofcode1702/adventofcode1702.cabal | 27 ++ adventofcode1702/app/advent02.hs | 44 ++++ adventofcode1702/app/advent02.ipynb | 325 ++++++++++++++++++++++++ adventofcode1702/src/Main.hs | 5 + data/advent02.txt | 16 ++ stack.yaml | 77 +----- 8 files changed, 427 insertions(+), 70 deletions(-) create mode 100644 adventofcode1702/README.md create mode 100644 adventofcode1702/Setup.hs create mode 100644 adventofcode1702/adventofcode1702.cabal create mode 100644 adventofcode1702/app/advent02.hs create mode 100644 adventofcode1702/app/advent02.ipynb create mode 100644 adventofcode1702/src/Main.hs create mode 100644 data/advent02.txt diff --git a/adventofcode1702/README.md b/adventofcode1702/README.md new file mode 100644 index 0000000..5c53720 --- /dev/null +++ b/adventofcode1702/README.md @@ -0,0 +1 @@ +# adventofcode1701 diff --git a/adventofcode1702/Setup.hs b/adventofcode1702/Setup.hs new file mode 100644 index 0000000..9a994af --- /dev/null +++ b/adventofcode1702/Setup.hs @@ -0,0 +1,2 @@ +import Distribution.Simple +main = defaultMain diff --git a/adventofcode1702/adventofcode1702.cabal b/adventofcode1702/adventofcode1702.cabal new file mode 100644 index 0000000..994616e --- /dev/null +++ b/adventofcode1702/adventofcode1702.cabal @@ -0,0 +1,27 @@ +name: adventofcode1702 +version: 0.1.0.0 +-- synopsis: +-- description: +homepage: https://github.com/neilnjae/advent-of-code-17#readme +license: BSD3 +license-file: LICENSE +author: Neil Smith +maintainer: noone@njae.me.uk +copyright: 2017 Neil Smith +category: None +build-type: Simple +cabal-version: >=1.10 +extra-source-files: README.md + +library + hs-source-dirs: src + build-depends: base >= 4.7 && < 5 + default-language: Haskell2010 + +executable advent02 + hs-source-dirs: app + main-is: advent02.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , parsec + , parsec-numbers diff --git a/adventofcode1702/app/advent02.hs b/adventofcode1702/app/advent02.hs new file mode 100644 index 0000000..c7a4348 --- /dev/null +++ b/adventofcode1702/app/advent02.hs @@ -0,0 +1,44 @@ +module Main(main) where + +import Text.Parsec +import Text.ParserCombinators.Parsec.Number + + +main :: IO () +main = do + text <- readFile "data/advent02.txt" + let sheet = successfulParse $ parseFile text + print $ part1 sheet + print $ part2 sheet + + +part1 :: [[Int]] -> Int +part1 = sum . map p1cSum + +part2 :: [[Int]] -> Int +part2 = sum . map p2cSum + + +p1cSum :: [Int] -> Int +p1cSum row = (maximum row) - (minimum row) + +p2cSum :: [Int] -> Int +p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0] + + + +sFile = sLine `sepEndBy` newline +sLine = int `sepBy` onlySpaces + +onlySpaces = many (oneOf " \t") + +parseFile :: String -> Either ParseError [[Int]] +parseFile input = parse sFile "(unknown)" input + +parseLine :: String -> Either ParseError [Int] +parseLine input = parse sLine "(unknown)" input + +successfulParse :: Either ParseError [a] -> [a] +successfulParse (Left _) = [] +successfulParse (Right a) = a + diff --git a/adventofcode1702/app/advent02.ipynb b/adventofcode1702/app/advent02.ipynb new file mode 100644 index 0000000..66a1ac9 --- /dev/null +++ b/adventofcode1702/app/advent02.ipynb @@ -0,0 +1,325 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "import Text.Parsec \n", + "import Text.ParserCombinators.Parsec.Number" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [], + "source": [ + "sFile = sLine `sepEndBy` newline \n", + "sLine = int `sepBy` onlySpaces" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "onlySpaces = many (oneOf \" \\t\")" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "parseFile :: String -> Either ParseError [[Int]]\n", + "parseFile input = parse sFile \"(unknown)\" input\n", + "\n", + "parseLine :: String -> Either ParseError [Int]\n", + "parseLine input = parse sLine \"(unknown)\" input\n", + "\n", + "successfulParse :: Either ParseError [a] -> [a]\n", + "successfulParse (Left _) = []\n", + "successfulParse (Right a) = a" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent02.txt\"\n", + " let sheet = successfulParse $ parseFile text\n", + "-- print sheet\n", + " print $ part1 sheet\n", + " print $ part2 sheet" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "39126\n", + "258" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Right [179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "parseLine \"179 2358 5197 867 163 4418 3135 5049 187 166 4682 5080 5541 172 4294 1397\"" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Right [[1,2],[8,9]]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "parseFile \"1 2\\n8 9\"" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[1,2],[8,9]]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "successfulParse $ parseFile \"1 2\\n8 9\"" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "cSum :: [Int] -> Int\n", + "cSum row = (maximum row) - (minimum row)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "check :: [[Int]] -> Int\n", + "check = sum . (map cSum)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "check $ successfulParse $ parseFile \"1 2\\n8 9\"" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [], + "source": [ + "part1 = check" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(2,1),(3,1),(4,1),(4,2)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "digits = [1,2,3,4]\n", + "[(a, b) | a <- digits, b <- digits, a /= b, a `mod` b == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(1,1),(2,2),(3,3),(4,4)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "zip digits digits" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "Right digits = parseLine \"179 2358 5197 867 163 4418 3135 5049 187 166 4682 5080 5541 172 4294 1397\"\n", + "digits" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [], + "source": [ + "part2 = sum . map p2cSum" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Haskell", + "language": "haskell", + "name": "haskell" + }, + "language_info": { + "codemirror_mode": "ihaskell", + "file_extension": ".hs", + "name": "haskell", + "version": "8.0.2" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/adventofcode1702/src/Main.hs b/adventofcode1702/src/Main.hs new file mode 100644 index 0000000..9cd992d --- /dev/null +++ b/adventofcode1702/src/Main.hs @@ -0,0 +1,5 @@ +module Main where + +main :: IO () +main = do + putStrLn "hello world" diff --git a/data/advent02.txt b/data/advent02.txt new file mode 100644 index 0000000..240b768 --- /dev/null +++ b/data/advent02.txt @@ -0,0 +1,16 @@ +179 2358 5197 867 163 4418 3135 5049 187 166 4682 5080 5541 172 4294 1397 +2637 136 3222 591 2593 1982 4506 195 4396 3741 2373 157 4533 3864 4159 142 +1049 1163 1128 193 1008 142 169 168 165 310 1054 104 1100 761 406 173 +200 53 222 227 218 51 188 45 98 194 189 42 50 105 46 176 +299 2521 216 2080 2068 2681 2376 220 1339 244 605 1598 2161 822 387 268 +1043 1409 637 1560 970 69 832 87 78 1391 1558 75 1643 655 1398 1193 +90 649 858 2496 1555 2618 2302 119 2675 131 1816 2356 2480 603 65 128 +2461 5099 168 4468 5371 2076 223 1178 194 5639 890 5575 1258 5591 6125 226 +204 205 2797 2452 2568 2777 1542 1586 241 836 3202 2495 197 2960 240 2880 +560 96 336 627 546 241 191 94 368 528 298 78 76 123 240 563 +818 973 1422 244 1263 200 1220 208 1143 627 609 274 130 961 685 1318 +1680 1174 1803 169 450 134 3799 161 2101 3675 133 4117 3574 4328 3630 4186 +1870 3494 837 115 1864 3626 24 116 2548 1225 3545 676 128 1869 3161 109 +890 53 778 68 65 784 261 682 563 781 360 382 790 313 785 71 +125 454 110 103 615 141 562 199 340 80 500 473 221 573 108 536 +1311 64 77 1328 1344 1248 1522 51 978 1535 1142 390 81 409 68 352 \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index 3993b53..14cf15c 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,73 +1,10 @@ -# This file was automatically generated by 'stack init' -# -# Some commonly used options have been documented as comments in this file. -# For advanced use and comprehensive documentation of the format, please see: -# http://docs.haskellstack.org/en/stable/yaml_configuration/ - -# Resolver to choose a 'specific' stackage snapshot or a compiler version. -# A snapshot resolver dictates the compiler version and the set of packages -# to be used for project dependencies. For example: -# -# resolver: lts-3.5 -# resolver: nightly-2015-09-21 -# resolver: ghc-7.10.2 -# resolver: ghcjs-0.1.0_ghc-7.10.2 -# resolver: -# name: custom-snapshot -# location: "./custom-snapshot.yaml" -resolver: lts-9.14 - -# User packages to be built. -# Various formats can be used as shown in the example below. -# -# packages: -# - some-directory -# - https://example.com/foo/bar/baz-0.0.2.tar.gz -# - location: -# git: https://github.com/commercialhaskell/stack.git -# commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a -# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a -# extra-dep: true -# subdirs: -# - auto-update -# - wai -# -# A package marked 'extra-dep: true' will only be built if demanded by a -# non-dependency (i.e. a user package), and its test suites and benchmarks -# will not be run. This is useful for tweaking upstream packages. -packages: -- adventofcode1701/ -# Dependency packages to be pulled from upstream that are not in the resolver -# (e.g., acme-missiles-0.3) -extra-deps: [] - -# Override default flag values for local packages and extra-deps flags: {} - -# Extra package databases containing global packages extra-package-dbs: [] - -# Control whether we use the GHC we find on the path -# system-ghc: true -# -# Require a specific version of stack, using version ranges -# require-stack-version: -any # Default -# require-stack-version: ">=1.1" -# -# Override the architecture used by stack, especially useful on Windows -# arch: i386 -# arch: x86_64 -# -# Extra directories used by stack for building -# extra-include-dirs: [/path/to/dir] -# extra-lib-dirs: [/path/to/dir] -# -# Allow a newer minor version of GHC than the snapshot specifies -# compiler-check: newer-minor - -# Include GHC options ghc-options: - # All packages - "*": -O2 -Wall -Wno-missing-signatures -threaded -rtsopts -with-rtsopts=-N - # for some package: - # some-package: -DSOME_CPP_FLAG + ! '*': -O2 -Wall -Wno-missing-signatures -threaded -rtsopts -with-rtsopts=-N +packages: +- adventofcode1701/ +- adventofcode1702/ +extra-deps: +- parsec-numbers-0.1.0 +resolver: lts-9.14 -- 2.34.1