mirror of
https://github.com/macocianradu/haskell-advent-of-code-2025.git
synced 2026-03-18 13:00:06 +00:00
day 2
This commit is contained in:
5
day2/CHANGELOG.md
Normal file
5
day2/CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Revision history for day2
|
||||
|
||||
## 0.1.0.0 -- YYYY-mm-dd
|
||||
|
||||
* First version. Released on an unsuspecting world.
|
||||
20
day2/LICENSE
Normal file
20
day2/LICENSE
Normal file
@@ -0,0 +1,20 @@
|
||||
Copyright (c) 2026 radumaco
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included
|
||||
in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
32
day2/app/Main.hs
Normal file
32
day2/app/Main.hs
Normal file
@@ -0,0 +1,32 @@
|
||||
module Main (main) where
|
||||
|
||||
import Data.List.Split
|
||||
import System.Environment
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
input <- readFile (args !! 0)
|
||||
let ranges = concat $ map parseRange $ splitOn "," input
|
||||
let invalidNumbers = filter (not . isValid) ranges
|
||||
print invalidNumbers
|
||||
print $ sum invalidNumbers
|
||||
|
||||
parseRange :: String -> [Int]
|
||||
parseRange rng = let s = splitOn "-" rng in
|
||||
[read $ s !! 0..read $ s !! 1]
|
||||
|
||||
isValid :: Int -> Bool
|
||||
isValid numb = let str = show numb in
|
||||
not $ equalSplits 1 str
|
||||
|
||||
|
||||
equalSplits :: Int -> String -> Bool
|
||||
equalSplits sp str = case sp > length str `div` 2 of
|
||||
True -> False
|
||||
False -> if allEqual (chunksOf sp str) then True else equalSplits (sp + 1) str
|
||||
|
||||
allEqual :: [String] -> Bool
|
||||
allEqual [] = True
|
||||
allEqual [_] = True
|
||||
allEqual (x:x2:t) = if x == x2 then allEqual (x2:t) else False
|
||||
1
day2/app/input1.txt
Normal file
1
day2/app/input1.txt
Normal file
@@ -0,0 +1 @@
|
||||
16100064-16192119,2117697596-2117933551,1-21,9999936269-10000072423,1770-2452,389429-427594,46633-66991,877764826-877930156,880869-991984,18943-26512,7216-9427,825-1162,581490-647864,2736-3909,39327886-39455605,430759-454012,1178-1741,219779-244138,77641-97923,1975994465-1976192503,3486612-3602532,277-378,418-690,74704280-74781349,3915-5717,665312-740273,69386294-69487574,2176846-2268755,26-45,372340114-372408052,7996502103-7996658803,7762107-7787125,48-64,4432420-4462711,130854-178173,87-115,244511-360206,69-86
|
||||
1
day2/app/test-input1.txt
Normal file
1
day2/app/test-input1.txt
Normal file
@@ -0,0 +1 @@
|
||||
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||
77
day2/day2.cabal
Normal file
77
day2/day2.cabal
Normal file
@@ -0,0 +1,77 @@
|
||||
cabal-version: 3.14
|
||||
-- The cabal-version field refers to the version of the .cabal specification,
|
||||
-- and can be different from the cabal-install (the tool) version and the
|
||||
-- Cabal (the library) version you are using. As such, the Cabal (the library)
|
||||
-- version used must be equal or greater than the version stated in this field.
|
||||
-- Starting from the specification version 2.2, the cabal-version field must be
|
||||
-- the first thing in the cabal file.
|
||||
|
||||
-- Initial package description 'day2' generated by
|
||||
-- 'cabal init'. For further documentation, see:
|
||||
-- http://haskell.org/cabal/users-guide/
|
||||
--
|
||||
-- The name of the package.
|
||||
name: day2
|
||||
|
||||
-- The package version.
|
||||
-- See the Haskell package versioning policy (PVP) for standards
|
||||
-- guiding when and how versions should be incremented.
|
||||
-- https://pvp.haskell.org
|
||||
-- PVP summary: +-+------- breaking API changes
|
||||
-- | | +----- non-breaking API additions
|
||||
-- | | | +--- code changes with no API change
|
||||
version: 0.1.0.0
|
||||
|
||||
-- A short (one-line) description of the package.
|
||||
-- synopsis:
|
||||
|
||||
-- A longer description of the package.
|
||||
-- description:
|
||||
|
||||
-- The license under which the package is released.
|
||||
license: MIT
|
||||
|
||||
-- The file containing the license text.
|
||||
license-file: LICENSE
|
||||
|
||||
-- The package author(s).
|
||||
author: radumaco
|
||||
|
||||
-- An email address to which users can send suggestions, bug reports, and patches.
|
||||
maintainer: rmacocian@yahoo.com
|
||||
|
||||
-- A copyright notice.
|
||||
-- copyright:
|
||||
build-type: Simple
|
||||
|
||||
-- Extra doc files to be distributed with the package, such as a CHANGELOG or a README.
|
||||
extra-doc-files: CHANGELOG.md
|
||||
|
||||
-- Extra source files to be distributed with the package, such as examples, or a tutorial module.
|
||||
-- extra-source-files:
|
||||
|
||||
common warnings
|
||||
ghc-options: -Wall
|
||||
|
||||
executable day2
|
||||
-- Import common warning flags.
|
||||
import: warnings
|
||||
|
||||
-- .hs or .lhs file containing the Main module.
|
||||
main-is: Main.hs
|
||||
|
||||
-- Modules included in this executable, other than Main.
|
||||
-- other-modules:
|
||||
|
||||
-- LANGUAGE extensions used by modules in this package.
|
||||
-- other-extensions:
|
||||
|
||||
-- Other library packages from which modules are imported.
|
||||
build-depends: base ^>=4.22.0.0,
|
||||
split >= 0.2.5
|
||||
|
||||
-- Directories containing source files.
|
||||
hs-source-dirs: app
|
||||
|
||||
-- Base language which the package is written in.
|
||||
default-language: GHC2024
|
||||
Reference in New Issue
Block a user