added next to days
This commit is contained in:
parent
2785fe4c1d
commit
6c2ec8b826
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.swp
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
1
08/input.txt
Normal file
1
08/input.txt
Normal file
File diff suppressed because one or more lines are too long
2
08/input_sample.txt
Normal file
2
08/input_sample.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
2 3 0 3 10 11 12 1 1 0 1 99 2 1 1 2
|
||||||
|
|
26
08/solve01.py
Executable file
26
08/solve01.py
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
def read_input():
|
||||||
|
with open('input.txt', 'r') as f:
|
||||||
|
data = f.read().splitlines()
|
||||||
|
return [ int(x) for x in data[0].split(" ")]
|
||||||
|
|
||||||
|
|
||||||
|
def get_node_meta(data, pos):
|
||||||
|
num_ch = data[pos]
|
||||||
|
num_m = data[pos+1]
|
||||||
|
len_ch = 0 # total length of children
|
||||||
|
meta = []
|
||||||
|
|
||||||
|
for ch in range(num_ch):
|
||||||
|
(ch_len, ch_meta) = get_node_meta(data, pos+2+len_ch)
|
||||||
|
len_ch += ch_len
|
||||||
|
meta = meta + ch_meta
|
||||||
|
|
||||||
|
meta = meta + data[pos+2+len_ch:pos+2+len_ch+num_m]
|
||||||
|
return (len_ch+2+num_m, meta)
|
||||||
|
|
||||||
|
tree = read_input()
|
||||||
|
(_, meta) = get_node_meta(tree, 0)
|
||||||
|
print(sum(meta))
|
35
08/solve02.py
Executable file
35
08/solve02.py
Executable file
@ -0,0 +1,35 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
|
||||||
|
def read_input():
|
||||||
|
with open('input.txt', 'r') as f:
|
||||||
|
data = f.read().splitlines()
|
||||||
|
return [ int(x) for x in data[0].split(" ")]
|
||||||
|
|
||||||
|
|
||||||
|
def get_node_meta(data, pos):
|
||||||
|
num_ch = data[pos]
|
||||||
|
num_m = data[pos+1]
|
||||||
|
len_ch = 0 # total length of children
|
||||||
|
values = [0] * num_ch
|
||||||
|
value = 0
|
||||||
|
|
||||||
|
for ch in range(num_ch):
|
||||||
|
(ch_len, ch_value) = get_node_meta(data, pos+2+len_ch)
|
||||||
|
len_ch += ch_len
|
||||||
|
values[ch] = ch_value
|
||||||
|
|
||||||
|
meta = data[pos+2+len_ch:pos+2+len_ch+num_m]
|
||||||
|
|
||||||
|
# do not have children
|
||||||
|
if num_ch == 0:
|
||||||
|
value = sum(meta)
|
||||||
|
else:
|
||||||
|
for m in meta:
|
||||||
|
if m <= num_ch:
|
||||||
|
value += values[m-1]
|
||||||
|
return (len_ch+2+num_m, value)
|
||||||
|
|
||||||
|
tree = read_input()
|
||||||
|
(_, value) = get_node_meta(tree, 0)
|
||||||
|
print(value)
|
75
09/solve01.py
Executable file
75
09/solve01.py
Executable file
@ -0,0 +1,75 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
|
def play(players, count):
|
||||||
|
marbles = dict()
|
||||||
|
marble_current = None
|
||||||
|
score = [0] * players
|
||||||
|
|
||||||
|
def add_marble(num):
|
||||||
|
nonlocal marbles
|
||||||
|
nonlocal marble_current
|
||||||
|
|
||||||
|
item = {
|
||||||
|
'number': num,
|
||||||
|
'prev': None,
|
||||||
|
'next': None
|
||||||
|
}
|
||||||
|
marbles[num] = item
|
||||||
|
if len(marbles) == 1:
|
||||||
|
item['prev'] = num
|
||||||
|
item['next'] = num
|
||||||
|
else:
|
||||||
|
j1 = marbles[marble_current]['next']
|
||||||
|
j2 = marbles[j1]['next']
|
||||||
|
item['prev'] = j1
|
||||||
|
item['next'] = j2
|
||||||
|
marbles[j1]['next'] = num
|
||||||
|
marbles[j2]['prev'] = num
|
||||||
|
|
||||||
|
marble_current = num
|
||||||
|
# print('adding marble {} next to {} and {}'.format(num, item['prev'], item['next']))
|
||||||
|
|
||||||
|
def rule_23(round):
|
||||||
|
nonlocal players
|
||||||
|
nonlocal marbles
|
||||||
|
nonlocal score
|
||||||
|
nonlocal marble_current
|
||||||
|
|
||||||
|
j1 = marble_current
|
||||||
|
for i in range(7):
|
||||||
|
j1 = marbles[j1]['prev']
|
||||||
|
|
||||||
|
marble_current = marbles[j1]['next']
|
||||||
|
j1p = marbles[j1]['prev']
|
||||||
|
j1n = marbles[j1]['next']
|
||||||
|
|
||||||
|
marbles[j1p]['next'] = j1n
|
||||||
|
marbles[j1n]['pred'] = j1p
|
||||||
|
|
||||||
|
player = round % players
|
||||||
|
score[player] += j1
|
||||||
|
score[player] += round
|
||||||
|
|
||||||
|
# print('removed marble {} between {} and {}'.format(j1,j1p,j1n))
|
||||||
|
# print('added {} and {} to player {}'.format(j1,round, player))
|
||||||
|
|
||||||
|
for r in range(count):
|
||||||
|
if (r % 23 == 0) and (r > 0):
|
||||||
|
rule_23(r)
|
||||||
|
else:
|
||||||
|
add_marble(r)
|
||||||
|
|
||||||
|
print(max(score))
|
||||||
|
|
||||||
|
#play(10, 26)
|
||||||
|
#play(10, 1619)
|
||||||
|
#play(13, 8000)
|
||||||
|
#play(17, 1105)
|
||||||
|
#play(21, 6112)
|
||||||
|
#play(30, 5808)
|
||||||
|
play(486, 70834)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
66
09/solve02.py
Executable file
66
09/solve02.py
Executable file
@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
|
def play(players, count):
|
||||||
|
marbles = dict()
|
||||||
|
marble_current = None
|
||||||
|
score = [0] * players
|
||||||
|
|
||||||
|
def add_marble(num):
|
||||||
|
nonlocal marbles
|
||||||
|
nonlocal marble_current
|
||||||
|
|
||||||
|
item = {
|
||||||
|
'number': num,
|
||||||
|
'prev': None,
|
||||||
|
'next': None
|
||||||
|
}
|
||||||
|
marbles[num] = item
|
||||||
|
if len(marbles) == 1:
|
||||||
|
item['prev'] = num
|
||||||
|
item['next'] = num
|
||||||
|
else:
|
||||||
|
j1 = marbles[marble_current]['next']
|
||||||
|
j2 = marbles[j1]['next']
|
||||||
|
item['prev'] = j1
|
||||||
|
item['next'] = j2
|
||||||
|
marbles[j1]['next'] = num
|
||||||
|
marbles[j2]['prev'] = num
|
||||||
|
|
||||||
|
marble_current = num
|
||||||
|
# print('adding marble {} next to {} and {}'.format(num, item['prev'], item['next']))
|
||||||
|
|
||||||
|
def rule_23(round):
|
||||||
|
nonlocal players
|
||||||
|
nonlocal marbles
|
||||||
|
nonlocal score
|
||||||
|
nonlocal marble_current
|
||||||
|
|
||||||
|
j1 = marble_current
|
||||||
|
for i in range(7):
|
||||||
|
j1 = marbles[j1]['prev']
|
||||||
|
|
||||||
|
marble_current = marbles[j1]['next']
|
||||||
|
j1p = marbles[j1]['prev']
|
||||||
|
j1n = marbles[j1]['next']
|
||||||
|
|
||||||
|
marbles[j1p]['next'] = j1n
|
||||||
|
marbles[j1n]['pred'] = j1p
|
||||||
|
|
||||||
|
player = round % players
|
||||||
|
score[player] += j1
|
||||||
|
score[player] += round
|
||||||
|
|
||||||
|
# print('removed marble {} between {} and {}'.format(j1,j1p,j1n))
|
||||||
|
# print('added {} and {} to player {}'.format(j1,round, player))
|
||||||
|
|
||||||
|
for r in range(count):
|
||||||
|
if (r % 23 == 0) and (r > 0):
|
||||||
|
rule_23(r)
|
||||||
|
else:
|
||||||
|
add_marble(r)
|
||||||
|
|
||||||
|
print(max(score))
|
||||||
|
|
||||||
|
play(486, 7083300)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user