-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4.jl
116 lines (81 loc) · 3.09 KB
/
day4.jl
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
using DataFrames
## Part A
## Read file
cd("/Users/patricklauer/Documents/GitHub/advent_of_code_2023/")
lines = readlines("day4_input.txt")
## create empty data frame
cards = DataFrame(card = [], winning_numbers = [], your_numbers = [])
## store information in data frame
for i in 1:length(lines)
card, numbers = split(lines[i], ":") #split cards and numbers
winning_numbers, your_numbers = split(numbers, "|") # split winning and your numbers
## remove whitespaces and transform to Integer values
winning_numbers = split(winning_numbers, ' ')
winning_numbers = parse.(Int64, filter(x -> x != "", winning_numbers))
your_numbers = split(your_numbers, ' ')
your_numbers = parse.(Int64, filter(x -> x != "", your_numbers))
## safe in data frame
temp = DataFrame(card = card, winning_numbers = [winning_numbers], your_numbers = [your_numbers])
cards = [cards; temp]
end
## create new columns containing the number of wins and the respective points
## use 0 as baseline value
cards.wins = zeros(Int64, length(cards.card))
cards.points = zeros(Int64, length(cards.card))
## loop through cards
for i in 1: length(cards.card)
wins = 0 # reset wins after each card
for j in 1: length(cards.winning_numbers[i])
if cards.winning_numbers[i][j] in cards.your_numbers[i]
wins = wins + 1 # if the winning number is in your numbers add a win
end
end
cards.wins[i] = wins
if wins != 0
cards.points[i] = 2^(wins - 1) # safe points, since 1 win is one point and then doubles, it follows the exponential
else
cards.points[i] = 0
end
end
println(sum(cards.points))
## Part B
## create a dictionary containing card win information
wins = cards.wins
card = collect(1:nrow(cards))
card_win = Dict(card .=> wins)
## create a vector that counts the number of cards
## initiated with 1 copy per card
n_cards = ones(Int, length(card))
## add copies to card
for i in 1:(length(card)-1)
n_win = card_win[i]
n_cards[i+1:i+n_win] .+= n_cards[i]
end
sum(n_cards)
## First version
## DOES NOT WORK: Just keeps on evaluating, worked on simplified example (probably to slow to iterate and fullcopy all the cards)
cardsB = [card wins]
## Initiate
i = 1
max_iteration = length(card)
while i <= max_iteration
wins = cardsB[i, 2] # the wins are stored at the 2nd index of the matrix
for j in 0:wins
if j == 0 # dont duplicate any cards if no wins
continue
end
## which card is copied (depends on the initial card i (stored in 1st index of matrix) and the iteration of j)
copy_card = cardsB[i, 1] + j
if copy_card >= maximum(card) + 1 # stop the copying process at max copyable card (my input contains 197 cards)
continue
end
## How many wins does the card to be copyed contain
copy_wins = cardsB[copy_card,2]
## add copied card to matrix
copy = [copy_card copy_wins]
cardsB = vcat(cardsB, copy)
end
i += 1
max_iteration = length(cardsB[:,1])
end
length(cardsB[:,1])