-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday06.nim
93 lines (80 loc) · 2.05 KB
/
day06.nim
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
86
87
88
89
90
91
92
93
import nimib, p5
nbInit
nbUseP5
nbCode:
import batteries
let example = """
toggle 461,550 through 564,900
turn off 370,39 through 425,839
turn off 464,858 through 833,915
turn off 812,389 through 865,874
turn on 599,989 through 806,993
turn on 376,415 through 768,548
turn on 606,361 through 892,600
turn off 448,208 through 645,684
toggle 50,472 through 452,788
toggle 205,417 through 703,826
"""
type
MoveKind = enum
mkToggle, mkOn, mkOff
Move = object
kind: MoveKind
x0, y0, x1, y1: int
proc parse(input: string, reduced = false): seq[Move] =
var m: Move
for line in input.splitLines:
if scanf(line, "toggle $i,$i through $i,$i", m.x0, m.y0, m.x1, m.y1):
m.kind = mkToggle
elif scanf(line, "turn on $i,$i through $i,$i", m.x0, m.y0, m.x1, m.y1):
m.kind = mkOn
elif scanf(line, "turn off $i,$i through $i,$i", m.x0, m.y0, m.x1, m.y1):
m.kind = mkOff
else:
echo "no match, line: ", line
continue
if reduced:
m.x0 = m.x0 div 10
m.y0 = m.y0 div 10
m.x1 = m.x1 div 10
m.y1 = m.y1 div 10
result.add m
echo parse(example, reduced=true)
nbCodeDisplay(nbP5Instance):
type
Light = object
x, y, r0, r: float
isOn: bool
proc initLight(x, y: float): Light =
result.x = x
result.y = y
result.r0 = 20.0
proc move(l: var Light) =
if l.isOn and l.r < l.r0:
l.r = l.r + 1
elif not l.isOn and l.r > 0:
l.r = l.r - 1
proc show(l: var Light) =
p5Inst.ellipse(l.x, l.y, l.r)
var l: Light
var t = 0
setup:
createCanvas(100, 100)
background(0)
fill(250)
l = initLight(50.0, 50.0)
draw:
l.move
l.show
#[ frameCount not yet wrapped in instance mode and adding `p5inst.` does not work (nim code does not compile)
if frameCount mod 100 == 50:
l.isOn = true
elif frameCount mod 100 == 0:
l.isOn = false
]#
if t mod 100 == 50:
l.isOn = true
elif t mod 100 == 0:
l.isOn = false
inc t
nbSave