mirror of
https://github.com/macocianradu/haskell-advent-of-code-2025.git
synced 2026-03-18 13:00:06 +00:00
day 1
This commit is contained in:
5
day1/CHANGELOG.md
Normal file
5
day1/CHANGELOG.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
# Revision history for day1
|
||||||
|
|
||||||
|
## 0.1.0.0 -- YYYY-mm-dd
|
||||||
|
|
||||||
|
* First version. Released on an unsuspecting world.
|
||||||
20
day1/LICENSE
Normal file
20
day1/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.
|
||||||
31
day1/app/Main.hs
Normal file
31
day1/app/Main.hs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
module Main (main) where
|
||||||
|
|
||||||
|
import System.Environment
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
path <- getArgs
|
||||||
|
file <- readFile $ path !! 0
|
||||||
|
let instructions = map parseInput $ lines file
|
||||||
|
let result = foldl rotate (0, 50) instructions
|
||||||
|
print result
|
||||||
|
|
||||||
|
parseInput :: String -> (Rotation, Int)
|
||||||
|
parseInput (dir:num) = (getRotation dir, read num)
|
||||||
|
parseInput _ = error "Invalid Input"
|
||||||
|
|
||||||
|
data Rotation = LEFT | RIGHT deriving (Show)
|
||||||
|
|
||||||
|
getRotation :: Char -> Rotation
|
||||||
|
getRotation 'L' = LEFT
|
||||||
|
getRotation 'R' = RIGHT
|
||||||
|
getRotation _ = error "Invalid Rotation"
|
||||||
|
|
||||||
|
|
||||||
|
rotate :: (Int, Int) -> (Rotation, Int) -> (Int, Int)
|
||||||
|
rotate (acc, pos) (RIGHT, num) = ((pos + num) `div` 100 + acc, (pos + num) `mod` 100)
|
||||||
|
rotate (acc, pos) (LEFT, num) = let totalPos = pos - num
|
||||||
|
offset = if pos == 0 then -1 else 0 in
|
||||||
|
case totalPos `mod` 100 of
|
||||||
|
0 -> (abs (totalPos `div` 100) + acc + 1 + offset, 0)
|
||||||
|
newPos -> (abs (totalPos `div` 100) + acc + offset, newPos)
|
||||||
4498
day1/app/input1.txt
Normal file
4498
day1/app/input1.txt
Normal file
File diff suppressed because it is too large
Load Diff
10
day1/app/test-input1.txt
Normal file
10
day1/app/test-input1.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
L68
|
||||||
|
L30
|
||||||
|
R48
|
||||||
|
L5
|
||||||
|
R60
|
||||||
|
L55
|
||||||
|
L1
|
||||||
|
L99
|
||||||
|
R14
|
||||||
|
L82
|
||||||
76
day1/day1.cabal
Normal file
76
day1/day1.cabal
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
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 'day1' generated by
|
||||||
|
-- 'cabal init'. For further documentation, see:
|
||||||
|
-- http://haskell.org/cabal/users-guide/
|
||||||
|
--
|
||||||
|
-- The name of the package.
|
||||||
|
name: day1
|
||||||
|
|
||||||
|
-- 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 day1
|
||||||
|
-- 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
|
||||||
|
|
||||||
|
-- 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