Add solution for 2021 day 06
This commit is contained in:
parent
5ae2143fab
commit
5646a5448b
@ -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.
|
||||
|
||||
<!-- links -->
|
||||
|
||||
[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
|
||||
|
64
2021/day-06/common.inko
Normal file
64
2021/day-06/common.inko
Normal file
@ -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
|
||||
}
|
1
2021/day-06/inputs/puzzle.txt
Normal file
1
2021/day-06/inputs/puzzle.txt
Normal file
@ -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
|
1
2021/day-06/inputs/sample.txt
Normal file
1
2021/day-06/inputs/sample.txt
Normal file
@ -0,0 +1 @@
|
||||
3,4,3,1,2
|
15
2021/day-06/part_one.inko
Normal file
15
2021/day-06/part_one.inko
Normal file
@ -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
|
15
2021/day-06/part_two.inko
Normal file
15
2021/day-06/part_two.inko
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user