Compare commits

...

10 Commits

Author SHA1 Message Date
Max Hohlfeld
f40104937d erlang day1 2023-08-14 09:36:36 +02:00
Max Hohlfeld
fd69c6e68c finish scala 2023-07-11 13:19:24 +02:00
Max Hohlfeld
683e77dd4f scala day3 wip 2023-05-17 15:00:12 +02:00
Max Hohlfeld
0a92c9cec9 scala day 2 2023-05-12 12:42:18 +02:00
Max Hohlfeld
97920a52fe scala day 1 2023-04-24 09:10:53 +02:00
Max Hohlfeld
541b11a50b prolog day 3 2023-04-19 10:46:09 +02:00
Max Hohlfeld
036fc642ba prolog day 2 2023-04-19 10:46:09 +02:00
Max Hohlfeld
63c2afdf29 prolog day 1 2023-04-19 10:46:09 +02:00
9571a9f81a io day 2 2023-04-10 17:11:27 +02:00
759f45a04b io day 1 2023-04-10 17:11:06 +02:00
11 changed files with 491 additions and 0 deletions

13
erlang/day1.erl Normal file
View File

@ -0,0 +1,13 @@
-module(day1).
-export([charcount/1]).
-export([counter/1]).
-export([print/1]).
charcount([]) -> 0;
charcount([_ | Rest]) -> 1 + charcount(Rest).
counter(10) -> 10;
counter(N) -> counter(N + 1).
print(success) -> io:write("success");
print({error, Message}) -> io:write(unicode:characters_to_list(["error: ", Message], utf8)).

16
io/day1.io Normal file
View File

@ -0,0 +1,16 @@
# Io ist stark typisiert, weil z.B. 1 + "one" nicht funktioniert / Fehler schmeißt
# 0 entspricht true; "" entspricht true; nil entspricht false
# um Slots eines Prototypen zu entdecken: Prototype slotNames
# = um zu einen bereits existierenden / gesetzten Slots zu setzen; ansonsten Fehler
# := um einen Slot, auch wenn er nicht existiert, zu setzen (=erstellen)
# ::= um einen Slot, auch wenn er nicht existiert, zu setzen (=erstellen) und einen Setter zu erstellen?
"Hallo Welt aus einer Datei" println
myObject := Object clone
myObject customField := method(
"Hallo Welt aus einer Methode!" println
)
myObject customField

141
io/day2.io Normal file
View File

@ -0,0 +1,141 @@
# Task 1:
fib := method(i,
if(i==0) then(return 0)
if(i==1) then(return 1)
return fib(i - 2) + fib(i - 1)
)
fib_loop := method(i,
r := 0
s := 0
t := 1
for(j, 2, i, 1, r := s; s := t; t := r + s)
return t
)
fib(1) println
fib(2) println
fib(3) println
fib(4) println
fib(5) println
fib(6) println
fib(7) println
fib(8) println
"=====================================" println
fib_loop(1) println
fib_loop(2) println
fib_loop(3) println
fib_loop(4) println
fib_loop(5) println
fib_loop(6) println
fib_loop(7) println
fib_loop(8) println
# Task 2:
Number old_div := Number getSlot("/")
Number / = method(divisor,
if(divisor == 0, 0, self old_div(divisor))
)
"Testing Task 2:" println
(6 / 0) println
(6 / 3) println
# Task 3:
"Task 3:" println
arr := list(list(1, 2, 3), list(4, 5, 6), list(7, 8, 9))
arr_sum := 0
for(i, 0, arr size - 1, 1, i println; arr_sum = arr_sum + arr at(i) sum)
arr_sum println
# Task 4:
"Task 4:" println
List myAverage := method(
self sum / self size
)
list(1, 2, 3, 4) myAverage println
# list() myAverage println
# Task 5, 6, 7:
"Task 5, 6, 7:" println
Matrix := Object clone
Matrix dim := method(x, y,
self content := list()
self y := y
self x := x
for(i, 1, y, 1,
self content push(list())
for(j, 1, x, 1,
self content at(i - 1) push(0)
)
)
)
Matrix set := method(x, y, value,
self content at(y) atPut(x, value)
)
Matrix get := method(x, y,
self content at(y) at(x)
)
Matrix writeToFile := method(filename,
file := File clone openForUpdating(filename)
for(i, 1, y, 1,
for(j, 1, x, 1,
file write(self content at(i - 1) at(j -1) asSimpleString, " ")
)
file write("\n")
)
)
Matrix readFromFile := method(filename,
file := File clone openForReading(filename)
lines := file readLines
x := lines first split size
y := lines size
self dim(x, y)
lines foreach(i, line, line split foreach(j, v, self content at(i) atPut(j, v)))
)
matrixOne := Matrix clone
matrixOne dim(2, 3)
matrixOne content println
matrixOne set(1,1, 20)
matrixOne get(1,1) println
matrixOne writeToFile("matrix.txt")
matrixTwo := Matrix clone
matrixTwo readFromFile("matrix.txt")
matrixTwo content println
# Task 8:
"Task 8:" println
lastGuess := 0
numberOfGuesses := 0
targetNumber := (Date asNumber * 70) round % 100
while(numberOfGuesses < 10,
input := File standardInput() readLine() asNumber()
if(input == targetNumber, "fertig!" println; break,
if(numberOfGuesses == 0, "nicht die richtige!" println,
if((targetNumber - input) abs < (targetNumber - lastGuess) abs, "hotter" println, "colder" println )
)
)
lastGuess = input
numberOfGuesses = numberOfGuesses + 1
)

