diff --git a/2021/README.md b/2021/README.md index 43c4966..445d0fb 100644 --- a/2021/README.md +++ b/2021/README.md @@ -4,12 +4,12 @@ | Day | Part 1 | Part 2 | Language | | :-: | :----: | :----: | :------- | -| 01 | ✓ | ✓ | Ruby | -| 02 | ✓ | ✓ | Ada | -| 03 | ✓ | ✓ | Perl | -| 04 | ✓ | ✓ | Go | -| 05 | ✓ | ✓ | Nim | -| 06 | | | | +| 01 | ✓ | ✓ | [Ruby] | +| 02 | ✓ | ✓ | [Ada] | +| 03 | ✓ | ✓ | [Perl] | +| 04 | ✓ | ✓ | [Go] | +| 05 | ✓ | ✓ | [Nim] | +| 06 | ✓ | ✓ | [Inko] | | 07 | | | | | 08 | | | | | 09 | | | | @@ -33,3 +33,12 @@ ## Additional Challenges - Attempt to solve each day in a different language. + + + +[ruby]: https://www.ruby-lang.org +[ada]: https://www.adacore.com/about-ada +[perl]: https://www.perl.org +[go]: https://go.dev +[nim]: https://nim-lang.org +[inko]: https://inko-lang.org diff --git a/2021/day-06/common.inko b/2021/day-06/common.inko new file mode 100644 index 0000000..428259f --- /dev/null +++ b/2021/day-06/common.inko @@ -0,0 +1,64 @@ +import std::fs::file::(ReadOnlyFile as File) +import std::stdio::stdout +import std::io::(Error as IoError) + +def read_file(path: String) !! IoError -> Array!(Integer) { + let file = try File.new(path) + let mut content = try file.read_string + content = content.slice(0, content.length - 1) + content.split(",").map do (c) { c.to_integer }.to_array +} + +def count_fish(initial: Array!(Integer), days: Integer) -> Integer { + let mut day = 0 + + let mut timers = Array.filled(9, 0) + initial.iter.each do (n) { timers[n] = timers[n] + 1 } + + while({day < days}) { + let mut new_timers = Array.filled(9, 0) + + timers.each_with_index do (timer, idx) { + new_timers[idx - 1] = new_timers[idx - 1] + timer + + (idx == 0).if_true { + (timer != 0).if_true { + new_timers[6] = new_timers[6] + timer + } + } + } + + timers = new_timers + + day += 1 + } + + timers.iter.reduce(0) do (acc, num) { acc + num } +} + +def count_fish_naive(initial: Array!(Integer), days: Integer) -> Integer { + let mut fish = initial + let mut day = 0 + + while({day < days}) { + let mut new_fish = Array.new() + + fish = fish + .iter + .map do (f) { + (f == 0).if(true: { + new_fish.push(8) + 6 + }, false: { + f - 1 + }) + } + .to_array + + fish.append(new_fish) + + day += 1 + } + + fish.length +} diff --git a/2021/day-06/inputs/puzzle.txt b/2021/day-06/inputs/puzzle.txt new file mode 100644 index 0000000..6b5d079 --- /dev/null +++ b/2021/day-06/inputs/puzzle.txt @@ -0,0 +1 @@ +3,5,3,1,4,4,5,5,2,1,4,3,5,1,3,5,3,2,4,3,5,3,1,1,2,1,4,5,3,1,4,5,4,3,3,4,3,1,1,2,2,4,1,1,4,3,4,4,2,4,3,1,5,1,2,3,2,4,4,1,1,1,3,3,5,1,4,5,5,2,5,3,3,1,1,2,3,3,3,1,4,1,5,1,5,3,3,1,5,3,4,3,1,4,1,1,1,2,1,2,3,2,2,4,3,5,5,4,5,3,1,4,4,2,4,4,5,1,5,3,3,5,5,4,4,1,3,2,3,1,2,4,5,3,3,5,4,1,1,5,2,5,1,5,5,4,1,1,1,1,5,3,3,4,4,2,2,1,5,1,1,1,4,4,2,2,2,2,2,5,5,2,4,4,4,1,2,5,4,5,2,5,4,3,1,1,5,4,5,3,2,3,4,1,4,1,1,3,5,1,2,5,1,1,1,5,1,1,4,2,3,4,1,3,3,2,3,1,1,4,4,3,2,1,2,1,4,2,5,4,2,5,3,2,3,3,4,1,3,5,5,1,3,4,5,1,1,3,1,2,1,1,1,1,5,1,1,2,1,4,5,2,1,5,4,2,2,5,5,1,5,1,2,1,5,2,4,3,2,3,1,1,1,2,3,1,4,3,1,2,3,2,1,3,3,2,1,2,5,2 diff --git a/2021/day-06/inputs/sample.txt b/2021/day-06/inputs/sample.txt new file mode 100644 index 0000000..55129f1 --- /dev/null +++ b/2021/day-06/inputs/sample.txt @@ -0,0 +1 @@ +3,4,3,1,2 diff --git a/2021/day-06/part_one.inko b/2021/day-06/part_one.inko new file mode 100644 index 0000000..6cd506e --- /dev/null +++ b/2021/day-06/part_one.inko @@ -0,0 +1,15 @@ +import std::stdio::stdout +import std::env +import std::error::Error +import common + +def main() !! Error { + let path = env.arguments[0] + let initial_fish = try common.read_file(path) + + let fish_count = common.count_fish(initial_fish, 80) + + stdout.print(fish_count) +} + +try! main diff --git a/2021/day-06/part_two.inko b/2021/day-06/part_two.inko new file mode 100644 index 0000000..0352d44 --- /dev/null +++ b/2021/day-06/part_two.inko @@ -0,0 +1,15 @@ +import std::stdio::stdout +import std::env +import std::error::Error +import common + +def main() !! Error { + let path = env.arguments[0] + let initial_fish = try common.read_file(path) + + let fish_count = common.count_fish(initial_fish, 256) + + stdout.print(fish_count) +} + +try! main diff --git a/README.md b/README.md index 370ad0c..a87990d 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,4 @@ - [2018](2018/README.md) (0% completed) - [2019](2019/README.md) (0% completed) - [2020](2020/README.md) (16% completed) -- [2021](2021/README.md) (20% completed) +- [2021](2021/README.md) (24% completed)