-
Notifications
You must be signed in to change notification settings - Fork 0
/
2022 day 11.bas
159 lines (146 loc) · 3.81 KB
/
2022 day 11.bas
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
!advent of code 2022 day 11
!x11-basic on android
day$="11"
test=FALSE
if test
fn$="bas/day"+day$+"test.txt"
else
fn$="bas/day"+day$+"input.txt"
endif
cls
@parse_input(fn$)
print
print num_monkeys; " monkeys read in."
print "holding "; total_items; " items."
arraycopy monkey_items(), init_monkeys() ! keep the original items around to play with later
dim monkey_handled(num_monkeys)
part1=TRUE
relief=3
num_rounds = 20
do_the_monkey:
t0=timer
for monkey_round = 0 to num_rounds-1
if part1=FALSE and (monkey_round mod 100)=0
print ".";
endif
for monkey_turn = 0 to num_monkeys-1
@monkey_business(monkey_turn)
next monkey_turn
next monkey_round
print
print "After "; num_rounds; ", Monkeys are holding:"
for m=0 to num_monkeys-1
print "Monkey ";m using "##";": ";
itm=0
while monkey_items(m,itm)<> 0
print monkey_items(m,itm);", ";
inc itm
wend
print
next m
for m = 0 to num_monkeys-1
print "Monkey ";m; " handled "; monkey_handled(m); " times"
next m
print "product of 2 most active: "
sort monkey_handled()
print monkey_handled(num_monkeys-1) * monkey_handled(num_monkeys-2)
print "done in ";timer-t0 using "####.###";" seconds"
if part1=TRUE
print "setting up part 2"
!set up and run part 2
part1 = FALSE
arraycopy monkey_items(), init_monkeys()
clr monkey_handled()
num_rounds=10000
relief=1
for t=0 to num_monkeys-1
mul relief,monkey_div_test(t)
next t
goto do_the_monkey
endif
end
procedure monkey_business(monkey)
local item,new,old,to_monkey
item=0
while monkey_items(monkey,item) <> 0
!inspect item (do operation)
new = 0
old = monkey_items(monkey,item)
eval monkey_op$(monkey) ! thank god this is an option
!relief (worry for item is div by 3)
if part1=TRUE
new = new div 3 ! integer floor division
else
new = new mod relief
endif
!test worry level
if (new mod monkey_div_test(monkey))=0
!throw item to true
to_monkey=monkey_true_act(monkey)
else
!throw item to false
to_monkey=monkey_false_act(monkey)
endif
!throw
monkey_items(monkey, item)=0
@monkey_catch(to_monkey, new)
inc item
inc monkey_handled(monkey)
wend
return
procedure monkey_catch(new_monkey, worry)
local i
i=0
if monkey_items(new_monkey,0) = 0
monkey_items(new_monkey,0)=worry
return
else
while monkey_items(new_monkey, i) <> 0
inc i
wend
monkey_items(new_monkey,i)=worry
endif
return
procedure parse_input(filename$)
local l$,c$,item_list$
open "I",#1,fn$
print "reading input file"
while not eof(#1)
!print ".";
l$=trim$(lineinput$(#1))
c$=left$(l$,1)
if c$ = "M"
! first get the number of monkeys
num_monkeys=val(rightof$(l$, " "))+1 ! automatically reads to the first non-number char
else if c$ = "S"
!get the number of items
num_items = tally(l$, ",")+1
add total_items, num_items
dim init_monkeys(num_monkeys, total_items)
item_list$=rightof$(l$, ":")
print "monkey ";num_monkeys-1 using "##";": ";
for i = 0 to num_items-1
split item_list$, ",", 0, item$, item_list$
item_num=val(item$)
init_monkeys(num_monkeys-1, i)=item_num
print item_num;", ";
next i
print
else if c$ = "O"
! process operation
! just take everything after ":" and eval later?
dim monkey_op$(num_monkeys)
monkey_op$(num_monkeys-1) = trim$(rightof$(l$, ":"))
else if c$ = "T"
dim monkey_div_test(num_monkeys)
monkey_div_test(num_monkeys-1)=val(word$(l$,4))
else if mid$(l$,4,1) = "t"
dim monkey_true_act(num_monkeys)
monkey_true_act(num_monkeys-1)=val(word$(l$,6))
else if mid$(l$,4,1) = "f"
dim monkey_false_act(num_monkeys)
monkey_false_act(num_monkeys-1)=val(word$(l$,6))
endif
wend
close #1
return