File tree Expand file tree Collapse file tree 1 file changed +106
-0
lines changed
Expand file tree Collapse file tree 1 file changed +106
-0
lines changed Original file line number Diff line number Diff line change 1+ require_relative 'day'
2+
3+ class Day24 < Day # >
4+
5+ # @example
6+ # day.part1 #=> 4
7+ # @example larger
8+ # day = Day24.new(EXAMPLE_LARGER)
9+ # day.part1 #=> 2024
10+ def part1
11+ known_wires = { }
12+ to_be_determined = { }
13+ input
14+ . split ( "\n " )
15+ . each do |line |
16+ case line
17+ when /\: /
18+ wire , signal = line . split ( ": " )
19+ known_wires [ wire ] = signal . to_i
20+ when /-\> /
21+ input1 , gate , input2 , wire = line . scan ( /\w +/ )
22+ to_be_determined [ wire ] = { inputs : [ input1 , input2 ] , gate : gate }
23+ end
24+ end
25+
26+ while to_be_determined . keys . any? { |wire | wire . start_with? ( 'z' ) } do
27+ puts to_be_determined . keys . inspect if ENV [ 'DEBUG' ]
28+ to_be_determined
29+ . select { |wire , data | data [ :inputs ] . all? { |input | known_wires . keys . include? ( input ) } }
30+ . each do |wire , data |
31+ known_wires [ wire ] =
32+ case data [ :gate ]
33+ when 'AND' ; known_wires [ data [ :inputs ] [ 0 ] ] & known_wires [ data [ :inputs ] [ 1 ] ]
34+ when 'OR' ; known_wires [ data [ :inputs ] [ 0 ] ] | known_wires [ data [ :inputs ] [ 1 ] ]
35+ when 'XOR' ; known_wires [ data [ :inputs ] [ 0 ] ] ^ known_wires [ data [ :inputs ] [ 1 ] ]
36+ else ; raise "lolwut: unknown gate #{ data [ :gate ] } "
37+ end
38+ to_be_determined . delete ( wire )
39+ end
40+ end
41+
42+ known_wires
43+ . select { |wire , signal | wire . start_with? ( 'z' ) }
44+ . sort_by { |wire , signal | wire }
45+ . reverse
46+ . map { |wire , signal | signal . to_s }
47+ . join
48+ . to_i ( 2 )
49+ end
50+
51+ # @example
52+ # day.part2 #=> 'how are you'
53+ def part2
54+ end
55+
56+ EXAMPLE_INPUT = File . read ( "../inputs/day24-example-input.txt" )
57+ EXAMPLE_LARGER = <<~LARGER
58+ x00: 1
59+ x01: 0
60+ x02: 1
61+ x03: 1
62+ x04: 0
63+ y00: 1
64+ y01: 1
65+ y02: 1
66+ y03: 1
67+ y04: 1
68+
69+ ntg XOR fgs -> mjb
70+ y02 OR x01 -> tnw
71+ kwq OR kpj -> z05
72+ x00 OR x03 -> fst
73+ tgd XOR rvg -> z01
74+ vdt OR tnw -> bfw
75+ bfw AND frj -> z10
76+ ffh OR nrd -> bqk
77+ y00 AND y03 -> djm
78+ y03 OR y00 -> psh
79+ bqk OR frj -> z08
80+ tnw OR fst -> frj
81+ gnj AND tgd -> z11
82+ bfw XOR mjb -> z00
83+ x03 OR x00 -> vdt
84+ gnj AND wpb -> z02
85+ x04 AND y00 -> kjc
86+ djm OR pbm -> qhw
87+ nrd AND vdt -> hwm
88+ kjc AND fst -> rvg
89+ y04 OR y02 -> fgs
90+ y01 AND x02 -> pbm
91+ ntg OR kjc -> kwq
92+ psh XOR fgs -> tgd
93+ qhw XOR tgd -> z09
94+ pbm OR djm -> kpj
95+ x03 XOR y03 -> ffh
96+ x00 XOR y04 -> ntg
97+ bfw OR bqk -> z06
98+ nrd XOR fgs -> wpb
99+ frj XOR qhw -> z04
100+ bqk OR frj -> z07
101+ y03 OR x01 -> nrd
102+ hwm AND bqk -> z03
103+ tgd XOR rvg -> z12
104+ tnw OR pbm -> gnj
105+ LARGER
106+ end
You can’t perform that action at this time.
0 commit comments