-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4-2.mar
49 lines (41 loc) · 1.29 KB
/
day4-2.mar
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
import ../stdlib.mar
fun card_matches(card: Slice[Char]): Int {
var prefix_and_turns = card.split(": ")
var all_numbers = prefix_and_turns.get(1).split(" | ")
| TODO: use hash set when implemented
var winning_numbers = list[Int]()
for num in all_numbers.get(0).split(" ") do
if not(num.is_empty()) then winning_numbers.&.push(num.parse_Int())
var my_numbers = list[Int]()
for num in all_numbers.get(1).split(" ") do
if not(num.is_empty()) then my_numbers.&.push(num.parse_Int())
println(winning_numbers)
var matches = 0
for num in my_numbers do {
var wins = false()
for winning in winning_numbers do
if num.equals(winning) then wins = true()
if wins then matches = matches.add(1)
}
matches
}
fun main(): Never {
var input = read_file("advent/day4.big").unwrap().to_string()
var num_cards = list[Int]()
for i in 0.to(input.lines().len) do num_cards.&.push(1)
var i = 0
for line in input.lines() do {
var amount_i = num_cards.get(i)
var matches = line.card_matches()
for j in i.add(1).until(i.add(matches)) do {
var amount_j = num_cards.get_ref(j)
amount_j.* = amount_j.*.add(amount_i)
}
i = i.add(1)
}
var total = 0
for amount in num_cards do total = total.add(amount)
println(num_cards)
println(total)
exit(0)
}