3
io/matrix.txt Normal file
View File

@ -0,0 +1,3 @@
0 0
0 20
0 0

24
prolog/day1.pl Normal file
View File

@ -0,0 +1,24 @@
/**/
author(derfremde, camus).
author(nineteeneightyfour, orwell).
author(farmdertiere, orwell).
author(diepest, camus).
% B1 Mülltonne
uses(angriffstrupp, schlauch).
uses(angriffstrupp, strahlrohr).
uses(angriffstrupp, pressluftatmer).
uses(angriffstrupp, funkgeraet).
uses(angriffstrupp, lampe).
uses(wassertrupp, schlauch).
uses(wassertrupp, haspel).
uses(wassertrupp, standrohr).
uses(wassertrupp, hydrantenschluessel).
uses(wassertrupp, rueckflussminderer).
uses(wassertrupp, funkgeraet).
uses(wassertrupp, lampe).
performs(angriffstrupp, brandbekaempfung).
performs(angriffstrupp, menschenrettung).
performs(wassertrupp, wasserversorgung).
performs(wassertrupp, absperrung).

35
prolog/day2.pl Normal file
View File

@ -0,0 +1,35 @@
/**/
fib(0, [0]).
fib(1, [1,0]).
fib(N, [R, X, Y | Zs]) :-
N > 1,
N1 is N - 1,
fib(N1, [X, Y | Zs]),
R is X + Y.
% wrong naive impl
reverse([], []).
reverse([x], [x]).
reverse([Head|Tail], [RNew, Head]) :-
reverse(Tail, RNew).
accrev([H|T], Acc, R) :-
accrev(T, [H|Acc], R).
accrev([], R, R).
rev(L, R) :- accrev(L, [], R).
smol([S], S).
smol([H|T], S) :-
smol(T, Snew),
Snew < H -> S is Snew; S is H.
% not working
accsortt([], Acc, Acc).
accsortt([H1 | T], Acc, R) :-
H2 | T,
H1 < H2
-> accsortt(T, [H1, H2 | Acc], R);
accsortt(T, [H2, H1 | Acc], R).
sortt(L, R) :- accsortt(L, [], R).

107
prolog/day3.pl Normal file
View File

