-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.rhm
65 lines (52 loc) · 1.64 KB
/
06.rhm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#lang rhombus
// Rhombus port of Brian Adkins solution
// <https://github.com/lojic/LearningRacket/blob/master/advent-of-code-2021/solutions/day06/day06.rkt>
import:
#{advent-of-code} as aoc:
rename:
#{open-aoc-input} as puzzle_input
#{find-session} as find_session
racket/base:
rename:
#{zero?} as is_zero
#{make-vector} as make_vector
#{string->number} as string_to_number
#{vector-length} as vector_length
racket/port:
rename:
#{port->string} as to_string
racket/string:
rename:
#{string-split} as split
#{string-trim} as trim
rhombus/compat/extra open
fun vector_update(vec, i, f):
vec[i] := f(vec[i])
fun iterate(f, seed, count):
if base.is_zero(count)
| seed
| iterate(f, f(seed), count - 1)
fun vector_sum(vec):
fun step(i, sum):
if base.is_zero(i)
| sum + vec[i]
| step(i - 1, sum + vec[i])
step(base.vector_length(vec) - 1, 0)
// `|>` operator is (currently) not as fancy as the threading macro, which is
// why there is some explicit grouping and literal funs.
val fish:
aoc.puzzle_input(aoc.find_session(), 2021, 6, ~cache: #true)
|> port.to_string
|> string.trim
|> (fun(s): string.split(s, ","))
|> (fun(ss): base.map(base.string_to_number, ss))
|> (fun(ns):
base.foldl(fun(n, vec): vector_update(vec, n, base.add1); vec,
base.make_vector(9, 0),
ns))
fun solve(fish, n):
fun spawn(fish):
Array(fish[1], fish[2], fish[3], fish[4], fish[5],
fish[6], fish[0] + fish[7], fish[8], fish[0])
vector_sum(iterate(spawn, fish, n))
solve(fish, 256)