File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # -*- coding: utf-8 -*-
3+
4+ from itertools import combinations
5+
6+ def parse (data ):
7+ lines = []
8+ for line in data .strip ().split ('\n ' ):
9+ output = line .split ()
10+ length = len (output [0 ]) - 2
11+ target = int (output [0 ][1 :- 1 ].replace ('#' , '1' ).replace ('.' , '0' ),2 )
12+ joltage = [int (x ) for x in output [- 1 ][1 :- 1 ].split (',' )]
13+ buttons = output [1 :- 1 ]
14+ nButtons = []
15+ for button in buttons :
16+ x = 0
17+ for i in button [1 :- 1 ].split (',' ):
18+ x |= (1 << (length - int (i ) - 1 ))
19+ nButtons .append (x )
20+
21+ lines .append ([length , target , nButtons , joltage ])
22+
23+ return lines
24+
25+ split_data = parse
26+ completed = 1
27+ raw_data = None # Not To be touched
28+
29+
30+ def part1 (data ):
31+ # This particular problem is awfully like a XOR subset finding problem.
32+ # We are going to take advantage of this fact to optimize the code and find the answers fast
33+ total = 0
34+ for line in data :
35+ length , target , nButtons , _ = line
36+ for i in range (1 , length ):
37+ # Now we get the subset of that length
38+ for comb in combinations (nButtons , r = i ):
39+ # Now we XOR all of them.
40+ res = 0
41+ for num in comb :
42+ res ^= num
43+ if res == target :
44+ break
45+ else :
46+ continue
47+ total += i
48+ break
49+
50+ return total
51+
52+
53+ def part2 (data ):
54+ # This is a simple row reduction problem. Then a subspace exploration
55+ ...
56+
You can’t perform that action at this time.
0 commit comments