File tree Expand file tree Collapse file tree 2 files changed +148
-0
lines changed Expand file tree Collapse file tree 2 files changed +148
-0
lines changed Original file line number Diff line number Diff line change
1
+ def match_rules (update , rules ):
2
+ for start , end in rules :
3
+ if start in update and end in update :
4
+ if update .index (start ) > update .index (end ):
5
+ return False
6
+ return True
7
+
8
+
9
+ def solution (lines : list [str ]):
10
+ ret = 0
11
+ section = 0
12
+ rules = []
13
+ updates = []
14
+ for line in lines :
15
+ if line == '' :
16
+ section += 1
17
+ continue
18
+ if section == 0 :
19
+ rules .append (tuple (line .split ('|' )))
20
+ elif section == 1 :
21
+ updates .append (line .split (',' ))
22
+
23
+ print (rules )
24
+ print (updates )
25
+ for update in updates :
26
+ if match_rules (update , rules ):
27
+ ret += int (update [(len (update ) - 1 ) // 2 ])
28
+ ...
29
+ return ret
30
+
31
+
32
+ data = '''47|53
33
+ 97|13
34
+ 97|61
35
+ 97|47
36
+ 75|29
37
+ 61|13
38
+ 75|53
39
+ 29|13
40
+ 97|29
41
+ 53|29
42
+ 61|53
43
+ 97|53
44
+ 61|29
45
+ 47|13
46
+ 75|47
47
+ 97|75
48
+ 47|61
49
+ 75|61
50
+ 47|29
51
+ 75|13
52
+ 53|13
53
+
54
+ 75,47,61,53,29
55
+ 97,61,53,29,13
56
+ 75,29,13
57
+ 75,97,47,61,53
58
+ 61,13,29
59
+ 97,13,75,29,47
60
+ ''' .split ('\n ' )
61
+
62
+ # data = open('input.txt', 'r').read().split('\n')
63
+
64
+ data = [datum for datum in data ][:- 1 ]
65
+
66
+ print (solution (data ))
Original file line number Diff line number Diff line change
1
+ from graphlib import TopologicalSorter
2
+ import pyperclip as pc
3
+
4
+
5
+ def match_rules (update , rules ):
6
+ for start , end in rules :
7
+ if start in update and end in update :
8
+ if update .index (start ) > update .index (end ):
9
+ return False
10
+ return True
11
+
12
+ def reorder (update , rules ):
13
+ graph = {}
14
+ for start , end in rules :
15
+ if start not in update :
16
+ continue
17
+ if start not in graph :
18
+ graph [start ] = set ()
19
+ graph [start ].add (end )
20
+ topo_sorted = list (TopologicalSorter (graph ).static_order ())[::- 1 ]
21
+ mp = {n : i for i , n in enumerate (topo_sorted )}
22
+ return list (sorted (update , key = lambda n : mp [n ]))
23
+
24
+
25
+ def solution (lines : list [str ]):
26
+ ret = 0
27
+ rules = []
28
+ updates = []
29
+ for line in lines :
30
+ if '|' in line :
31
+ rules .append (tuple (line .split ('|' )))
32
+ elif ',' in line :
33
+ updates .append (line .split (',' ))
34
+
35
+ bad_updates = []
36
+ for update in updates :
37
+ if not match_rules (update , rules ):
38
+ bad_updates .append (update )
39
+
40
+ reordered_updates = []
41
+ for update in bad_updates :
42
+ reordered_update = reorder (update , rules )
43
+ reordered_updates .append (reordered_update )
44
+ ret += int (reordered_update [(len (reordered_update )- 1 ) // 2 ])
45
+ return ret
46
+
47
+
48
+ # data = '''47|53
49
+ # 97|13
50
+ # 97|61
51
+ # 97|47
52
+ # 75|29
53
+ # 61|13
54
+ # 75|53
55
+ # 29|13
56
+ # 97|29
57
+ # 53|29
58
+ # 61|53
59
+ # 97|53
60
+ # 61|29
61
+ # 47|13
62
+ # 75|47
63
+ # 97|75
64
+ # 47|61
65
+ # 75|61
66
+ # 47|29
67
+ # 75|13
68
+ # 53|13
69
+ #
70
+ # 75,47,61,53,29
71
+ # 97,61,53,29,13
72
+ # 75,29,13
73
+ # 75,97,47,61,53
74
+ # 61,13,29
75
+ # 97,13,75,29,47
76
+ # '''.split('\n')
77
+
78
+ data = open ('../day5/input.txt' , 'r' ).read ().split ('\n ' )
79
+
80
+ s = solution (data )
81
+ print (s )
82
+ pc .copy (s )
You can’t perform that action at this time.
0 commit comments