@ -0,0 +1,107 @@
/*
* sudoku([5, 3, _, _, 7, _, _, _, _,
* 6, _, _, 1, 9, 5, _, _, _,
* _, 9, 8, _, _, _, _, 6, _,
* 8, _, _, _, 6, _, _, _, 3,
* 4, _, _, 8, _, 3, _, _, 1,
* 7, _, _, _, 2, _, _, _, 6,
* _, 6, _, _, _, _, 2, 8, _,
* _, _, _, 4, 1, 9, _, _, 5,
* _, _, _, _, 8, _, _, 7, 9
* ], What).
* sudoku([5, 3, _, _, 7, _, _, _, _, 6, _, _, 1, 9, 5, _, _, _, _, 9, 8, _, _, _, _, 6, _, 8, _, _, _, 6, _, _, _, 3, 4, _, _, 8, _, 3, _, _, 1, 7, _, _, _, 2, _, _, _, 6, _, 6, _, _, _, _, 2, 8, _, _, _, _, 4, 1, 9, _, _, 5, _, _, _, _, 8, _, _, 7, 9 ], What).
* puzzle input from wikipedia
* */
:- use_module(library(clpfd)).
sudoku(Input, Solution) :-
Solution = Input,
Input = [ S11, S12, S13, S14, S15, S16, S17, S18, S19,
S21, S22, S23, S24, S25, S26, S27, S28, S29,
S31, S32, S33, S34, S35, S36, S37, S38, S39,
S41, S42, S43, S44, S45, S46, S47, S48, S49,
S51, S52, S53, S54, S55, S56, S57, S58, S59,
S61, S62, S63, S64, S65, S66, S67, S68, S69,
S71, S72, S73, S74, S75, S76, S77, S78, S79,
S81, S82, S83, S84, S85, S86, S87, S88, S89,
S91, S92, S93, S94, S95, S96, S97, S98, S99
],
Solution ins 1..9,
Row1 = [S11, S12, S13, S14, S15, S16, S17, S18, S19],
Row2 = [S21, S22, S23, S24, S25, S26, S27, S28, S29],
Row3 = [S31, S32, S33, S34, S35, S36, S37, S38, S39],
Row4 = [S41, S42, S43, S44, S45, S46, S47, S48, S49],
Row5 = [S51, S52, S53, S54, S55, S56, S57, S58, S59],
Row6 = [S61, S62, S63, S64, S65, S66, S67, S68, S69],
Row7 = [S71, S72, S73, S74, S75, S76, S77, S78, S79],
Row8 = [S81, S82, S83, S84, S85, S86, S87, S88, S89],
Row9 = [S91, S92, S93, S94, S95, S96, S97, S98, S99],
Rows = [Row1, Row2, Row3, Row4, Row5, Row6, Row7, Row8, Row9],
Col1 = [S11, S21, S31, S41, S51, S61, S71, S81, S91],
Col2 = [S12, S22, S32, S42, S52, S62, S72, S82, S92],
Col3 = [S13, S23, S33, S43, S53, S63, S73, S83, S93],
Col4 = [S14, S24, S34, S44, S54, S64, S74, S84, S94],
Col5 = [S15, S25, S35, S45, S55, S65, S75, S85, S95],
Col6 = [S16, S26, S36, S46, S56, S66, S76, S86, S96],
Col7 = [S17, S27, S37, S47, S57, S67, S77, S87, S97],
Col8 = [S18, S28, S38, S48, S58, S68, S78, S88, S98],
Col9 = [S19, S29, S39, S49, S59, S69, S79, S89, S99],
Cols = [Col1, Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9],
Sqr1 = [S11, S12, S13, S21, S22, S23, S31, S32, S33],
Sqr2 = [S14, S15, S16, S24, S25, S26, S34, S35, S36],
Sqr3 = [S17, S18, S19, S27, S28, S29, S37, S38, S39],
Sqr4 = [S41, S42, S43, S51, S52, S53, S61, S62, S63],
Sqr5 = [S44, S45, S46, S54, S55, S56, S64, S65, S66],
Sqr6 = [S47, S48, S49, S57, S58, S59, S67, S68, S69],
Sqr7 = [S71, S72, S73, S81, S82, S83, S91, S92, S93],
Sqr8 = [S74, S75, S76, S84, S85, S86, S94, S95, S96],
Sqr9 = [S77, S78, S79, S87, S88, S89, S97, S98, S99],
Squares = [Sqr1, Sqr2, Sqr3, Sqr4, Sqr5, Sqr6, Sqr7, Sqr8, Sqr9],
valid(Squares),
valid(Rows),
valid(Cols),
show(Solution).
sudoku4(Input, Solution) :-
Solution = Input,
Input = [ S11, S12, S13, S14,
S21, S22, S23, S24,
S31, S32, S33, S34,
S41, S42, S43, S44
],
Solution ins 1..4,
Squares = [[S11, S12, S21, S22], [S13, S14, S23, S24], [S31, S32, S41, S42], [S33, S34, S43, S44]],
Rows = [[S11, S12, S13, S14], [S21, S22, S23, S24], [S31, S32, S33, S34], [S41, S42, S43, S44]],
Cols = [[S11, S21, S31, S41], [S12, S22, S32, S42], [S13, S23, S33, S43], [S14, S24, S34, S44]],
valid(Squares),
valid(Rows),
valid(Cols).
valid([]).
valid([H|T]) :-
all_distinct(H),
valid(T).
show([S1, S2, S3, S4, S5, S6, S7, S8, S9 | T]) :-
write(S1),
write(" | "),
write(S2),
write(" | "),
write(S3),
write(" | "),
write(S4),
write(" | "),
write(S5),
write(" | "),
write(S6),
write(" | "),
write(S7),
write(" | "),
write(S8),
write(" | "),
write(S9),
nl,
show(T).

