-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
240a24f
commit 97da460
Showing
9 changed files
with
420 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Solution for day 01 of advent of code 2024 | ||
// https://adventofcode.com/2023/day/1 | ||
// | ||
// Run it as: | ||
// aoc-lang examples/aoc_day_01.aoc < input | ||
|
||
// Read input | ||
data = [] | ||
for (line = input(); line; line = input()) { | ||
push(data, line) | ||
} | ||
|
||
// Part one | ||
res = 0 | ||
for (i = 0; i < len(data); i = i + 1) { | ||
chars = split(data[i], "") | ||
n = 0 | ||
|
||
// First number | ||
for (j = 0; j < len(chars); j = j + 1) { | ||
if (int(chars[j])) { | ||
n = int(chars[j]) | ||
break | ||
} | ||
} | ||
|
||
// Last number | ||
for (j = len(chars) - 1; j >= 0; j = j - 1) { | ||
if (int(chars[j])) { | ||
n = n * 10 + int(chars[j]) | ||
break | ||
} | ||
} | ||
|
||
res = res + n | ||
} | ||
|
||
print("Part one: " + str(res)) | ||
|
||
// Part two | ||
substr_is = fn(target_ch, position, lookup_ch) { | ||
if (position + len(lookup_ch) > len(target_ch)) { | ||
return false | ||
} | ||
|
||
for (i = 0; i < len(lookup_ch); i = i + 1) { | ||
if (target_ch[position + i] != lookup_ch[i]) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
digits = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"] | ||
digit = fn(chars, position) { | ||
if (int(chars[position])) { | ||
return int(chars[position]) | ||
} | ||
|
||
for (d = 0; d < len(digits); d = d + 1) { | ||
if (substr_is(chars, position, split(digits[d], ""))) { | ||
return d + 1 | ||
} | ||
} | ||
} | ||
|
||
|
||
res = 0 | ||
for (i = 0; i < len(data); i = i + 1) { | ||
chars = split(data[i], "") | ||
n = 0 | ||
|
||
// First number | ||
for (j = 0; j < len(chars); j = j + 1) { | ||
d = digit(chars, j) | ||
if (d) { | ||
n = d | ||
break | ||
} | ||
|
||
} | ||
|
||
// Last number | ||
for (j = len(chars) - 1; j >= 0; j = j - 1) { | ||
d = digit(chars, j) | ||
if (d) { | ||
n = n * 10 + d | ||
break | ||
} | ||
} | ||
|
||
res = res + n | ||
} | ||
|
||
print("Part two: " + str(res)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Recursive fibonacci function implementation, | ||
// which also uses a for loop to print the first | ||
// 10 number in the sequence. | ||
|
||
fib = fn(n) { | ||
if (n <= 2) { | ||
return 1 | ||
} else { | ||
fib(n-1) + fib(n-2) | ||
} | ||
} | ||
|
||
for (i = 1; i <= 10; i = i + 1) { | ||
print(fib(i)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
print("What is your name?") | ||
name = input() | ||
print("Hello " + name + "!") |
Oops, something went wrong.