-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.red
86 lines (76 loc) · 3.59 KB
/
day2.red
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Red [
title: "Advent of Code 2023 - Day 2: Cube Conundrum"
file: %day2.red
author: "Christian Ensel"
]
; ██████ █████ ██ ██ ██████ ██████ ██ ██ ██████ ███████ ██████ ██████ ███ ██ ██ ██ ███ ██ ██████ ██████ ██ ██ ███ ███
; ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ████ ████
; ██ ██ ███████ ████ █████ ██ ██ ██ ██████ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██████ ██ ██ ██ ████ ██
; ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
; ██████ ██ ██ ██ ███████ ██████ ██████ ██████ ███████ ██████ ██████ ██ ████ ██████ ██ ████ ██████ ██ ██ ██████ ██ ██
day2: context [
set 'sum-of function [values [block!]] [ ;-- poor man's SUM-OF
sum: 0 parse values [any [set val integer! (sum: sum + val) | skip]] sum
]
set 'map function [function [any-function!] values [any-block!]] [ ;-- poor man's MAP
collect [foreach value values [keep function value]]
]
space: [any " "]
colon: [space ":" space]
comma: [space "," space]
semicolon: [space ";" space]
digit: charset "1234567890"
set 'bagged-games? function [games [string!] bag [block!]] [
parse games [collect [any "^/"
some [
"Game" space copy game some digit
( game: load game)
colon
some [
some [
copy count some digit space
( count: load count)
copy color ["red" | "green" | "blue"]
( color: load color
if count > bag/:color [game: none]
)
opt comma
]
opt semicolon
]
some "^/"
keep (game)
]
]
]]
set 'power-of-bag func [bag [block!]] [
(any [bag/red 0]) * (any [bag/green 0]) * (any [bag/blue 0])
]
set 'minimal-bags? function [games [string!]] [
parse games [collect [any "^/"
some [
"Game" space copy game some digit
( game: load game
bag: copy [red 0 green 0 blue 0]
)
colon
some [
some [
copy count some digit space
( count: load count)
copy color ["red" | "green" | "blue"]
( color: load color
if count > bag/:color [bag/:color: count]
)
opt comma
]
opt semicolon
]
[some "^/" | end]
keep (bag)
]
]
]]
]
probe sum-of bagged-games? games: read %day2.dat [red 12 green 13 blue 14] ;-- 2406
probe sum-of map :power-of-bag minimal-bags? games ;-- 78375