<commit_before>-- --------------------------------------------------------------- [ Day03.idr ]
-- Module      : Data.Advent.Day03
-- Description : My solution to the Day 3 puzzle of the 2016 Advent of Code.
-- Copyright   : Copyright (c) 2016, <NAME>
-- License     : MIT
-- Link        : http://adventofcode.com/2016/day/3
-- --------------------------------------------------------------------- [ EOH ]
||| Day 3: Squares with Three Sides
module Data.Advent.Day03

import public Lightyear
import public Lightyear.Char
import public Lightyear.Strings
import public Lightyear.StringFile

%access export

-- ----------------------------------------------------------------- [ Parsers ]

triangle : Parser (Vect 3 Integer)
triangle = ntimes 3 (spaces *> integer) <?> "three side lengths"

triangles : Parser (List (Vect 3 Integer))
triangles = some triangle <?> "a list of triangle side lengths"

-- ------------------------------------------------------------------- [ Logic ]

isEquilateral : Vect 3 Integer -> Bool
isEquilateral [x,y,z] = x == y && y == z

isLogical : Vect 3 Integer -> Bool
isLogical [x,y,z] = x < y + z && y < z + x && z < x + y

isPossible : Vect 3 Integer -> Bool
isPossible sides = isEquilateral sides || isLogical sides

countPossible : List (Vect 3 Integer) -> Integer
countPossible = foldl go 0
  where
    go : Integer -> Vect 3 Integer -> Integer
    go n sides = if isPossible sides then n + 1 else n

-- ---------------------------------------------------------------- [ Part One ]

namespace PartOne

    main : IO ()
    main =
        do Right input <- run $ parseFile rErr pErr triangles "input/day03.txt"
             | Left err => putStrLn err
           printLn $ countPossible input
      where
        rErr : String -> FileError -> String
        rErr = const show
        pErr : String -> String -> String
        pErr = const id

-- -------------------------------------------------------------------- [ Main ]

namespace Main

    main : IO ()
    main = putStr "Part One: " *> PartOne.main

-- --------------------------------------------------------------------- [ EOF ]<commit_msg>[Day 3] Add generalized main’<commit_after>-- --------------------------------------------------------------- [ Day03.idr ]
-- Module      : Data.Advent.Day03
-- Description : My solution to the Day 3 puzzle of the 2016 Advent of Code.
-- Copyright   : Copyright (c) 2016, <NAME>
-- License     : MIT
-- Link        : http://adventofcode.com/2016/day/3
-- --------------------------------------------------------------------- [ EOH ]
||| Day 3: Squares with Three Sides
module Data.Advent.Day03

import public Lightyear
import public Lightyear.Char
import public Lightyear.Strings
import public Lightyear.StringFile

%access export

-- ----------------------------------------------------------------- [ Parsers ]

triangle : Parser (Vect 3 Integer)
triangle = ntimes 3 (spaces *> integer) <?> "three side lengths"

triangles : Parser (List (Vect 3 Integer))
triangles = some triangle <?> "a list of triangle side lengths"

-- ------------------------------------------------------------------- [ Logic ]

isEquilateral : Vect 3 Integer -> Bool
isEquilateral [x,y,z] = x == y && y == z

isLogical : Vect 3 Integer -> Bool
isLogical [x,y,z] = x < y + z && y < z + x && z < x + y

isPossible : Vect 3 Integer -> Bool
isPossible sides = isEquilateral sides || isLogical sides

countPossible : List (Vect 3 Integer) -> Integer
countPossible = foldl go 0
  where
    go : Integer -> Vect 3 Integer -> Integer
    go n sides = if isPossible sides then n + 1 else n

main' : Parser (List (Vect 3 Integer)) -> IO ()
main' p = either putStrLn (printLn . countPossible)
                 !(run $ parseFile (const show) (const id) p "input/day03.txt")

-- ---------------------------------------------------------------- [ Part One ]

namespace PartOne

    main : IO ()
    main = main' triangles

-- -------------------------------------------------------------------- [ Main ]

namespace Main

    main : IO ()
    main = putStr "Part One: " *> PartOne.main

-- --------------------------------------------------------------------- [ EOF ]
{'fraction_non_alphanumeric': 0.3141688717825355, 'fraction_numerical': 0.016357950445032476, 'mean_word_length': 1.2959690778575372, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3363652', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1040, 'n_words': 386}