This commit is contained in:
chayleaf 2021-12-02 18:13:56 +07:00
commit 6c76e0d224
10 changed files with 2129 additions and 0 deletions

7
.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
*
!/*
!hs/*.hs
!rkt/*.rkt
!pl/*.pl
!.gitignore
!input/*

5
LICENSE Normal file
View file

@ -0,0 +1,5 @@
Copyright (C) 2021 by chayleaf <chayleaf@protonmail.com>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

3
README.md Normal file
View file

@ -0,0 +1,3 @@
# Advent of Code 2021
Decided to learn 3 new languages at a time! Hopefully I will last...

8
hs/01.1.hs Normal file
View file

@ -0,0 +1,8 @@
import System.IO
main = print . snd . foldl step (Nothing, 0) . map (read :: String -> Int) . lines =<< readFile "../input/01"
step (Nothing, sum) n = (Just n, sum)
step ((Just last), sum) n = (Just n, sum +
(if n > last then 1 else 0))
step :: (Maybe Int, Int) -> Int -> (Maybe Int, Int)

9
hs/01.2.hs Normal file
View file

@ -0,0 +1,9 @@
import System.IO
main = print . snd . foldl step ([], 0) . map (read :: String -> Int) . lines =<< readFile "../input/01"
step ([], sum) n = ([n], sum)
step ([a], sum) n = ([a, n], sum)
step ([a, b], sum) n = ([a, b, n], sum)
step ([a, b, c], sum) n = ([b, c, n], sum + if n > a then 1 else 0)
step :: ([Int], Int) -> Int -> ([Int], Int)

2000
input/01 Normal file

File diff suppressed because it is too large Load diff

30
pl/01.1.pl Normal file
View file

@ -0,0 +1,30 @@
read_file(Stream, []) :-
at_end_of_stream(Stream).
read_file(Stream, [X|L]) :-
\+ at_end_of_stream(Stream),
read_line_to_codes(Stream, C),
atom_chars(A, C),
read_file(Stream, L),
atom_number(A, X).
main :-
open('../input/01', read, Stream),
read_file(Stream, Lines), !,
close(Stream),
step(Lines, N),
print(N).
step([], 0).
step([A|Tail], B) :-
step_(A, Tail, B).
step_(_, [], 0).
step_(A, [B|Tail], Res) :-
A < B,
step_(B, Tail, TailRes),
Res is TailRes + 1.
step_(A, [B|Tail], Res) :-
A >= B,
step_(B, Tail, Res).

36
pl/01.2.pl Normal file
View file

@ -0,0 +1,36 @@
read_file(Stream, []) :-
at_end_of_stream(Stream).
read_file(Stream, [X|L]) :-
\+ at_end_of_stream(Stream),
read_line_to_codes(Stream, C),
atom_chars(A, C),
read_file(Stream, L),
atom_number(A, X).
main :-
open('../input/01', read, Stream),
read_file(Stream, Lines), !,
close(Stream),
step(Lines, N),
print(N).
step([], 0).
step([_], 0).
step([_, _], 0).
step([A|Tail], Res) :-
step1(A, Tail, Res).
step1(A, [B|Tail], Res) :-
step2(A, B, Tail, Res).
step2(A, B, [C|Tail], Res) :-
step_(A, B, C, Tail, Res).
step_(_, _, _, [], 0).
step_(A, B, C, [N|Tail], Res) :-
A < N,
step_(B, C, N, Tail, TailRes),
Res is TailRes + 1.
step_(A, B, C, [N|Tail], Res) :-
A >= N,
step_(B, C, N, Tail, Res).

14
rkt/01.1.rkt Normal file
View file

@ -0,0 +1,14 @@
#lang racket
(with-input-from-file "../input/01"
(lambda ()
(for/fold ([cnt 0]
[last null]
#:result cnt)
([line (in-lines)])
(let ([num (string->number line)])
(values (cond [(null? last) cnt]
[(> num last) (+ cnt 1)]
[else cnt])
num)))))

17
rkt/01.2.rkt Normal file
View file

@ -0,0 +1,17 @@
#lang racket
(with-input-from-file "../input/01"
(lambda ()
(for/fold ([cnt 0]
[last null]
#:result cnt)
([line (in-lines)])
(let ([num (string->number line)])
(if (< (length last) 3)
(values cnt (append last (list num)))
(values
(if (> num (car last))
(+ cnt 1)
cnt)
(append (cdr last) (list num))))))))