77 "strings"
88
99 "github.com/pkg/errors"
10+ log "github.com/sirupsen/logrus"
1011
1112 "github.com/oleg-balunenko/advent-of-code/puzzles"
1213)
@@ -32,10 +33,7 @@ func (s solution) Part1(input io.Reader) (string, error) {
3233 return "" , errors .Wrap (err , "failed to init computer" )
3334 }
3435
35- c .replace (map [int ]int {
36- 1 : 12 ,
37- 2 : 2 ,
38- })
36+ c .input (12 , 2 )
3937
4038 res , err := c .calc ()
4139 if err != nil {
@@ -46,15 +44,46 @@ func (s solution) Part1(input io.Reader) (string, error) {
4644}
4745
4846func (s solution ) Part2 (input io.Reader ) (string , error ) {
49- return "" , puzzles .ErrNotImplemented
47+ c , err := newComputer (input )
48+ if err != nil {
49+ return "" , errors .Wrap (err , "failed to init computer" )
50+ }
51+
52+ for i := 0 ; i <= 99 ; i ++ {
53+ for j := 0 ; j <= 99 ; j ++ {
54+ c .reset ()
55+
56+ c .input (i , j )
57+
58+ res , err := c .calc ()
59+ if err != nil {
60+ return "" , errors .Wrap (err , "failed to calc" )
61+ }
62+
63+ if res == 19690720 {
64+ log .WithFields (log.Fields {
65+ "noun" : i ,
66+ "verb" : j ,
67+ }).Info ("Solved at positions" )
68+ return strconv .Itoa (nounVerb (i , j )), nil
69+ }
70+ }
71+ }
72+
73+ return "" , errors .New ("can't found non and verb" )
74+ }
75+
76+ func nounVerb (noun int , verb int ) int {
77+ return 100 * noun + verb
5078}
5179
5280func (s solution ) Name () string {
5381 return s .name
5482}
5583
5684type computer struct {
57- input map [int ]int
85+ memory map [int ]int
86+ initial []int
5887}
5988
6089const (
@@ -74,15 +103,17 @@ func newComputer(input io.Reader) (computer, error) {
74103 }
75104
76105 nums := strings .Split (buf .String (), "," )
77- c .input = make (map [int ]int , len (nums ))
106+ c .initial = make ([]int , len (nums ))
107+ c .memory = make (map [int ]int , len (nums ))
78108
79109 for i , num := range nums {
80110 n , err := strconv .Atoi (num )
81111 if err != nil {
82112 return c , errors .Wrap (err , "failed to convert string to int" )
83113 }
84114
85- c .input [i ] = n
115+ c .initial [i ] = n
116+ c .memory [i ] = n
86117 }
87118
88119 return c , nil
@@ -95,8 +126,8 @@ func (c computer) calc() (int, error) {
95126 )
96127
97128loop:
98- for i := 0 ; i < len (c .input ); i += shift {
99- opt , aPos , bPos , resPos := c .input [i ], c .input [i + 1 ], c .input [i + 2 ], c .input [i + 3 ]
129+ for i := 0 ; i < len (c .memory ); i += shift {
130+ opt , aPos , bPos , resPos := c .memory [i ], c .memory [i + 1 ], c .memory [i + 2 ], c .memory [i + 3 ]
100131 switch opt {
101132 case optAdd :
102133 if err = c .add (aPos , bPos , resPos ); err != nil {
@@ -120,50 +151,58 @@ loop:
120151}
121152
122153func (c computer ) add (aPos , bPos , resPos int ) error {
123- a , ok := c .input [aPos ]
154+ a , ok := c .memory [aPos ]
124155 if ! ok {
125156 return errors .New ("value not exist" )
126157 }
127158
128- b , ok := c .input [bPos ]
159+ b , ok := c .memory [bPos ]
129160 if ! ok {
130161 return errors .New ("value not exist" )
131162 }
132163
133164 res := a + b
134- c .input [resPos ] = res
165+ c .memory [resPos ] = res
135166
136167 return nil
137168}
138169
139170func (c * computer ) mult (aPos , bPos , resPos int ) error {
140- a , ok := c .input [aPos ]
171+ a , ok := c .memory [aPos ]
141172 if ! ok {
142173 return errors .New ("value not exist" )
143174 }
144175
145- b , ok := c .input [bPos ]
176+ b , ok := c .memory [bPos ]
146177 if ! ok {
147178 return errors .New ("value not exist" )
148179 }
149180
150181 res := a * b
151- c .input [resPos ] = res
182+ c .memory [resPos ] = res
152183
153184 return nil
154185}
155186
156187func (c * computer ) abort () (int , error ) {
157- res , ok := c .input [0 ]
188+ res , ok := c .memory [0 ]
158189 if ! ok {
159190 return 0 , errors .New ("value not exist" )
160191 }
161192
162193 return res , nil
163194}
164195
165- func (c * computer ) replace (data map [int ]int ) {
166- for i , v := range data {
167- c .input [i ] = v
196+ func (c * computer ) input (noun int , verb int ) {
197+ c .memory [1 ] = noun
198+ c .memory [2 ] = verb
199+ }
200+
201+ func (c * computer ) reset () {
202+ c .memory = make (map [int ]int , len (c .initial ))
203+
204+ for i , n := range c .initial {
205+ n := n
206+ c .memory [i ] = n
168207 }
169208}
0 commit comments