1
scala/corrections.txt Normal file
View File

@ -0,0 +1 @@
blub#gluck

56
scala/day1.scala Normal file
View File

@ -0,0 +1,56 @@
object TicTacToe {
val board = new Array[Char](9)
var mark = 'X'
def tick(horizontal: String, vertical: String) {
val x = horizontal match {
case "left" => 0
case "middle" => 1
case "right" => 2
case _ => 0
}
val y = vertical match {
case "up" => 0
case "middle" => 3
case "down" => 6
case _ => 0
}
val index = x + y
if (board(index) == 'X' || board(index) == 'O') {
println("Spot is already taken!")
return
}
board(index) = mark
isWinner('X')
isWinner('O')
mark = mark match {
case 'X' => 'O'
case 'O' => 'X'
case _ => 'c'
}
}
def isWinner(mark: Char) {
if ((board(0) == mark && board(1) == mark && board(2) == mark) ||
(board(3) == mark && board(4) == mark && board(5) == mark) ||
(board(6) == mark && board(7) == mark && board(8) == mark) ||
(board(0) == mark && board(3) == mark && board(6) == mark) ||
(board(1) == mark && board(4) == mark && board(7) == mark) ||
(board(2) == mark && board(5) == mark && board(8) == mark) ||
(board(0) == mark && board(4) == mark && board(8) == mark) ||
(board(2) == mark && board(4) == mark && board(6) == mark)) {
println("Gewinner: " + mark)
}
}
}
TicTacToe.tick("left", "up")
TicTacToe.tick("right", "up")
TicTacToe.tick("left", "middle")
TicTacToe.tick("middle", "middle")
TicTacToe.tick("left", "down")

44
scala/day2.scala Normal file
View File

@ -0,0 +1,44 @@
import scala.io.Source
def getTotalSize(strings: List[String]): Int = {
(0 /: strings) {(sum, string) => sum + string.size}
}
val list = List("123", "345", "ABN")
val bla = getTotalSize(list)
println(bla)
trait Censor {
var corrections = Map("Shoot" -> "Pucky", "Darn" -> "Beans")
def readCorrections() = {
val filename = "corrections.txt"
for (line <- Source.fromFile(filename).getLines) {
val splitted = line.split("#")
corrections = corrections + (splitted(0) -> splitted(1))
}
}
def correct(input: String): String = {
var output = input;
corrections.foreach((e: (String, String)) => output = e._1.r.replaceAllIn(output, e._2))
return output
}
}
class Corrector extends Censor {
def say(input: String) = {
val output = correct(input)
println(output)
}
}
val hering = new Corrector()
hering.readCorrections()
hering.say("I'm a fish, blub blub")
hering.say("Shoot Darn")

51
scala/day3.scala Normal file
View File

@ -0,0 +1,51 @@
// 2023 syntax of scala is too different to reproduce this example
import scala.io._
import scala.actors._
import Actor._
object PageLoader {
def getPageSize(url: String) = Source.fromURL(url).mkString.length
def getCountOfLinks(url: String): int = {
val raw = Source.fromURL(url).mkString
val count = "<a>".r.findAllIn(raw).length
return count
}
}
val urls = List("https://www.amazon.com/",
"https://www.twitter.com/",
"https://www.google.com/",
"https://www.cnn.com/")
def timeMethod(method: () => Unit) = {
val start = System.nanoTime
method()
val end = System.nanoTime
println("Method took " + (end - start)/1000000000.0 + " seconds.")
}
def getPageSizeSequentially() = {
for(url <- urls) {
println("Size for " + url + ": " + PageLoader.getPageSize(url))
}
}
def getPageSizeConcurrrently() = {
def caller = self
for(url <- urls) {
actor { caller ! (url, PageLoader.getPageSize(url)) }
}
for(i <- 1 to urls.size) {
receive {
case (url, size) =>
println("Size for " + url + ": " + size)
}
}
}